Defining a function
def square(x):
return x ** 2
print(square(5))
def starts a function definition; return sends a value back to the caller. A function with no return statement returns None.
Default arguments
def greet(name, greeting="Hello"):
return greeting + ", " + name + "!"
print(greet("Ada"))
print(greet("Ada", "Welcome"))
Lists
A list is an ordered, changeable collection, written in square brackets:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple -- indexing starts at 0
print(fruits[-1]) # cherry -- -1 is the last element
print(len(fruits)) # 3
fruits.append("date") # adds to the end
Looping over a list
for fruit in fruits:
print(fruit)
Why This Matters for Your Exams
Functions let you write a calculation once and reuse it everywhere -- the same principle behind not repeating yourself in any language. Get comfortable with return vs print: a function that prints its answer instead of returning it can't have that answer used in further calculations.
Try it yourself
lock
Real-time practice is a paid add-on
Reading units and the practice quiz are free. Pay once to unlock running real code for PYTHON.

