Operators
Math, comparisons, and logic in Python
Operators are special symbols or keywords that tell Python to perform an action, such as mathematical calculations or comparisons.
What are operators?
Think of operators as the verbs of Python. They let you manipulate data and make decisions in your code.
You'll use operators constantly to perform calculations, check if values are equal, or combine conditions.
Arithmetic operators
These are the operators you're familiar with from math. They perform numerical calculations.
+(addition)-(subtraction)*(multiplication)/(division)%(modulo - gives the remainder)**(exponentiation - raises to a power)//(floor division - divides and rounds down to the nearest whole number)
print(10 + 3) # Output: 13
print(10 - 3) # Output: 7
print(10 * 3) # Output: 30
print(10 / 3) # Output: 3.3333333333333335 (float division)
print(10 % 3) # Output: 1 (remainder)
print(2 ** 3) # Output: 8 (2 to the power of 3)
print(10 // 3) # Output: 3 (integer division)Order of operations
Just like in school, Python follows the standard order of operations (PEMDAS/BODMAS).
- Parentheses first
() - Exponents
** - Multiplication
*, Division/, Modulo%, Floor Division//(from left to right) - Addition
+, Subtraction-(from left to right)
result = 5 + 2 * 3 # 5 + 6 = 11
print(result)
result = (5 + 2) * 3 # 7 * 3 = 21
print(result)Comparison operators
These operators compare two values and return either True or False. This is how your programs make decisions.
==(equal to)!=(not equal to)>(greater than)<(less than)>=(greater than or equal to)<=(less than or equal to)
print(5 == 5) # Output: True
print(5 == 6) # Output: False
print('hello' != 'world') # Output: True
print(10 > 5) # Output: True
print(7 <= 7) # Output: True== is for comparison. = is for assignment (giving a value to a variable).Logical operators
Logical operators combine conditional statements. They also return True or False.
and: ReturnsTrueif BOTH conditions are true.or: ReturnsTrueif AT LEAST ONE condition is true.not: Reverses the logical state of its operand (True becomes False, False becomes True).
age = 25
has_license = True
print(age > 18 and has_license) # Output: True (both are true)
print(age < 20 or has_license) # Output: True (one is true)
print(not has_license) # Output: False (reverses True)Truth tables
Truth tables help visualize how logical operators work with all possible input combinations.
# Operator: and
# A B A and B
# True True True
# True False False
# False True False
# False False False
# Operator: or
# A B A or B
# True True True
# True False True
# False True True
# False False False
# Operator: not
# A not A
# True False
# False TrueAssignment shortcuts
Sometimes you want to perform an operation and assign the result back to the same variable. Python has shortcuts for this.
+=(add and assign):x += 5is same asx = x + 5-=(subtract and assign):x -= 5is same asx = x - 5*=(multiply and assign):x *= 5is same asx = x * 5/=(divide and assign):x /= 5is same asx = x / 5- And similarly for
%=,**=,//=, etc.
score = 100
score += 20 # score is now 120
print(score)
health = 50
health -= 15 # health is now 35
print(health)Common mistakes
Watch out for these common errors when using operators.
- Confusing
=(assignment) with==(comparison). - Forgetting order of operations and not using parentheses when needed.
- Using
andwhereoris more appropriate (or vice versa) in logical conditions.
Practice
Try these exercises to lock in what you just learned.
1x = 15023print(is_big)
What's next?
Now that you know how to operate on data, you'll want to store that data and make decisions based on it.
Next, we'll explore variables to hold values, and then control flow statements that use these operators to change how your program runs.