Skip to main content

Learning outcomes

By the end of this chapter, you should be able to:
  • apply arithmetic operators
  • write relational expressions
  • evaluate logical expressions
  • understand precedence basics

30-minute recording plan

  • 0-8 min: arithmetic operators
  • 8-14 min: / vs // and % edge cases
  • 14-20 min: relational operators and chained comparisons
  • 20-26 min: logical operators (and, or, not)
  • 26-30 min: precedence and MCQ output prediction

Expression

An expression is a combination of values, variables, and operators that evaluates to a result.

Arithmetic operators

+, -, *, /, //, %, **
a, b = 10, 3
print(a + b)
print(a // b)
print(a % b)
print(a ** b)
Quick notes:
  • / gives decimal (float) output
  • // gives floor result
  • % helps in divisibility checks
  • ** is exponent (power)

Beginner alert: / vs //

/ gives exact decimal division, but // gives floor division.
print(7 / 3)    # 2.333...
print(7 // 3)   # 2
print(-7 // 3)  # -3 (goes to lower integer)
Many beginners expect -7 // 3 to be -2, but Python floors downward.

Relational operators

==, !=, >, <, >=, <=
x = 7
print(x > 5)
print(x == 7)
Chained form is common and readable:
temp = 32
print(20 <= temp <= 35)

Logical operators

and, or, not
att = 82
fees = True
print(att >= 75 and fees)
Logical operators evaluate conditions left to right and are heavily used in if statements.

MCQ trap: and/or may return operands

In Python, and and or do not always return True/False. They can return one of the original operands. Rules:
  • x and y: returns first falsey operand, otherwise returns last operand
  • x or y: returns first truthy operand, otherwise returns last operand
Examples:
print(1 and 2)          # 2
print(0 and 5)          # 0
print("" or "hello")    # hello
print("2" and 4 and None)  # None
When wrapped with bool(...), final answer becomes boolean:
print(bool(1 and 2 or 0))  # True

Operator precedence (basic)

  1. ()
  2. **
  3. unary +, -
  4. *, /, //, %
  5. +, -
  6. relational operators (<, <=, >, >=, ==, !=, in, not in, is, is not)
  7. not
  8. and
  9. or
Use parentheses to remove ambiguity. Worked example:
print(5 + 2 * 3 ** 2)  # 23
Step order:
  1. 3 ** 2 -> 9
  2. 2 * 9 -> 18
  3. 5 + 18 -> 23

Mini showcase: scholarship eligibility check

marks = int(input("Enter marks: "))
attendance = int(input("Enter attendance %: "))
income = int(input("Enter family income: "))

eligible = (marks >= 85) and (attendance >= 75) and (income <= 500000)

print(f"Scholarship eligible: {eligible}")
Concepts used:
  • relational operators (>=, <=)
  • logical and
  • grouping with parentheses

Exam-focused points

  • / always returns float
  • // is floor division
  • % gives remainder
  • logical operators return boolean result in standard comparisons
  • plain and/or expressions can return non-boolean operands

Practice questions

  1. Evaluate manually, then verify in Python:
5 + 2 * 3 ** 2
  1. Write expression to check if a number is between 10 and 50.
  2. Write condition for pass: marks >= 40 and attendance >= 75.
  3. Predict:
print("2" and 4 and None)
print(bool(1 and 2 or 0))