Chapter 3: Python Data Types Exercises

Hands-on exercises to practice working with Python data structures

Chapter 3 Concepts

This chapter covers the essential data structures in Python programming:

Lists Tuples Dictionaries Sets Strings Numbers
Exercise 1

List Manipulation

Create a program that demonstrates various list operations.

# Start with this list
fruits = ["apple", "banana", "cherry", "date"]

# Perform these operations:
# 1. Add "elderberry" to the end
# 2. Insert "blueberry" at position 2
# 3. Remove "banana"
# 4. Reverse the list
# 5. Sort the list alphabetically
  • Use append() to add an item
  • Use insert() to add at a specific position
  • Use remove() to delete an item
  • Use reverse() to reverse the list
  • Use sort() to sort the list
  • Print the list after each operation
  • Exercise 2

    Tuple Operations

    Explore the properties of tuples and practice basic operations.

    # Create a tuple
    colors = ("red", "green", "blue", "yellow", "red")

    # Perform these operations:
    # 1. Count how many times "red" appears
    # 2. Find the index of "blue"
    # 3. Try to change "green" to "orange" (observe the error)
    # 4. Create a new tuple by concatenating with ("purple", "pink")
  • Use count() to count occurrences
  • Use index() to find position of an element
  • Try to modify an element (will raise error)
  • Create a new tuple using + operator
  • Print the results
  • Hint

    Tuples are immutable, so you cannot change their elements once created.

    Exercise 3

    Dictionary Operations

    Create and manipulate a dictionary to store student information.

    # Create a student dictionary
    student = {
        "name": "John Doe",
        "age": 20,
        "major": "Computer Science",
        "gpa": 3.8
    }

    # Perform these operations:
    # 1. Add a new key "courses" with value ["Python", "Algorithms"]
    # 2. Update the "age" to 21
    # 3. Remove the "gpa" key
    # 4. Check if "major" exists in the dictionary
  • Add a new key-value pair
  • Update an existing value
  • Remove a key-value pair
  • Check for key existence
  • Print all keys and values
  • Exercise 4

    Set Operations

    Practice working with sets and their mathematical operations.

    # Create two sets
    A = {1, 2, 3, 4, 5}
    B = {4, 5, 6, 7, 8}

    # Perform these operations:
    # 1. Union of A and B
    # 2. Intersection of A and B
    # 3. Difference between A and B
    # 4. Symmetric difference between A and B
    # 5. Check if A is a subset of B
  • Use union() or | operator
  • Use intersection() or & operator
  • Use difference() or - operator
  • Use symmetric_difference() or ^ operator
  • Use issubset() method
  • Print the results
  • Exercise 5

    String Methods

    Practice various string methods and operations.

    # Start with this string
    text = " Python Programming is Amazing! "

    # Perform these operations:
    # 1. Remove leading/trailing whitespace
    # 2. Convert to uppercase
    # 3. Replace "Amazing" with "Fantastic"
    # 4. Split into words
    # 5. Check if the string starts with "Python"
    # 6. Find the position of "Programming"
  • Use strip() to remove whitespace
  • Use upper() to convert to uppercase
  • Use replace() to change text
  • Use split() to create a word list
  • Use startswith() to check prefix
  • Use find() to locate a substring
  • Exercise 6

    Number Operations

    Practice working with different numeric types and operations.

    # Work with different number types
    integer_num = 15
    float_num = 7.5
    complex_num = 2 + 3j

    # Perform these operations:
    # 1. Convert integer to float and vice versa
    # 2. Perform arithmetic operations
    # 3. Use math module functions (sqrt, pow)
    # 4. Use built-in functions (abs, round)
    # 5. Work with complex numbers
  • Convert between int and float
  • Try different arithmetic operations
  • Import and use math module functions
  • Use abs() and round() functions
  • Perform operations with complex numbers
  • Hint

    Remember to import the math module: import math

    Exercise 7

    Type Conversion

    Practice converting between different data types.

    # Start with these values
    num_str = "123"
    float_str = "45.67"
    num_list = [1, 2, 3, 4, 5]
    tuple_data = (10, 20, 30)

    # Perform these conversions:
    # 1. Convert string to integer
    # 2. Convert string to float
    # 3. Convert list to