Arithmetic Operations
Arithmetic operations are essential in any programming language. Python provides various operators to perform calculations.
Basic Arithmetic Operators
Python supports the following arithmetic operations:
Operator | Symbol | Example | Description |
---|---|---|---|
Addition | + |
10 + 5 |
Adds two numbers |
Subtraction | - |
10 - 5 |
Subtracts second number from first |
Multiplication | * |
10 * 5 |
Multiplies two numbers |
Division | / |
10 / 5 |
Returns a floating-point division result |
Floor Division | // |
10 // 3 |
Returns the quotient without the remainder |
Modulus | % |
10 % 3 |
Returns the remainder of division |
Exponentiation | ** |
2 ** 3 |
Raises a number to a power |
Examples of Arithmetic Operations
a = 10
b = 3
print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.3333
print("Floor Division:", a // b) # 3
print("Modulus:", a % b) # 1
print("Exponentiation:", a ** b) # 1000
Order of Operations (PEMDAS Rule)
Python follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction) for evaluating expressions.
Example:
result = 5 + 2 * 3
print(result) # Output: 11 (Multiplication first)
result = (5 + 2) * 3
print(result) # Output: 21 (Parentheses first)
Using Arithmetic Operators with Different Data Types
print(10 + 3.5) # Output: 13.5 (int + float = float)
print("Hello" * 3) # Output: HelloHelloHello (String repetition)
Exercise: Try It Yourself!
- Calculate
15 % 4
and print the result. - Compute
(8 + 2) ** 2 / 5
and print the result. - Multiply
"Python"
by3
and print the result. - Predict and print the output of
7 // 2
.