what are the main control structures
sequence (code runs in order)
selection (if, elif, else)
iteration (for/while loops)
how to write an if…elif…else control structure
age = 17
if age < 13:
print(“Child”)
elif age < 18:
print(“Teenager”)
else:
print(“Adult”)
what’s the difference between a global and local variable?
global: declared outside functions, accessible everywhere
local: declared inside a function, accessible only within that function
what are some examples of simple and structured data types
simple: int, float, bool, str
structured: list, tuple, dict, set
how to retrieve information from a dictionary
print(person[“name”]) # Output: Liam
print(person.get(“grade”, “Not recorded”)) # Output: Not recorded
for key in person:
print(key, “→”, person[key])
for key, value in person.items():
print(f”{key}: {value}”)
define a basic python class with attributes and a method
class Car:
def __init__(self, brand, year):
self.brand = brand
self.year = year
def drive(self):
print(f"The {self.brand} is driving.")my_car = Car(“Toyota”, 2022)
my_car.drive()
what is self in a python class
self refers to the instance of the class
- it is used to access attributes and methods of the object
what is a function and why is it used
a reusable block of code that performs a task
- improves code organisation, reuse and testing
write python code to read a file line by line
with open(“data.txt”, “r”) as file:
for line in file:
print(line.strip())
write code to append text to a file
with open(“log.txt”, “a”) as file:
file.write(“New log entry\n”)
what are libraries and modules, and their difference?