1. Getting set up
To write and run Python on your own computer:
- Go to python.org and download the latest version of Python.
- Run the installer. On Windows, tick “Add Python to PATH” before clicking Install.
- Open IDLE (comes with Python) or install VS Code for a nicer editor.
- 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
| Symbol | Meaning | Example | Result |
|---|---|---|---|
+ | Add | 5 + 2 | 7 |
- | Subtract | 5 - 2 | 3 |
* | Multiply | 5 * 2 | 10 |
/ | Divide | 5 / 2 | 2.5 |
// | Whole divide | 5 // 2 | 2 |
% | Remainder | 5 % 2 | 1 |
** | Power | 5 ** 2 | 25 |
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.
- Greeter: Ask for a name and say hello back.
- Age checker: Ask for an age and print whether they can watch a PG-13 film.
- Times table: Ask for a number and print its times table up to 12.
- Guessing game: Pick a secret number 1–10. Keep asking the user to guess until they get it right.