Introduction
In this lesson, you’ll work with three fundamental building blocks of Python logic: arithmetic operators, comparison operators, and logical operators. You’ll learn how to perform calculations, compare values, combine conditions, and reason about expressions step-by-step using Boolean logic and simple truth tables.
We’ll build on your knowledge of numbers, booleans, and control flow to write clearer, more reliable decision-making code.
1. Arithmetic Operators in Python
Python provides standard arithmetic operators that work with integers, floats, and variables:
+addition-subtraction*multiplication/true division (always returns a float)//floor division (integer quotient)%modulo (remainder)**exponentiation
1.1 Basic arithmetic with variables
price = 19.99
quantity = 3
total = price * quantity
discount_rate = 0.10
discount_amount = total * discount_rate
final_total = total - discount_amount
print("Total:", total)
print("Discount:", discount_amount)
print("Final total:", final_total)
# Output
Total: 59.97
Discount: 5.997
Final total: 53.973
Here we combine integers (quantity) and floats (price, discount_rate). Python automatically promotes to float when needed. The final result is a float representing the discounted total.
1.2 Floor division and modulo
minutes = 135
hours = minutes // 60 # whole hours
remaining_minutes = minutes % 60
print("Hours:", hours)
print("Remaining minutes:", remaining_minutes)
# Output
Hours: 2
Remaining minutes: 15
// discards the fractional part; % returns the remainder. This combination is very common when splitting quantities into larger and smaller units (hours/minutes, euros/cents, etc.).
2. Operator Precedence and Parentheses
Whenever you combine multiple operators in a single expression, operator precedence determines the order of evaluation. In Python, the relevant order (from higher to lower) for this lesson is:
**- unary
+,-(e.g.,-x) *,/,//,%+,-- comparison operators (
<,>,==, etc.) - logical
not - logical
and - logical
or
2.1 Complex arithmetic expression
a = 10
b = 3
c = 2
result_1 = a + b * c
result_2 = (a + b) * c
result_3 = a ** b / c
print("result_1:", result_1)
print("result_2:", result_2)
print("result_3:", result_3)
# Output
result_1: 16
result_2: 26
result_3: 500.0
result_1: multiplication happens before addition →10 + 3 * 2 = 10 + 6 = 16.result_2: parentheses override precedence →(10 + 3) * 2 = 13 * 2 = 26.result_3: exponentiation first →10 ** 3 = 1000, then division →1000 / 2 = 500.0.
Rule of thumb: whenever an expression looks non-trivial, add parentheses to make the order explicit and improve readability.
3. Comparison Operators and Boolean Results
Comparison operators evaluate to a Boolean (True or False):
==equal to!=not equal to<,>,<=,>=
3.1 Simple comparisons
age = 20
adult_age = 18
is_adult = age >= adult_age
is_teen = age >= 13 and age <= 19
print("is_adult:", is_adult)
print("is_teen:", is_teen)
# Output
is_adult: True
is_teen: False
The comparisons produce Boolean values that can be stored in variables and later combined with logical operators.
3.2 Chained comparisons
Python lets you chain comparisons for more readable range checks:
temperature = 22
comfortable = 18 <= temperature <= 25
print("Comfortable temperature?", comfortable)
# Output
Comfortable temperature? True
18 <= temperature <= 25 is equivalent to (18 <= temperature) and (temperature <= 25) but more concise and readable.
4. Logical Operators: and, or, not
Logical operators combine or invert Boolean values and conditions:
and→ True if both operands are Trueor→ True if at least one operand is Truenot→ flips True ↔ False
4.1 Combining conditions for decision making
age = 17
has_parent_permission = True
can_watch_pg13 = age >= 13
can_watch_rated_r = (age >= 18) or (has_parent_permission and age >= 16)
print("PG-13 allowed:", can_watch_pg13)
print("R-rated allowed:", can_watch_rated_r)
# Output
PG-13 allowed: True
R-rated allowed: True
can_watch_pg13: depends only on age.can_watch_rated_r: user qualifies either by being 18+ or by being at least 16 and having permission.
4.2 not for negation
logged_in = False
show_login_prompt = not logged_in
print("Show login prompt?", show_login_prompt)
# Output
Show login prompt? True
not is often used to express the opposite of a condition.
5. Evaluating Expressions Step-by-Step with Boolean Logic
Understanding how Python evaluates logical expressions helps you debug and design conditions. Consider this expression:
age = 15
member = True
eligible_for_discount = (age < 18 or age >= 65) and member
print("Eligible for discount?", eligible_for_discount)
# Output
Eligible for discount? True
Evaluate it step-by-step:
age < 18→15 < 18→Trueage >= 65→15 >= 65→Falseage < 18 or age >= 65→True or False→Truemember→TrueTrue and True→True
So eligible_for_discount is True.
5.1 Truth tables for logical operators
You don’t need to memorize these deeply, but they’re useful for reasoning about conditions.
AND (and) truth table
| A | B | A and B | |-------|-------|---------| | True | True | True | | True | False | False | | False | True | False | | False | False | False |
OR (or) truth table
| A | B | A or B | |-------|-------|--------| | True | True | True | | True | False | True | | False | True | True | | False | False | False |
NOT (not) truth table
| A | not A | |-------|-------| | True | False | | False | True |
5.2 Debugging complex conditions
A practical way to debug is to break a complex condition into named intermediate variables.
age = 30
income = 45000
meets_age_requirement = 25 <= age <= 60
meets_income_requirement = income >= 40000
eligible_for_loan = meets_age_requirement and meets_income_requirement
print("meets_age_requirement:", meets_age_requirement)
print("meets_income_requirement:", meets_income_requirement)
print("eligible_for_loan:", eligible_for_loan)
# Output
meets_age_requirement: True
meets_income_requirement: True
eligible_for_loan: True
By printing intermediate Booleans, you can see exactly which part of a condition causes a final True or False result.
Summary / Key Takeaways
In this lesson on Arithmetic, Comparison, and Logical Operators, you learned:
- Arithmetic operators (
+,-,*,/,//,%,**) let you perform calculations with integers, floats, and variables. - Operator precedence determines evaluation order; parentheses should be used to clarify complex expressions and avoid ambiguity.
- Comparison operators (
==,!=,<,>,<=,>=) always produce Boolean results that can be stored, combined, and printed. - Chained comparisons provide a concise and readable way to express range checks in Python.
- Logical operators (
and,or,not) combine or invert conditions to create powerful decision-making logic. - Truth tables for
and,or, andnothelp you reason about and predict the outcome of Boolean expressions. - Step-by-step evaluation and breaking conditions into intermediate Boolean variables is a practical debugging strategy for complex expressions.
- Combining arithmetic, comparison, and logic allows you to encode real-world rules such as discounts, eligibility checks, and access control in your programs.
