Module 3

Python Basics

Python is one of the easiest languages to read and one of the most powerful to use. Let’s get coding.

1. Getting set up

To write and run Python on your own computer:

  1. Go to python.org and download the latest version of Python.
  2. Run the installer. On Windows, tick “Add Python to PATH” before clicking Install.
  3. Open IDLE (comes with Python) or install VS Code for a nicer editor.
  4. Create a new file, save it as hello.py, and you’re ready to code.

2. Printing output

The print() function shows something on the screen.

print("Hello, world!")
print("My name is Alex.")

Output

Hello, world!
My name is Alex.

3. Variables

Store information with an = sign. The name comes first.

name = "Priya"
age = 12
print(name, "is", age)

Output

Priya is 12

4. Asking for input

Use input() to ask the user a question. Whatever they type is stored as text.

name = input("What is your name? ")
print("Hi,", name+"!")
Heads up: input() always gives you a string. To do maths with it, convert to a number with int() or float().
age = int(input("How old are you? "))
print("Next year you will be", age + 1)

5. Doing maths

SymbolMeaningExampleResult
+Add5 + 27
-Subtract5 - 23
*Multiply5 * 210
/Divide5 / 22.5
//Whole divide5 // 22
%Remainder5 % 21
**Power5 ** 225

6. If / else

age = int(input("Your age? "))

if age >= 13:
    print("You are a teenager.")
elif age >= 5:
    print("You are at school.")
else:
    print("You are little!")
Indentation matters. Python uses spaces (usually 4) to show which lines belong inside an if, for, or def.

7. Loops

For loops repeat a fixed number of times:

for i in range(3):
    print("Hip hip hooray!")

While loops repeat as long as something is true:

lives = 3
while lives > 0:
    print("Still alive!")
    lives = lives - 1

8. Functions

A function is a reusable block of code. Define it once with def, then call it whenever.

def greet(name):
    print("Hello,", name+"!")

greet("Leo")
greet("Mia")

Output

Hello, Leo!
Hello, Mia!

Challenges

Try these in your Python editor. Start simple, then add features.

  1. Greeter: Ask for a name and say hello back.
  2. Age checker: Ask for an age and print whether they can watch a PG-13 film.
  3. Times table: Ask for a number and print its times table up to 12.
  4. Guessing game: Pick a secret number 1–10. Keep asking the user to guess until they get it right.