Learning Objectives – after this chapter you will be able to:
  • 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
OperatorNameExample (a = 17, b = 5)Result
+Additiona + b22
-Subtractiona - b12
*Multiplicationa * b85
/Division (always float)a / b3.4
//Floor divisiona // b3
%Modulus (remainder)a % b2
**Exponent (power)a ** 2289
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)
Output
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 alphabetically
Output
True True False True
True
True
Logical Operators
OperatorReturns True when
andboth conditions are true
orat least one condition is true
notreverses 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)
Output
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)
Output
49
Bitwise, Membership and Identity Operators
TypeOperatorsMeaning
Bitwise& | ^ ~ << >>AND, OR, XOR, NOT, left shift, right shift on binary bits
Membershipin, not inIs a value present in a sequence/collection?
Identityis, is notDo 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)
Output
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)
  1. () parentheses
  2. ** exponent (right-to-left)
  3. +x -x ~x unary plus, minus, bitwise NOT
  4. * / // %
  5. + -
  6. << >>, then &, then ^, then |
  7. Comparisons, in, not in, is, is not
  8. not, then and, then or
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
Output
14
20
512
-4
3
Key Takeaways
  • / always returns a float; // floors; % gives the remainder.
  • and/or short-circuit; == compares values while is compares identity.
  • Use parentheses to make precedence explicit; ** is right-associative.
Video : Basic Operators
Question / Answer (15 questions)

Answer : / performs true division and always returns a float (10 / 2 gives 5.0). // performs floor division and returns the largest whole number less than or equal to the result (17 // 5 gives 3).

Answer : -4. Floor division rounds towards minus infinity, and -3.4 rounded down is -4, not -3.

Answer : % returns the remainder of division. 17 % 5 is 2. -17 % 5 is 3, because in Python the remainder takes the sign of the divisor and (a // b) * b + a % b always equals a.

Answer : It returns a tuple (a // b, a % b), the quotient and remainder together. divmod(17, 5) returns (3, 2).

Answer : == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal to) and <= (less than or equal to). They return True or False.

Answer : Python allows multiple comparisons in one expression, such as 10 < x < 20, which means 10 < x and x < 20.

Answer : For and, if the first operand is false Python stops and returns it without evaluating the second. For or, if the first operand is true Python stops and returns it. Hence 0 or "default" returns "default" and 5 and 10 returns 10.

Answer : They combine an operation with assignment: x += 5 means x = x + 5. Others include -=, *=, /=, //=, %= and **=.

Answer : No. Python has no increment or decrement operators; use x += 1 or x -= 1 instead.

Answer : 12 is 1100 and 10 is 1010 in binary. 12 & 10 = 1000 = 8, 12 | 10 = 1110 = 14, and 12 ^ 10 = 0110 = 6.

Answer : << shifts bits to the left (each shift multiplies by 2); >> shifts bits to the right (each shift floor-divides by 2). 12 << 1 is 24 and 12 >> 2 is 3.

Answer : in and not in test whether a value is present in a sequence or collection. "y" in "Python" is True and 4 not in [1, 2, 3] is True.

Answer : == checks whether two objects have equal values. is checks whether two names refer to the same object in memory. Two separate lists [1, 2] are == but not is.

Answer : The exponent operator is right-associative, so it is evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512.

Answer : Multiplication has higher precedence than addition, so 2 + 3 * 4 = 2 + 12 = 14. Parentheses are evaluated first, so (2 + 3) * 4 = 5 * 4 = 20.
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.

Q1. What is the output of print(7 // 2)?

Show Answer

Correct Answer : C. 3
Floor division of 7 by 2 gives 3.

Q2. What is the output of print(2 ** 3 ** 2)?

Show Answer

Correct Answer : B. 512
** is right-associative: 2 ** 9 = 512.

Q3. Which operator checks whether two names refer to the SAME object?

Show Answer

Correct Answer : A. is
is compares identity; == compares values.

Q4. What does print(0 or "default") display?

Show Answer

Correct Answer : D. default
0 is falsy, so or returns the second operand.

Q5. What is the result of 10 / 2 in Python 3?

Show Answer

Correct Answer : B. 5.0
The / operator always returns a float.

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.

  1. Python Software Foundation. (n.d.). An Informal Introduction to Python. In The Python Tutorial. https://docs.python.org/3/tutorial/introduction.html
  2. Python Software Foundation. (n.d.). Expressions (including Operator Precedence). In The Python Language Reference. https://docs.python.org/3/reference/expressions.html
  3. Python Software Foundation. (n.d.). Built-in Types. The Python Standard Library. https://docs.python.org/3/library/stdtypes.html
  4. Python Software Foundation. (n.d.). Built-in Functions. The Python Standard Library. https://docs.python.org/3/library/functions.html
  5. Lutz, M. (2013). Learning Python (5th ed.). O’Reilly Media.
  6. Downey, A. B. (2024). Think Python (3rd ed.). O’Reilly Media. (Free online edition by Green Tea Press.) https://allendowney.github.io/ThinkPython/