Remember
Operators combine values. / performs true division; // performs floor division, including for negative numbers.
Operators combine values. / performs true division; // performs floor division, including for negative numbers.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = 7
print(number / 2)
print(number // 2)
print(number % 2)Top-level scope
7Operators combine values. / performs true division; // performs floor division, including for negative numbers.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 7
print(number / 2)
print(number // 2)
print(number % 2)
|
v
DISPLAYED RESULT
3.5
3
1
FINAL BINDINGS
number --> object 1 (int)
object 1: 7Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = 7
print(number / 2)
print(number // 2)
print(number % 2)Top-level scope
7The example has finished. Compare the final output with the values and references above.
3.5 3 1
What do you expect this program to print?
number = 7
print(number / 2)
print(number // 2)
print(number % 2)Operators in Python are symbols or keywords that help you perform different kinds of actions, like calculations, comparisons, or checking conditions. Let’s explore each type of operator with easy-to-understand examples.
These are used to do basic math like addition, subtraction, multiplication, etc.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 3 + 2 | 5 |
| - | Subtraction | 5 - 2 | 3 |
| * | Multiplication | 4 * 2 | 8 |
| / | Division | 6 / 2 | 3.0 |
| % | Modulus (Remainder) | 5 % 2 | 1 |
| // | Floor Division | 5 // 2 | 2 |
| ** | Exponent (Power) | 2 ** 3 | 8 |
a = 10
b = 3
print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.333...
print("Modulus:", a % b) # 1
print("Floor Division:", a // b) # 3
print("Exponent:", a ** b) # 1000 (10³)
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus: 1
Floor Division: 3
Exponent: 1000
These are used to compare two values. The result is always either True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 3 == 3 | True |
| != | Not equal to | 5 != 2 | True |
| > | Greater than | 5 > 3 | True |
| < | Less than | 2 < 4 | True |
| >= | Greater or equal | 4 >= 4 | True |
| <= | Less or equal | 3 <= 5 | True |
x = 7
y = 10
print(x == y) # False, because 7 is not equal to 10
print(x != y) # True, because 7 is not equal to 10
print(x > y) # False, because 7 is not greater than 10
print(x < y) # True, because 7 is less than 10
print(x >= y) # False, because 7 is not greater than or equal to 10
print(x <= y) # True, because 7 is less than or equal to 10
False
True
False
True
False
True
These are used to assign or update values of variables.
| Operator | Meaning | Example |
|---|---|---|
| = | Assign | x = 5 |
| += | Add and assign | x += 3 (same as x = x + 3) |
| -= | Subtract and assign | x -= 2 |
| *= | Multiply and assign | x *= 4 |
| /= | Divide and assign | x /= 2 |
| %= | Modulus and assign | x %= 3 |
| //= | Floor divide and assign | x //= 2 |
| **= | Power and assign | x **= 2 |
The following example applies each of the assignment operators one after the other, showing how the value of x changes step by step:
x = 5 # Start with assignment
x += 3 # x = x + 3 → x becomes 8
x -= 2 # x = x - 2 → x becomes 6
x *= 4 # x = x * 4 → x becomes 24
x /= 2 # x = x / 2 → x becomes 12.0
x %= 5 # x = x % 5 → x becomes 2.0
x //= 2 # x = x // 2 → x becomes 1.0 (floor division)
x **= 3 # x = x ** 3 → x becomes 1.0 (1 raised to power 3)
print(x)
1.0
Used to combine multiple conditions.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| and | True if both are True | 5 > 3 and 4 > 2 | True |
| or | True if at least one is True | 5 > 3 or 2 > 4 | True |
| not | Reverse the result | not(5 == 5) | False |
This example shows how logical operators work in different scenarios:
a = 7
b = 3
# AND: both conditions must be True
print(a > 5 and b < 5)
# OR: at least one condition must be True
print(a < 5 or b < 5)
# NOT: reverses the result
print(not a == 7)
# Combined logic
print(not (a < 10 and b > 5))
True
True
False
True
Used to check if two variables point to the same object.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| is | True if both are same object | a is b | True or False |
| is not | True if not same object | a is not b | True or False |
a = [1, 2]
b = a
print(a is b) # True
print(a is not b) # False
True
False
True
Used to check if a value is inside a group (like a list or string).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| in | True if value is present | "a" in "apple" | True |
| not in | True if value not present | "x" not in "apple" | True |
colors = ["red", "blue"]
print("red" in colors) # True
print("green" not in colors) # True
True
True
These work on binary (0s and 1s). You won’t need them yet, but here’s what they do:
| Operator | Name |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left Shift |
| >> | Right Shift |
a = 5 # 0101 in binary
b = 3 # 0011 in binary
print(a & b) # 1
print(a | b) # 7
print(a ^ b) # 6
print(~a) # -6
print(a << 1) # 10
print(a >> 1) # 2
1
7
6
-6
10
2
Operators are the tools that help you build logic in your Python programs. Now that you’ve learned the basics, try using them in your own mini programs!
Connect what you noticed in the example with the idea below.
Operators combine values. / performs true division; // performs floor division, including for negative numbers.
// always truncates toward zero.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.