**Python Fundamentals: Data Structures & Control Flow

In this lesson, you'll build upon your Python knowledge by exploring fundamental data structures and control flow. You'll learn how to store and organize data using lists, tuples, dictionaries, and sets, and then use control flow statements like `if`, `elif`, `else`, `for`, and `while` to make your programs dynamic and perform actions based on conditions.

Learning Objectives

  • Define and differentiate between Python's core data structures: lists, tuples, dictionaries, and sets.
  • Use `if`, `elif`, and `else` statements to control program flow based on conditions.
  • Implement `for` and `while` loops for iterating over data and repeating tasks.
  • Apply these concepts to solve simple programming problems involving data manipulation and decision-making.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Data Structures

Data structures are fundamental building blocks for organizing and storing data in Python. Choosing the right data structure is crucial for efficient programming. We will cover lists, tuples, dictionaries, and sets.

  • Lists: Ordered, mutable (changeable) collections of items. Created using square brackets []. They can contain mixed data types.
    python my_list = [1, "apple", 3.14, True] my_list[0] # Accessing the first element (index 0) my_list.append("banana") # Adding an element to the end

  • Tuples: Ordered, immutable (unchangeable) collections of items. Created using parentheses (). Often used for data that shouldn't be altered.
    python my_tuple = (1, "apple", 3.14) # my_tuple[0] = 2 # This would raise an error because tuples are immutable

  • Dictionaries: Unordered collections of key-value pairs. Created using curly braces {}. Keys must be unique and immutable, values can be any data type.
    python my_dict = {"name": "Alice", "age": 30, "city": "New York"} my_dict["name"] # Accessing the value associated with the key "name" my_dict["occupation"] = "Data Scientist" # Adding a new key-value pair

  • Sets: Unordered collections of unique items. Created using curly braces {} or the set() constructor. Useful for removing duplicates and performing mathematical set operations.
    python my_set = {1, 2, 3, 3, 4} # Duplicate 3 is automatically removed my_set # Output: {1, 2, 3, 4} my_set.add(5)

Control Flow: Conditional Statements

Control flow statements allow your program to make decisions and execute different code blocks based on conditions. The most common are if, elif (else if), and else.

# Example: Check if a number is positive, negative, or zero
number = 10

if number > 0:
    print("Positive")
elif number < 0:
    print("Negative")
else:
    print("Zero")
  • The if statement checks a condition. If the condition is True, the code block indented below it is executed.
  • The elif statement is used to check additional conditions if the previous if or elif conditions were False.
  • The else statement is executed if none of the preceding if or elif conditions are True.

Control Flow: Loops

Loops allow you to repeat a block of code multiple times. Python has two main types of loops: for and while.

  • for Loops: Used to iterate over a sequence (e.g., a list, tuple, string, or range).
    ```python
    # Example: Print each item in a list
    my_list = ["apple", "banana", "cherry"]
    for fruit in my_list:
    print(fruit)

    Example: Using range()

    for i in range(5): # Iterates from 0 to 4
    print(i)
    ```

  • while Loops: Used to repeat a block of code as long as a condition is True.
    python # Example: Print numbers from 1 to 5 count = 1 while count <= 5: print(count) count += 1 # Important: Increment the counter to avoid an infinite loop!

Progress
0%