Built with Tutify.io

    AIStudyLabs

    Numbers, Booleans, and None

    Numbers, Booleans, and None

    Introduction

    In this lesson, you’ll work with three fundamental building blocks of Python programs: numbers, booleans, and None. You’ll perform arithmetic with integers and floats, understand how division behaves, control program logic using boolean values and comparisons, take advantage of truthy/falsy shortcuts, and use None to represent missing data safely.


    1. Working with Numbers: Integers and Floats

    Python has several numeric types; the two core ones you’ll use constantly are:

    You’ve already assigned values to variables; now you’ll use them in arithmetic.

    1.1 Declaring and Using Integer and Float Variables

    # Basic numeric variables
    apples = 10          # int
    price_per_apple = 0.75  # float
    
    # Arithmetic operations
    total_cost = apples * price_per_apple
    
    # Updating values
    apples_bought = 3
    apples = apples - apples_bought
    remaining_cost = apples * price_per_apple
    
    print("Total cost:", total_cost)
    print("Remaining apples:", apples)
    print("Remaining cost:", remaining_cost)
    
    # Output
    Total cost: 7.5
    Remaining apples: 7
    Remaining cost: 5.25
    

    Explanation:

    1.2 Division Behavior: / vs // vs %

    Python has several division-related operators:

    students = 10
    slices = 25
    
    # True division (float result)
    avg_slices = slices / students
    
    # Floor division (whole slices each)
    whole_slices_each = slices // students
    
    # Remainder slices after fair share
    leftover_slices = slices % students
    
    print("Average slices per student:", avg_slices)
    print("Whole slices per student:", whole_slices_each)
    print("Leftover slices:", leftover_slices)
    
    # Output
    Average slices per student: 2.5
    Whole slices per student: 2
    Leftover slices: 5
    

    Explanation:

    // is particularly useful when you need integer results, for example for indexing or splitting data into fixed-size chunks.


    2. Booleans and Comparison Operators

    Booleans (bool) represent truth values: True or False. They’re essential for if statements and any logic-based decisions.

    2.1 Basic Booleans and Comparisons

    Comparison operators create boolean values:

    age = 20
    is_adult = age >= 18
    has_ticket = True
    
    can_enter = is_adult and has_ticket
    
    print("Is adult?", is_adult)
    print("Has ticket?", has_ticket)
    print("Can enter?", can_enter)
    
    # Output
    Is adult? True
    Has ticket? True
    Can enter? True
    

    Explanation:

    2.2 Using Booleans in Program Logic

    score = 73
    
    passed_exam = score >= 50
    
    if passed_exam:
        print("You passed the exam!")
    else:
        print("You did not pass. Try again.")
    
    # Output
    You passed the exam!
    

    Explanation:


    3. Truthy and Falsy Values in Conditionals

    In Python, many non-boolean values can be used directly in conditions. Python interprets them as truthy or falsy.

    Common falsy values:

    Almost everything else is truthy.

    3.1 Concise Checks with Truthy/Falsy

    Instead of writing explicit comparisons like if len(name) > 0:, you can use the value directly:

    username = input("Enter username (leave blank for guest): ")
    
    if username:
        print("Welcome,", username)
    else:
        print("Welcome, guest user")
    
    # Possible Output (if user enters Alice)
    Enter username (leave blank for guest): Alice
    Welcome, Alice
    
    # Possible Output (if user just presses Enter)
    Enter username (leave blank for guest): 
    Welcome, guest user
    

    Explanation:

    3.2 Using Numbers as Conditions

    items_in_cart = 0
    
    if items_in_cart:
        print("You have items in your cart.")
    else:
        print("Your cart is empty.")
    
    # Output
    Your cart is empty.
    

    Explanation:


    4. Using None for Missing or Unknown Data

    None is a special singleton object used to represent “no value”, “unknown”, or “not yet set”.

    4.1 Declaring and Checking for None

    # User hasn't set their nickname yet
    nickname = None
    
    if nickname is None:
        print("No nickname set. Using full name instead.")
    else:
        print("Nickname:", nickname)
    
    # Output
    No nickname set. Using full name instead.
    

    Explanation:

    4.2 Why Use is with None?

    None is a single shared object in a Python process. The is operator checks identity (same object in memory). This is the recommended, reliable way to test for None:

    result = None
    
    # Simulating an operation that might fail
    user_input = "42"
    if user_input.isdigit():
        result = int(user_input)
    
    if result is None:
        print("Conversion failed.")
    else:
        print("Converted number:", result)
    
    # Output
    Converted number: 42
    

    Explanation:

    4.3 Combining None with Truthiness

    Remember: None is falsy, so you can use it directly in conditions, but still use is when you specifically care about missing-vs-zero or missing-vs-empty:

    discount = None  # could also be 0 or a positive number later
    
    if discount is None:
        print("No discount information available.")
    elif discount == 0:
        print("No discount applied.")
    else:
        print("Discount applied:", discount, "%")
    
    # Output
    No discount information available.
    

    Explanation:


    Summary / Key Takeaways

    In this lesson on Numbers, Booleans, and None, you have learned:

    Python Programming – Complete Course with Practical Examples — Variables, Data Types, and Type Conversion

    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