Week 2


Week 2: Data Structures & Exception Handling

1. Lists, Tuples, Dictionaries, Sets

Lists

  1. Basic List Operations

    my_list = [1, 2, 3, 4, 5]
    my_list.append(6)  # Add an element
    my_list.remove(2)  # Remove an element
    print(my_list)  # Output: [1, 3, 4, 5, 6]
  2. List Indexing and Slicing

    numbers = [10, 20, 30, 40, 50]
    print(numbers[1])  # Output: 20
    print(numbers[-1])  # Output: 50
    print(numbers[1:4])  # Output: [20, 30, 40]

Tuples

  1. Tuple Basics

    my_tuple = (1, 2, 3, 4)
    print(my_tuple[1])  # Output: 2
  2. Immutable Nature of Tuples

    my_tuple = (10, 20, 30)
    # my_tuple[0] = 100  # This will raise an error since tuples are immutable

Dictionaries

  1. Creating and Accessing Dictionary Elements

  2. Adding and Removing Elements from Dictionary

Sets

  1. Creating and Using Sets

  2. Set Operations (Union, Intersection, Difference)


2. List & Dictionary Comprehensions

  1. List Comprehension Example

  2. Dictionary Comprehension Example


3. Exception Handling (try-except-finally)

  1. Basic Exception Handling

  2. Handling Multiple Exceptions

  3. Using finally Block

  4. Using else in Exception Handling

  5. Raising Custom Exceptions


4. File Handling (Reading/Writing Files, CSV, JSON)

  1. Writing to a File

  2. Reading from a File

  3. Reading a File Line by Line

  4. Writing and Reading CSV Files

  5. Writing and Reading JSON Files


Last updated