All Course > Python > Basic Syntax And Variables Oct 05, 2023

Python Basic Operations

This tutorial explores Python's basic operations which are the foundation of all Python programming. Whether you are starting your programming journey or looking to refresh your skills, this tutorial provides clear examples and practical tips to enhance your understanding of Python.

Arithmetic Operations in Python

Arithmetic operations in Python are quite straightforward and are similar to those found in many other programming languages. Python supports basic arithmetic operations like addition, subtraction, multiplication, and division, as well as more advanced operations such as exponentiation and modulus.

Addition (+): Adds two numbers together.

result = 3 + 5
print(result)  # Output: 8

Subtraction (-): Subtracts the second number from the first.

result = 10 - 4
print(result)  # Output: 6

Multiplication (*): Multiplies two numbers.

result = 6 * 7
print(result)  # Output: 42

Division (/): Divides the first number by the second. The result is a float.

result = 20 / 4
print(result)  # Output: 5.0

Floor Division (//): Divides the first number by the second and rounds down to the nearest integer.

result = 20 // 3
print(result)  # Output: 6

Modulus (%): Returns the remainder of the division of the first number by the second.

result = 22 % 5
print(result)  # Output: 2

Exponentiation (**): Raises the first number to the power of the second number.

result = 3 ** 4
print(result)  # Output: 81

Unary Minus (-): Negates the value of the number.

result = -5
print(result)  # Output: -5

Unary Plus (+): Returns the value of the number (mostly used for readability).

result = +5
print(result)  # Output: 5

Combining Python Arithmetic Operations

Python allows you to combine multiple arithmetic operations in a single expression. When doing so, it follows the standard order of operations (PEMDAS/BODMAS rules).

  • Parentheses ()
  • Exponents **
  • Multiplication *, Division /, Floor Division //, and Modulus %
  • Addition + and Subtraction -
result = (3 + 5) * 2 ** 2 / 4 - 1
print(result)  # Output: 7.0

Using Arithmetic Operations in Variables

You can use variables to store values and perform arithmetic operations on them.

a = 10
b = 3
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
floor_division = a // b
modulus = a % b
exponentiation = a ** b

print(addition)        # Output: 13
print(subtraction)     # Output: 7
print(multiplication)  # Output: 30
print(division)        # Output: 3.3333333333333335
print(floor_division)  # Output: 3
print(modulus)         # Output: 1
print(exponentiation)  # Output: 1000

Conclusion

Understanding the basics of arithmetic operations in Python gives you a solid foundation for more advanced programming. This articles explained you with the knowledge needed to tackle arithmetic challenges in Python.

FAQs

Q: What is Python best used for?
A: Python is versatile, ideal for web development, data analysis, artificial intelligence, and more.

Q: How do I start learning Python?
A: Start with the basics like the operations covered in this guide, and practice regularly.

Q: Can I use Python for web development?
A: Yes, Python frameworks like Django and Flask are designed for developing web applications.

Comments

There are no comments yet.

Write a comment

You can use the Markdown syntax to format your comment.