Code Examples
Example 1
The addition operator (+) combines two values. It works with integers, floating-point numbers, and even strings (for concatenation). When adding an integer and a float, Python automatically converts the result to a float.
# Adding two numbers
result = 5 + 3
print(result) # Output: 8
# Adding variables
x = 10
y = 7
sum_result = x + y
print(sum_result) # Output: 17
# Adding different data types
integer_value = 5
float_value = 2.5
combined = integer_value + float_value
print(combined) # Output: 7.5
Example 2
The subtraction operator (-) calculates the difference between two values. It can produce negative results when the second value is larger than the first.
# Subtracting two numbers
result = 10 - 4
print(result) # Output: 6
# Subtracting variables
total = 100
discount = 25
final_price = total - discount
print(final_price) # Output: 75
# Negative numbers
temperature = -5
change = 3
new_temperature = temperature - change
print(new_temperature) # Output: -8
Example 3
The multiplication operator (*) calculates the product of two values. It can also be used with strings to repeat them a specified number of times.
# Multiplying two numbers
result = 6 * 4
print(result) # Output: 24
# Calculating area
length = 5
width = 3
area = length * width
print(area) # Output: 15
# Repeating strings (a special use case)
print("*" * 10) # Output: **********
Example 4
The division operator (/) divides the first number by the second. In Python 3, division always returns a float value, even if the numbers divide evenly.
# Regular division (always returns a float)
result = 10 / 2
print(result) # Output: 5.0
result = 9 / 2
print(result) # Output: 4.5
# Division with variables
total_cost = 85
num_people = 4
cost_per_person = total_cost / num_people
print(cost_per_person) # Output: 21.25
Example 5
Floor division (//) returns the whole number part of the division, discarding any remainder. It always rounds down to the nearest integer, which is important to remember when dealing with negative numbers.
# Floor division (returns the integer part of the division)
result = 9 // 2
print(result) # Output: 4
result = 10 // 3
print(result) # Output: 3
# Negative numbers behave differently
result = -10 // 3
print(result) # Output: -4 (rounds down, not toward zero)
Example 6
The modulus operator (%) returns the remainder after division. It's commonly used to check if numbers are even or odd, or to cycle through a range of values.
# Modulus (returns the remainder of division)
result = 10 % 3
print(result) # Output: 1
result = 15 % 4
print(result) # Output: 3
# Checking if a number is even or odd
number = 7
is_even = number % 2 == 0
print(is_even) # Output: False (since 7 is odd)
Example 7
The exponentiation operator (**) raises the first number to the power of the second. It can be used for various calculations, including square roots by using 0.5 as the exponent.
# Exponentiation (raising to a power)
result = 2 ** 3
print(result) # Output: 8 (2³ = 2×2×2 = 8)
# Calculating area of a circle
radius = 5
pi = 3.14
area = pi * (radius ** 2)
print(area) # Output: 78.5
# Square root (using fractional exponent)
number = 16
square_root = number ** 0.5
print(square_root) # Output: 4.0
Example 8
Python follows the standard order of operations (PEMDAS): Parentheses, Exponents, Multiplication/Division (from left to right), Addition/Subtraction (from left to right). You can use parentheses to override this default order and make your code clearer.
# Multiple operations in one expression
result = 10 + 3 * 2
print(result) # Output: 16 (not 26, because multiplication happens first)
# Using parentheses to change the order
result = (10 + 3) * 2
print(result) # Output: 26
# Complex example
result = 4 + 6 / 2 - 1 * 2
print(result) # Output: 5.0 (6/2=3, 1*2=2, 4+3-2=5)