Python Lecture Notes (Final Expanded)
Lecture 3: Lists and Tuples
- **Lists**: Ordered, mutable collections that can store different data types.
- Access with indices, slicing works like strings.
List Methods (with details)
- **append(x)** → Adds a single element `x` at the end of the list.
Example: `[1,2].append(3)` → `[1,2,3]`.
- **insert(i, x)** → Inserts element `x` at index `i` (shifts existing items right).
Example: `[1,2].insert(1,5)` → `[1,5,2]`.
- **sort(reverse=False)** → Sorts the list in ascending order by default; `reverse=True` sorts
descending.
- **reverse()** → Reverses the order of elements *in place* (does not sort).
- **remove(x)** → Removes the *first occurrence* of value `x` in the list. Error if `x` not found.
- **pop(i=-1)** → Removes and returns the element at index `i` (last element by default).
Example: `[1,2,3].pop()` → returns `3`, list becomes `[1,2]`.
- **Tuples**: Ordered, immutable collections.
- Defined with `()`, cannot be changed after creation.
Tuple Methods (with details)
- **count(x)** → Returns how many times value `x` occurs in the tuple.
Example: `(1,2,1).count(1)` → `2`.
- **index(x)** → Returns the first index where value `x` occurs. Error if not found.
Example: `(1,2,3).index(2)` → `1`.
**Practice:**
- Check if a list is a palindrome.
- Store favorite movies in a list.
- Count how many students got grade "A" from a tuple.
Lecture 4: Dictionaries and Sets
- **Dictionaries**: Store data in key-value pairs.
- Mutable, unordered, unique keys only.
- Example: `student = {"name": "Arjun", "marks": 85}`
Dictionary Methods (with details)
- **keys()** → Returns all keys as a `dict_keys` object (can be converted to list).
Example: `student.keys()` → `["name","marks"]`.
- **values()** → Returns all values as a `dict_values` object.
Example: `student.values()` → `["Arjun",85]`.
- **items()** → Returns all key-value pairs as tuples inside a list-like object.
Example: `student.items()` → `[("name","Arjun"),("marks",85)]`.
- **get(key, default=None)** → Returns value for key if present, otherwise returns `default` (avoids error
unlike `dict[key]`).
- **update(newDict)** → Adds/overwrites key-value pairs from another dictionary.
Example: `student.update({"city":"Delhi"})`.
- **Sets**: Unordered collections of unique, immutable items.
- Defined with `{}` or `set()`.
Set Methods (with details)
- **add(x)** → Adds element `x` to the set (ignored if already present).
- **remove(x)** → Removes element `x`. Error if not present.
- **discard(x)** → Removes element `x` if present, does nothing if not.
- **pop()** → Removes and returns an arbitrary element (since sets are unordered).
- **clear()** → Removes all elements from the set.
- **union(set2)** → Returns a new set with all unique elements from both sets.
- **intersection(set2)** → Returns a new set with only elements common to both sets.
- **difference(set2)** → Returns elements present in the first set but not the second.
- **issubset(set2)** → Returns True if all elements of the set are contained in `set2`.
- **issuperset(set2)** → Returns True if the set contains all elements of `set2`.
**Practice:**
- Create a dictionary of word meanings.
- Count required classrooms for subjects using sets.
- Store `9` and `9.0` separately in a set.
Lecture 5: Loops
- **While Loops**: Repeat until condition is false.
- **For Loops**: Iterate over sequences (lists, tuples, strings).
- Can use `else` with loops (skips if `break` is used).
- **Control statements**:
- `break` → exits loop.
- `continue` → skips current iteration.
- `pass` → does nothing (placeholder).
- **Range function**: `range(start, stop, step)` generates sequences.
**Practice:**
- Print numbers 1–100 and 100–1.
- Multiplication table of a number.
- Search for a number in a tuple.
- Factorial and sum of first `n` numbers.
Lecture 6: Functions
- **Functions**: Block of reusable code.
- Defined with `def func_name(params):`.
- Called with `func_name(args)`.
- **Types**:
- Built-in (`print()`, `len()`, `type()`, `range()`).
- User-defined.
- **Parameters**: Can have default values.
- **Recursion**: A function calling itself.
- Must include a base case.
**Practice:**
- Function to print list elements.
- Function for factorial.
- Convert USD to another currency.
- Recursive sum of first `n` numbers.
- Recursive print of list elements.