Python for AIPython for AI
Python Basics

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)
python
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)
python
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)
python
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
Be careful! == 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: Returns True if BOTH conditions are true.
  • or: Returns True if AT LEAST ONE condition is true.
  • not: Reverses the logical state of its operand (True becomes False, False becomes True).
python
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.

text
# 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  True

Assignment 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 += 5 is same as x = x + 5
  • -= (subtract and assign): x -= 5 is same as x = x - 5
  • *= (multiply and assign): x *= 5 is same as x = x * 5
  • /= (divide and assign): x /= 5 is same as x = x / 5
  • And similarly for %=, **=, //=, etc.
python
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 and where or is more appropriate (or vice versa) in logical conditions.
When in doubt about operator precedence, use parentheses to explicitly define the order of operations. It makes your code clearer too!

Practice

Try these exercises to lock in what you just learned.

Quiz
Not started
What is the result of `7 // 2` in Python?
Fill in the blanks
Not started
Complete the expression so `is_big` is True when x is over 100.
1x = 150
2
3print(is_big)
Code challenge
Not started
Print True if n is even, otherwise False. n is 8.

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.