Summary Notes

Lists in Python

Lists are mutable, ordered sequences that can store multiple items of different data types. They are created using square brackets [] and are one of the most versatile data structures in Python.

List Characteristics:

Common List Methods:

List Operations:

Tuples in Python

Tuples are immutable, ordered sequences similar to lists but cannot be modified after creation. They are created using parentheses () or just comma-separated values.

Tuple Characteristics:

When to use tuples:

Tuple Methods:

Dictionaries in Python

Dictionaries are mutable, unordered collections of key-value pairs. They provide fast lookups and are created using curly braces {} or the dict() constructor.

Dictionary Characteristics:

Dictionary Methods:

Dictionary Comprehensions: Create dictionaries using comprehension syntax: {key_expr: value_expr for item in iterable}

File Handling in Python

Python provides built-in functions for reading from and writing to files. File handling is essential for data persistence and working with external data sources.

File Opening Modes:

File Methods:

Best Practices:

Modules and Packages

Modules are files containing Python code that can be imported and reused. Packages are directories containing multiple modules.

Importing Modules:

Standard Library Modules:

Creating Custom Modules:

Packages: Directories with __init__.py file containing multiple modules.

Q&A

What is the difference between list and tuple?
Lists are mutable (can be changed), tuples are immutable (cannot be changed after creation). Lists use [], tuples use ().
How to add an item to a list?
Use append() to add to end, or insert(index, item) to add at specific position.
How to read a file in Python?
Use with open('filename', 'r') as f: content = f.read() - the 'with' statement automatically closes the file.
What is a module?
A file containing Python code that can be imported and reused in other programs.
How do you access dictionary values?
Use square brackets: dict['key'] or the get() method: dict.get('key', default).
What is the difference between 'w' and 'a' file modes?
'w' overwrites the file, 'a' appends to existing content.
Can tuples be modified?
No, tuples are immutable. You cannot add, remove, or change elements after creation.
What are dictionary comprehensions?
Concise way to create dictionaries: {key_expr: value_expr for item in iterable if condition}.
How do you handle file errors?
Use try-except blocks: try: open file except FileNotFoundError: handle error.
What is the __name__ variable in modules?
It contains the module's name. When run directly, it's "__main__", when imported, it's the module name.
How to sort a list in descending order?
Use list.sort(reverse=True) or sorted(list, reverse=True).
What is the difference between pop() and remove() for lists?
pop(index) removes by index and returns the value, remove(value) removes by value.
How to check if a key exists in a dictionary?
Use 'key' in dict or dict.get('key') is not None.
What are the benefits of using tuples over lists?
Tuples are immutable (safer), faster for iteration, can be used as dictionary keys, and use less memory.
How do you read a file line by line?
Use for line in file: or file.readlines() to get all lines as list.
What is a package in Python?
A directory containing multiple modules and an __init__.py file.
How to merge two dictionaries?
Use dict1.update(dict2) or {**dict1, **dict2} (Python 3.5+).
What is the purpose of the 'with' statement for files?
It ensures files are properly closed even if an exception occurs.
How to get all keys from a dictionary?
Use dict.keys() which returns a dict_keys view object.
Can lists contain different data types?
Yes, lists can contain elements of different types: [1, "hello", True, 3.14].

Fill in the Blanks

  1. ________ are mutable sequences in Python. (Lists)
  2. ________ are immutable and created with parentheses. (Tuples)
  3. Dictionaries store data as ________ pairs. (key-value)
  4. To open a file for reading, use mode ________. ('r')
  5. The ________ statement ensures files are properly closed. (with)
  6. The ________ method adds an item to the end of a list. (append)
  7. ________ cannot be used as dictionary keys. (Lists)
  8. The ________ method returns all dictionary keys. (keys)
  9. ________ mode overwrites existing file content. ('w')
  10. The ________ module provides mathematical functions. (math)
  11. ________ are faster than lists for read-only operations. (Tuples)
  12. The ________ method removes and returns the last item from a list. (pop)
  13. ________ can contain elements of different data types. (Lists)
  14. The ________ function imports a module. (import)
  15. ________ are used to group related modules. (Packages)
  16. The ________ method sorts a list in place. (sort)
  17. ________ provide fast lookups using keys. (Dictionaries)
  18. The ________ method reads all lines from a file. (readlines)
  19. ________ are hashable and can be dictionary keys. (Tuples)
  20. The ________ statement is used for automatic file handling. (with)

True/False

  1. Lists can be modified after creation. (True)
  2. Tuples use square brackets. (False)
  3. Dictionary keys must be unique. (True)
  4. Files opened with 'w' mode append to existing content. (False)
  5. Modules must be installed separately. (False)
  6. Tuples are faster than lists for iteration. (True)
  7. Dictionaries maintain insertion order in Python 3.7+. (True)
  8. The 'with' statement automatically closes files. (True)
  9. Lists can be used as dictionary keys. (False)
  10. The math module is part of Python's standard library. (True)
  11. Tuples can contain duplicate values. (True)
  12. The pop() method removes the first element. (False)
  13. Dictionaries allow duplicate keys. (False)
  14. File mode 'a' creates a new file if it doesn't exist. (True)
  15. Modules can contain functions and classes. (True)
  16. Lists are immutable. (False)
  17. The get() method raises KeyError for missing keys. (False)
  18. Packages are directories with __init__.py. (True)
  19. Tuples use less memory than lists. (True)
  20. The readline() method reads the entire file. (False)

Multiple Choice Questions

  1. Which method adds an item to the end of a list?
    a) insert()
    b) append()
    c) add()
    d) push()
    Answer: b) append()
  2. What is the correct way to create a tuple?
    a) [1, 2, 3]
    b) (1, 2, 3)
    c) {1, 2, 3}
    d) 1, 2, 3
    Answer: b) (1, 2, 3)
  3. How to access a dictionary value?
    a) dict.key
    b) dict['key']
    c) dict(key)
    d) dict::key
    Answer: b) dict['key']
  4. Which file mode is used to append to an existing file?
    a) 'r'
    b) 'w'
    c) 'a'
    d) 'x'
    Answer: c) 'a'
  5. What does the list.pop() method do?
    a) Adds an item
    b) Removes last item and returns it
    c) Removes first item
    d) Clears the list
    Answer: b) Removes last item and returns it
  6. Which of these can be used as dictionary keys?
    a) Lists
    b) Tuples
    c) Dictionaries
    d) Sets
    Answer: b) Tuples
  7. How to import a specific function from a module?
    a) import module.function
    b) from module import function
    c) import function from module
    d) using module.function
    Answer: b) from module import function
  8. What is the output of len([1, 2, 3, 4])?
    a) 3
    b) 4
    c) 5
    d) Error
    Answer: b) 4
  9. Which method returns all dictionary values?
    a) keys()
    b) values()
    c) items()
    d) all()
    Answer: b) values()
  10. What does 'with' statement do for files?
    a) Opens file in write mode
    b) Automatically closes file
    c) Reads entire file
    d) Creates new file
    Answer: b) Automatically closes file
  11. How to check if key exists in dictionary?
    a) key in dict
    b) dict.has_key(key)
    c) dict.contains(key)
    d) dict.exists(key)
    Answer: a) key in dict
  12. Which is faster for read-only operations?
    a) Lists
    b) Tuples
    c) Both same
    d) Depends on size
    Answer: b) Tuples
  13. What does list.sort() return?
    a) New sorted list
    b) None
    c) Sorted list
    d) Error
    Answer: b) None
  14. Which file method reads one line at a time?
    a) read()
    b) readline()
    c) readlines()
    d) readall()
    Answer: b) readline()
  15. What is a Python package?
    a) A single .py file
    b) Directory with __init__.py
    c) Built-in module
    d) Function collection
    Answer: b) Directory with __init__.py

Application Based Questions

1. Create a program that reads a text file and counts the number of words.

Solution:

def count_words(filename):
    try:
        with open(filename, 'r') as f:
            content = f.read()
            words = content.split()
            return len(words)
    except FileNotFoundError:
        return "File not found"

word_count = count_words('sample.txt')
print(f"Number of words: {word_count}")

2. Write a program to create a student database using dictionaries.

Solution:

students = {}

def add_student(roll_no, name, grade):
    students[roll_no] = {'name': name, 'grade': grade}

def get_student(roll_no):
    return students.get(roll_no, "Student not found")

add_student(101, "Alice", "A")
add_student(102, "Bob", "B")
print(get_student(101))  # {'name': 'Alice', 'grade': 'A'}

3. Create a function that merges two lists and removes duplicates.

Solution:

def merge_lists_unique(list1, list2):
    merged = list1 + list2
    return list(set(merged))

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
result = merge_lists_unique(list1, list2)
print(result)  # [1, 2, 3, 4, 5, 6] (order may vary)

4. Write a program to read a CSV file and find the average of numbers in a column.

Solution:

def calculate_average(filename, column_index):
    numbers = []
    with open(filename, 'r') as f:
        for line in f:
            parts = line.strip().split(',')
            if len(parts) > column_index:
                try:
                    numbers.append(float(parts[column_index]))
                except ValueError:
                    continue
    return sum(numbers) / len(numbers) if numbers else 0

# Assuming CSV format: name,age,score
avg_score = calculate_average('data.csv', 2)
print(f"Average score: {avg_score}")

5. Create a tuple-based phonebook that stores names and numbers.

Solution:

phonebook = (
    ("Alice", "123-456-7890"),
    ("Bob", "987-654-3210"),
    ("Charlie", "555-123-4567")
)

def find_number(name):
    for contact in phonebook:
        if contact[0] == name:
            return contact[1]
    return "Not found"

print(find_number("Alice"))  # 123-456-7890

6. Write a program that uses the math module to calculate compound interest.

Solution:

import math

def compound_interest(principal, rate, time, compounds_per_year):
    amount = principal * math.pow(1 + rate/compounds_per_year, compounds_per_year * time)
    return amount

principal = 1000
rate = 0.05  # 5%
time = 2  # years
compounds = 12  # monthly

final_amount = compound_interest(principal, rate, time, compounds)
print(f"Final amount: ${final_amount:.2f}")

7. Create a dictionary comprehension to count word frequencies.

Solution:

def word_frequency(text):
    words = text.lower().split()
    return {word: words.count(word) for word in set(words)}

text = "the quick brown fox jumps over the lazy dog"
freq = word_frequency(text)
print(freq)  # {'the': 2, 'quick': 1, 'brown': 1, ...}

8. Write a program to backup a file with timestamp using os and datetime modules.

Solution:

import os
import datetime

def backup_file(filename):
    if os.path.exists(filename):
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        backup_name = f"{filename}_{timestamp}.bak"
        with open(filename, 'r') as original:
            with open(backup_name, 'w') as backup:
                backup.write(original.read())
        return f"Backup created: {backup_name}"
    return "File not found"

result = backup_file('important.txt')
print(result)

Examples

Lists - Basic Operations

# Creating lists
fruits = ["apple", "banana", "orange"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", True, 3.14]

# Adding elements
fruits.append("grape")        # Add to end
fruits.insert(1, "mango")     # Insert at index 1
print(fruits)                  # ['apple', 'mango', 'banana', 'orange', 'grape']

# Removing elements
fruits.remove("banana")       # Remove by value
last_fruit = fruits.pop()     # Remove and return last item
print(last_fruit)             # 'grape'

# Accessing elements
print(fruits[0])              # First element: 'apple'
print(fruits[-1])             # Last element: 'orange'
print(fruits[1:3])            # Slice: ['mango', 'orange']

# List methods
numbers.sort()                # Sort in place
numbers.reverse()             # Reverse in place
print(numbers)                # [5, 4, 3, 2, 1]
print(len(numbers))           # Length: 5
print(numbers.count(3))       # Count occurrences: 1

Lists - Advanced Operations

# List comprehensions
squares = [x**2 for x in range(1, 6)]
print(squares)                # [1, 4, 9, 16, 25]

even_numbers = [x for x in range(10) if x % 2 == 0]
print(even_numbers)           # [0, 2, 4, 6, 8]

# Matrix operations
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2])           # Access element: 6

# List concatenation and repetition
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2      # [1, 2, 3, 4, 5, 6]
repeated = list1 * 3          # [1, 2, 3, 1, 2, 3, 1, 2, 3]

# Membership testing
print(2 in list1)             # True
print(6 not in list1)         # True

Tuples - Basic Operations

# Creating tuples
point = (3, 4)
colors = ("red", "green", "blue")
single_item = (42,)          # Note the comma for single item
no_parentheses = 1, 2, 3      # Also creates a tuple

# Accessing elements
print(point[0])              # 3
print(colors[-1])            # 'blue'
print(point[1:])             # (4,)

# Tuple unpacking
x, y = point
print(f"x={x}, y={y}")       # x=3, y=4

# Tuple methods
numbers = (1, 2, 3, 2, 4, 2)
print(numbers.count(2))      # 3
print(numbers.index(3))      # 2

# Immutable nature
# numbers[0] = 10  # This would raise TypeError

Tuples - Practical Uses

# Returning multiple values from function
def get_coordinates():
    return 10, 20

x, y = get_coordinates()
print(f"Coordinates: ({x}, {y})")

# Using tuples as dictionary keys
locations = {}
locations[(40.7128, -74.0060)] = "New York"
locations[(34.0522, -118.2437)] = "Los Angeles"

print(locations[(40.7128, -74.0060)])  # New York

# String formatting
person = ("Alice", 25, "Engineer")
print("Name: %s, Age: %d, Job: %s" % person)

# Swapping values
a, b = 5, 10
a, b = b, a
print(f"a={a}, b={b}")       # a=10, b=5

Dictionaries - Basic Operations

# Creating dictionaries
student = {
    "name": "Alice",
    "age": 20,
    "grade": "A"
}

# Alternative creation
empty_dict = {}
another_dict = dict(name="Bob", age=22)

# Accessing values
print(student["name"])       # Alice
print(student.get("age"))    # 20
print(student.get("major", "Not specified"))  # Not specified

# Adding/modifying values
student["major"] = "CS"
student["age"] = 21

# Removing values
removed_grade = student.pop("grade")
del student["major"]

print(student)               # {'name': 'Alice', 'age': 21}

Dictionaries - Advanced Operations

# Dictionary methods
student = {"name": "Alice", "age": 20, "courses": ["Math", "Science"]}

print(student.keys())        # dict_keys(['name', 'age', 'courses'])
print(student.values())      # dict_values(['Alice', 20, ['Math', 'Science']])
print(student.items())       # dict_items([('name', 'Alice'), ('age', 20), ...])

# Dictionary comprehensions
squares = {x: x**2 for x in range(1, 6)}
print(squares)               # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

# Merging dictionaries
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
merged = {**dict1, **dict2}
print(merged)                # {'a': 1, 'b': 2, 'c': 3, 'd': 4}

# Checking key existence
print("name" in student)     # True
print("grade" not in student) # True

File Handling - Basic Operations

# Writing to a file
with open('example.txt', 'w') as f:
    f.write("Hello, World!\n")
    f.write("This is a test file.\n")

# Reading from a file
with open('example.txt', 'r') as f:
    content = f.read()
    print(content)

# Reading line by line
with open('example.txt', 'r') as f:
    for line in f:
        print(line.strip())

# Appending to a file
with open('example.txt', 'a') as f:
    f.write("This line was appended.\n")

# Reading all lines into a list
with open('example.txt', 'r') as f:
    lines = f.readlines()
    print(f"Number of lines: {len(lines)}")

File Handling - Advanced Operations

# Working with binary files
with open('data.bin', 'wb') as f:
    f.write(b'Binary data')

with open('data.bin', 'rb') as f:
    data = f.read()
    print(data)

# File pointer operations
with open('example.txt', 'r') as f:
    print(f.tell())          # Current position: 0
    first_line = f.readline()
    print(f.tell())          # Position after reading first line
    f.seek(0)                # Go back to beginning
    print(f.tell())          # Back to 0

# Error handling
try:
    with open('nonexistent.txt', 'r') as f:
        content = f.read()
except FileNotFoundError:
    print("File not found")
except IOError:
    print("IO Error occurred")

Modules - Importing and Using

# Importing entire modules
import math
print(math.sqrt(16))         # 4.0
print(math.pi)               # 3.141592653589793

import random
print(random.randint(1, 10)) # Random number between 1-10

# Importing specific functions
from math import sqrt, pi
print(sqrt(25))              # 5.0
print(pi)                    # 3.141592653589793

# Importing with alias
import math as m
print(m.cos(0))              # 1.0

# Using datetime module
import datetime
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))

# Using os module
import os
print(os.getcwd())           # Current working directory
print(os.path.exists('example.txt'))  # Check if file exists

Modules - Creating Custom Modules

# Save this as mymodule.py
def greet(name):
    return f"Hello, {name}!"

def add(a, b):
    return a + b

PI = 3.14159

if __name__ == "__main__":
    print("This module is being run directly")
    print(greet("World"))

# Then import and use
import mymodule
print(mymodule.greet("Alice"))  # Hello, Alice!
print(mymodule.add(5, 3))       # 8
print(mymodule.PI)              # 3.14159

Practical Examples

# Student grade management
students = {}

def add_student(name, grades):
    students[name] = grades

def get_average(name):
    if name in students:
        return sum(students[name]) / len(students[name])
    return None

add_student("Alice", [85, 90, 88])
add_student("Bob", [78, 82, 80])
print(f"Alice's average: {get_average('Alice'):.2f}")

# Simple contact book
contacts = {}

def add_contact(name, phone):
    contacts[name] = phone

def find_contact(name):
    return contacts.get(name, "Contact not found")

add_contact("John", "555-1234")
add_contact("Jane", "555-5678")
print(find_contact("John"))   # 555-1234

# File word counter
def count_words_in_file(filename):
    word_count = {}
    with open(filename, 'r') as f:
        for line in f:
            words = line.lower().split()
            for word in words:
                word = word.strip('.,!?')
                word_count[word] = word_count.get(word, 0) + 1
    return word_count

# Usage: word_counts = count_words_in_file('text.txt')