Built with Tutify.io

    AIStudyLabs

    Arithmetic, Comparison, and Logical Operators

    Arithmetic, Comparison, and Logical Operators

    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:

    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:

    1. **
    2. unary +, - (e.g., -x)
    3. *, /, //, %
    4. +, -
    5. comparison operators (<, >, ==, etc.)
    6. logical not
    7. logical and
    8. 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
    

    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):

    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:

    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
    

    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:

    1. age < 18 → 15 < 18 → True
    2. age >= 65 → 15 >= 65 → False
    3. age < 18 or age >= 65 → True or False → True
    4. member → True
    5. True 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:

    Python Programming – Complete Course with Practical Examples — Operators, Expressions, and Control Flow

    Getting Started with Python and the Course

    Python Syntax, Indentation, and Core Concepts

    Variables, Data Types, and Type Conversion

    Operators, Expressions, and Control Flow

    Loops and Iteration Patterns

    Working with Strings in Depth

    Collections: Lists, Tuples, Sets, and Dictionaries

    Functions and Program Structuring

    Functional Programming Concepts in Python

    Modules, Packages, and Code Organization

    Error Handling and Debugging Fundamentals

    File Handling and Data Formats

    Object-Oriented Programming in Python

    Advanced Python: Iterators, Generators, and Decorators

    Python Standard Library for Real-World Tasks

    Python for Automation and Scripting

    Testing and Quality Assurance in Python

    Tutorial Not Found