Tag: for loops

  • `for` loops in Python

    `for` loops in Python

    # for loops
    # About for loops
    # for loops are used to iterate over a sequence (list, tuple, string) or other 
    # iterable objects.
    
    # Syntax
    # for variable in sequence:
    #    code block
    # Examples
    
    # Iterate over a list
    # A list is a collection which is ordered and changeable.
    print("\nIterate over a list")
    fruits = ["apple", "banana", "cherry"]
    for fruit in fruits:
        print(fruit)
        # Output
        # apple
        # banana
        # cherry
    
    # Iterate over a string
    # A string is a sequence of characters
    print("\nIterate over a string")
    for letter in "apple":
        print(letter)
        # Output
        # a
        # p
        # p
        # l
        # e
    
    # Iterate over a range
    # A range is a sequence of numbers, starting from 0 by default, and increments
    print("\nIterate over a range")
    for i in range(5):
        print(i)
        # Output
        # 0
        # 1
        # 2
        # 3
        # 4
        # Note: The range() function returns a sequence of numbers, starting from 0 
        # by default, and increments by 1 (by default), and stops before a 
        # specified number.
    
    # Iterate over a tuple
    # A tuple is a collection which is ordered and unchangeable.
    print("\nIterate over a tuple")
    colors = ("red", "green", "blue")
    for color in colors:
        print(color)
        # Output
        # red
        # green
        # blue
    
    # Iterate over a dictionary
    # A dictionary is a collection which is unordered, changeable and indexed.
    print("\nIterate over a dictionary")
    person = {
        "name": "Alice",
        "age": 25,
        "is_active": True
    }
    for key in person:
        print(key, person[key])
        # Output
        # name Alice
        # age 25
        # is_active True
        # Note: The for loop iterates over the keys of the dictionary.
    
    # Iterate over a dictionary using items() method
    # The items() method returns a view object that displays a list of a given
    # dictionary's key-value tuple pair.
    print("\nIterate over a dictionary using items() method")
    for key, value in person.items():
        print(key, value)
        # Output
        # name Alice
        # age 25
        # is_active True
        # Note: The for loop iterates over the key-value pairs of the dictionary.
        if key == "age" and value >= 18:
            print(person["name"], "is an adult.")
    
    # Iterate over a dictionary using values() method
    # The values() method returns a view object that displays a list of all the
    # values in the dictionary.
    print("\nIterate over a dictionary using values() method")
    for value in person.values():
        print(value)
        # Output
        # Alice
        # 25
        # True
        # Note: The for loop iterates over the values of the dictionary.
    
    # Iterate over a dictionary using keys() method
    # The keys() method returns a view object that displays a list of all the keys
    # in the dictionary.
    print("\nIterate over a dictionary using keys() method")
    for key in person.keys():
        print(key)
        # Output
        # name
        # age
        # is_active
        # Note: The for loop iterates over the keys of the dictionary.
    
    # Iterate over a dictionary using get() method
    # The get() method returns the value of the specified key.
    print("\nIterate over a dictionary using get() method")
    print(person.get("name"))
    
    # Nested for loops
    
    # clients
    clients = [
        {"name": "Alice", "age": 25},
        {"name": "Bob", "age": 30},
        {"name": "Charlie", "age": 35}
    ]
    # products
    products = ["apple", "banana", "cherry"]
    inventory = {
        "apple": 10,
        "banana": 20,
        "cherry": 30
    }
    # orders
    orders = [
        {"client": "Alice", "product": "apple"},
        {"client": "Bob", "product": "banana"},
        {"client": "Charlie", "product": "cherry"}
    ]
    
    # Iterate over clients
    print("\nIterate over clients")
    for client in clients:
        print(client["name"], client["age"])
        # Output
        # Alice 25
        # Bob 30
        # Charlie 35
    
    # Iterate over products
    print("\nIterate over products")
    for product in products:
        print(product)
        # Output
        # apple
        # banana
        # cherry
    
    # Iterate over orders
    print("\nIterate over orders")
    for order in orders:
        print(order["client"], order["product"])
        # Output
        # Alice apple
        # Bob banana
        # Charlie cherry
    
    # Iterate over orders and products and update inventory
    print("\nIterate over orders and products and update inventory")
    for order in orders:
        for product in products:
            if order["product"] == product:
                inventory[product] -= 1
                print(order["client"], "ordered", order["product"])
                print("Inventory of", product, "is", inventory[product])
                # Output
                # Alice ordered apple
                # Inventory of apple is 9
                # Bob ordered banana
                # Inventory of banana is 19
                # Charlie ordered cherry
                # Inventory of cherry is 29
    # Note: The nested for loop iterates over the orders and products and updates
    # the inventory of the products.