Lists are ordered, mutable (changeable), and allow duplicate elements.
They are defined using square brackets []. Items are separated by commas (,).
We can access elements by passing the position and can also change values.
| Method | Purpose | Example |
| append(x) | Adds an item to the end of the list | list.append(10) |
| insert(i, x) | Inserts an item at index i | list.insert(2, 50) |
| remove(x) | Removes the first occurrence of value x | list.remove(3) |
| pop(i) | Removes and returns item at index i (default last) | list.pop() |
| clear() | Removes all items from the list | list.clear() |
| sort() | Sorts the list in ascending order | list.sort() |
| reverse() | Reverses the list order | list.reverse() |
| count(x) | Returns how many times x appears | list.count(5) |
| index(x) | Returns the index of the first occurrence of x | list.index("apple") |
| extend(iterable) | Adds multiple items (joins lists) | list.extend([4,5]) |
| len() | Returns the number of elements | len(l) |
Tuples are ordered, immutable (unchangeable), and allow duplicate elements.
They are defined using parentheses ().
| Method | Purpose | Example |
| count(x) | Returns how many times value x appears in the tuple | t.count(2) |
| index(x) | Returns the index of the first occurrence of x | t.index("apple") |
| len() | Returns the number of elements | len(l) |
my_tuple = (1, 2, 3, 4, 5) print(my_tuple) /# Output: (1, 2, 3, 4, 5)
print(my_tuple[0]) /# Output: 1 (first element) print(my_tuple[-1]) /# Output: 5 (last element)
Tuples are immutable; you cannot modify elements.
/# my_tuple[1] = 10 /# This will raise a TypeError
person = ("Alice", 30, "Engineer") print(person) /# Output: ('Alice', 30, 'Engineer')
• Dictionaries are ordered, mutable, and use key-value pairs to store data.
• Keys are unique, and values can be of any data type.
• They are defined using curly braces {}.
| Method | Purpose | Example |
| get(key) | Returns the value of the given key (does not show error if key doesn’t exist) | d.get("name") |
| keys() | Returns all keys in the dictionary | d.keys() |
| values() | Returns all values in the dictionary | d.values() |
| items() | Returns all key-value pairs as tuples | d.items() |
| update(d2) | Updates dictionary with elements from another dictionary | d.update({"age": 21}) |
| pop(key) | Removes the item with the given key | d.pop("name") |
| popitem() | Removes the last inserted key-value pair | d.popitem() |
| clear() | Removes all items from the dictionary | d.clear() |
my_dict = {"name": "Alice", "age": 30, "city": "New York"} print(my_dict) /# Output: {'name': 'Alice', 'age': 30, 'city': 'New York'}
print(my_dict["name"]) /# Output: Alice
my_dict["age"] = 31 print(my_dict) /# Output: {'name': 'Alice', 'age': 31, 'city': 'New York'}
my_dict["occupation"] = "Engineer" print(my_dict) /# Output: {'name': 'Alice', 'age': 31, 'city': 'New York', 'occupation': 'Engineer'}
del my_dict["city"] print(my_dict) /# Output: {'name': 'Alice', 'age': 31, 'occupation': 'Engineer'}
A set is a collection of unique elements (no duplicates).
It is unordered, meaning the items do not have a fixed position.
Sets are mutable, so we can add or remove elements.
| Method | Purpose | Example |
| add(x) | Adds an element | s.add(10) |
| remove(x) | Removes x (error if not found) | s.remove(3) |
| discard(x) | Removes x (no error if not found) | s.discard(3) |
| clear() | Removes all items | s.clear() |
| union(s2) | Combines sets | s1.union(s2) |
| intersection(s2) | Common elements | s1.intersection(s2) |
Immutable version of a set (cannot be changed after creation).
No add(), remove(), etc.
Supports only reading operations like union() and intersection.
fs = frozenset({1, 2, 3})
| Feature | List | Tuple | Set | Dictionary |
| Definition | Ordered collection of items | Ordered collection of items | Unordered collection of unique items | Collection of key–value pairs |
| Syntax | [] | () | {} | {key: value} |
| Mutability | Mutable (can change values) | Immutable (cannot change values) | Mutable, but elements must be unique | Mutable (keys are unique) |
| Allows | Yes | Yes | No | Keys: No, Values: Yes |
| Indexing | Supported | Supported (fixed data that shouldn’t change) | Not supported | Accessed using key |
| Best Used | General purpose data storage | Supported (fixed data that shouldn’t change) | Removing duplicates, math set operations | Data mapped as key → value pairs |
The input() function is used to take input from the user. It reads a line from input, converts it into a string, and returns it.
name = input("Enter your name: ") print(f"Hello, {name}!")
The print() function is used to display output to the console. It can take multiple arguments, which are printed with a space between them by default.
name = "Alice" age = 30 print("Name:", name, "Age:", age)
Control flow statements in Python allow you to manage the execution of code based on certain conditions or repeatedly execute code blocks.
Executes a block of code if a specified condition is true. Example:
age = 18 if age >= 18: print("You are an adult.")
Executes one block of code if the condition is true, and another block if it is false. Example:
age = 16 if age >= 18: print("You are an adult.") else: print("You are not an adult.")
Allows you to check multiple conditions. If the first condition is false, it checks the next one, and so on. Example:
age = 20 if age < 13: print("You are a child.") elif age < 20: print("You are a teenager.") else: print("You are an adult.")