Operators tell Python what to do with values.
An operator tells Python which operation to perform on one or more values. Building on Variables and Data Types, read a simple expression as value → operator → value → result. Different operators calculate values, ask comparison questions, or combine Boolean results.
10 + 3Python sees the value 10, the operator +, and the value 3. The + operator tells Python to add the two numbers, so the result is 13. Operators are not just symbols to memorize: each one tells Python how to work with values.
value → operator → value → result
10 → + → 3 → 13In this lesson, arithmetic operators calculate values, comparison operators produce a bool value, and logical operators combine or change Boolean results. The type of a value still matters: you learned in Data Types that Python can treat numbers and text differently.
Data types still matter: Arithmetic operators in this lesson work with numbers. As you learned in Data Types, the same symbol can behave differently with text, so first notice what type of value Python is working with.
x = 10
y = 3
print(x + y) # 13
print(x - y) # 7
print(x * y) # 30
print(x / y) # 3.333...
print(x // y) # 3
print(x % y) # 1
print(2 ** 3) # 8+, -, and * add, subtract, and multiply. The other arithmetic operators deserve a closer look: / divides, // counts complete groups, % gives the remainder, and ** raises a number to a power.
print(10 / 2) # 5.0
print(10 // 3) # 3
print(10 % 3) # 1
# 10 = 3 × 3 + 1
number = 8
print(number % 2) # 0The / operator performs ordinary division and returns a float, even when the answer looks like a whole number: 10 / 2 is 5.0. With positive numbers, 10 // 3 tells us that three complete groups of three fit into ten, while 10 % 3 gives the remainder, 1. A remainder of 0 means a number divides evenly by 2 — useful later when working with even and odd numbers.
print(2 ** 3) # 8
# 2 ** 3 means 2 × 2 × 2The ** operator is exponentiation. 2 ** 3 means multiply 2 by itself three times: 2 × 2 × 2, which gives 8.
age = 18
print(age == 18) # True
print(age != 20) # True
print(age > 20) # False
print(age >= 18) # True
print(age < 21) # True
print(age <= 18) # TrueA comparison operator asks Python a question, such as “Is age greater than or equal to 18?” Its answer is always True or False — a bool value from the Data Types lesson. ==, !=, >, <, >=, and <= all make comparisons.
age = 18 # assign 18 to age
print(age == 18) # ask whether age equals 18= assigns a value to a name: age = 18. == asks whether a value is equal: age == 18. Remember: = assigns; == compares.
age = 20
has_ticket = True
is_raining = False
print(age >= 18 and has_ticket) # True
print(age < 18 or has_ticket) # True
print(not is_raining) # Trueand is True when both conditions are True. or is True when at least one condition is True. not reverses a Boolean value: not False becomes True. Think: and means both, or means at least one, and not means the opposite. You will use these ideas in Conditions later.
print(2 + 3 * 4) # 14
print((2 + 3) * 4) # 20Python does not always evaluate an expression from left to right. Multiplication happens before addition, so 2 + 3 * 4 is 14. Parentheses change the order, so (2 + 3) * 4 is 20. When an expression could be unclear, use parentheses to show your intended order.
score = 12
bonus = 3
print(score + bonus * 2)
print(score >= 10)
print(score >= 10 and bonus > 0)Pause before running this program. What do you predict? Python first multiplies bonus * 2, then adds it to score, so the first result is 18. The next two lines ask comparison questions; both produce True. Reading expressions step by step helps you understand code instead of only copying it.
