- Use arithmetic, comparison, logical and assignment operators.
- Explain the difference between
/and//, and how%works. - Apply bitwise, membership and identity operators.
- Evaluate expressions using operator precedence.
Arithmetic Operators
| Operator | Name | Example (a = 17, b = 5) | Result |
|---|---|---|---|
+ | Addition | a + b | 22 |
- | Subtraction | a - b | 12 |
* | Multiplication | a * b | 85 |
/ | Division (always float) | a / b | 3.4 |
// | Floor division | a // b | 3 |
% | Modulus (remainder) | a % b | 2 |
** | Exponent (power) | a ** 2 | 289 |
a, b = 17, 5 print(a / b, a // b, a % b, a ** 2) print(10 / 2) # / always gives a float print(-17 // 5) # floor division rounds DOWN print(-17 % 5) # result takes the sign of the divisor print(divmod(17, 5)) # (quotient, remainder)
3.4 3 2 289 5.0 -4 3 (3, 2)
// rounds down towards minus infinity, so -17 // 5 is -4, not -3. Python always keeps (a // b) * b + a % b == a.Comparison (Relational) Operators
==, !=, >, <, >=, <= compare two values and return True or False. Comparisons can be chained.
x = 15
print(x == 15, x != 10, x > 20, x <= 15)
print(10 < x < 20) # chained: same as 10 < x and x < 20
print("apple" < "banana") # strings compare alphabeticallyTrue True False True True True
Logical Operators
| Operator | Returns True when |
|---|---|
and | both conditions are true |
or | at least one condition is true |
not | reverses the result |
age, has_id = 20, True print(age >= 18 and has_id) print(age < 18 or has_id) print(not has_id) # Short-circuit: and / or return one of the operands print(0 or "default") print(5 and 10)
True True False default 10
Short-circuit evaluation: and stops at the first false value, or stops at the first true value, and each returns the operand where it stopped.
Assignment Operators
= assigns; compound operators update a variable: +=, -=, *=, /=, //=, %=, **=. Python has no ++ or -- operators.
n = 10 n += 5 # n = n + 5 n *= 2 # n = n * 2 n //= 4 # n = n // 4 n **= 2 # n = n ** 2 print(n)
49
Bitwise, Membership and Identity Operators
| Type | Operators | Meaning |
|---|---|---|
| Bitwise | & | ^ ~ << >> | AND, OR, XOR, NOT, left shift, right shift on binary bits |
| Membership | in, not in | Is a value present in a sequence/collection? |
| Identity | is, is not | Do two names refer to the same object? |
a, b = 12, 10 # 1100 and 1010 in binary
print(a & b, a | b, a ^ b, ~a, a << 1, a >> 2)
print("y" in "Python", 4 not in [1, 2, 3])
list1 = [1, 2]
list2 = [1, 2]
list3 = list1
print(list1 == list2, list1 is list2, list1 is list3)8 14 6 -13 24 3 True True True False True
== compares values; is compares identity. Use is mainly to compare with None (if x is None:).
Operator Precedence (highest to lowest)
()parentheses**exponent (right-to-left)+x -x ~xunary plus, minus, bitwise NOT* / // %+ -<< >>, then&, then^, then|- Comparisons,
in,not in,is,is not not, thenand, thenor
print(2 + 3 * 4) print((2 + 3) * 4) print(2 ** 3 ** 2) # ** is right-associative: 2 ** 9 print(-2 ** 2) # ** binds tighter than unary minus print(10 - 4 - 3) # left-to-right
14 20 512 -4 3
Key Takeaways
/always returns a float;//floors;%gives the remainder.and/orshort-circuit;==compares values whileiscompares identity.- Use parentheses to make precedence explicit;
**is right-associative.
Video : Basic Operators
Question / Answer (15 questions)
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Python Software Foundation. (n.d.). An Informal Introduction to Python. In The Python Tutorial. https://docs.python.org/3/tutorial/introduction.html
- Python Software Foundation. (n.d.). Expressions (including Operator Precedence). In The Python Language Reference. https://docs.python.org/3/reference/expressions.html
- Python Software Foundation. (n.d.). Built-in Types. The Python Standard Library. https://docs.python.org/3/library/stdtypes.html
- Python Software Foundation. (n.d.). Built-in Functions. The Python Standard Library. https://docs.python.org/3/library/functions.html
- Lutz, M. (2013). Learning Python (5th ed.). O’Reilly Media.
- Downey, A. B. (2024). Think Python (3rd ed.). O’Reilly Media. (Free online edition by Green Tea Press.) https://allendowney.github.io/ThinkPython/