Python Revision Sheet
1. Lists - Revisiting Python's Workhorse
- Ordered, mutable collections
- Can hold mixed data types
Key Operations:
my_list = [1, 2, 3, "hello", [4, 5]]
print(my_list[0]) # Access
my_list[1] = "two" # Modify
my_list.append("new") # Add
my_list.remove("hello")# Remove
squares = [x**2 for x in range(5)] # Comprehension
2. Dictionaries - Key-Value Power
- Unordered, keys must be immutable
Key Operations:
my_dict = {"name": "Alice", "age": 30}
my_dict["age"] = 31
my_dict.get("age", "Not found")
del my_dict["name"]
for k, v in my_dict.items(): print(k, v)
3. Files - Reading & Writing
Use 'with open(...) as f:' to read/write files.
Writing:
with open("file.txt", "w") as f:
f.write("Hello")
Python Revision Sheet
Reading:
with open("file.txt", "r") as f:
content = f.read()
4. OOP - Python Classes & Objects
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, I'm {self.name}")
Inheritance:
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade
5. Strings - Useful Functions
Common string methods:
s = " Hello World "
s.strip() # Remove surrounding whitespace
s.lower() # "hello world"
s.upper() # "HELLO WORLD"
s.replace("H", "J") # " Jello World "
s.find("World") # Index of substring
s.split() # ["Hello", "World"]
Python Revision Sheet
",".join(["a", "b"]) # "a,b"