Programming · Grade 10 · Chapter 5
Intro to Python: Setup & Syntax
Writing your first real programs, understanding how Python works, and building a solid foundation for everything that follows.
Learning objectives
What We'll Cover
- Understand what Python is and why it matters in industry and AI
- Set up a Python environment (IDLE, Thonny, or online IDE)
- Understand how Python executes code (interpreter model)
- Write programs using
print(), input(), variables, and comments
- Read and fix simple Python error messages
- Understand Python's indentation rules and why they exist
Why Python?
The Language That Runs the World
Python is the #1 programming language for AI and data science, used by Google, Netflix, NASA, Instagram, and most machine learning research teams. It is also the most beginner-friendly serious language ever designed.
Fun fact: Python was named after Monty Python's Flying Circus — not the snake. It was designed to be readable enough that a non-programmer could understand it.
How Python works
The Interpreter Model
Unlike compiled languages (C, Java) that translate all code to machine code in advance, Python uses an interpreter — it reads and executes your code line by line, in real time.
1You write .py file
2Python interpreter reads line 1
3Executes it immediately
4Moves to line 2...
Result: Errors are found at runtime — only when Python reaches the broken line. This is different from compiled languages where all errors are found before running.
Setting up
Three Ways to Run Python
IDLE (built-in)
Comes with Python. Simple, reliable. Good for beginners. Use the editor for scripts, shell for testing single lines.
Thonny
Designed for learning. Shows variable values visually. Great debugger. Recommended for this course.
Online IDEs
repl.it, Programiz — no installation needed. Run Python in browser. Perfect for quick demos.
VS Code + Python
Professional editor, used by most working developers. More setup required but extremely powerful.
First syntax
print() — Your First Function
print() outputs text to the screen. It is the most fundamental tool for understanding what your program is doing.
# Print a simple message
print("Hello, world!")
# Print a number
print(42)
# Print multiple things
print("The answer is", 42)
Variables
Storing Information in Variables
A variable is a named container that stores a value. You assign values with the = operator.
# Variable assignment
name = "Ahmed"
age = 16
height = 1.75
# Use in print
print("Name:", name)
print("Age:", age)
Note: Variable names are case-sensitive. age and Age are two different variables.
Variable naming rules
Legal vs. Illegal Variable Names
✅ Valid
student_name
score2
total_price
_counter
isActive
❌ Invalid
2score (starts with number)
student-name (hyphen not allowed)
for (reserved keyword)
my name (spaces not allowed)
$price (special characters)
Python convention: Use snake_case for variable names (words joined by underscores). Not camelCase or PascalCase.
User input
input() — Getting Information from the User
input() pauses execution, shows a prompt, and waits for the user to type something and press Enter. It always returns a string.
# Ask the user for their name
name = input("What is your name? ")
print("Welcome to Python,", name + "!")
# Ask for age (remember: it comes in as a string!)
age = input("How old are you? ")
print("In 10 years you will be", int(age) + 10)
Quick check · 1
What does input() always return?
AAn integer
BA float
CA string
DA boolean
Click to reveal answer
Comments
Commenting Your Code
A comment is a line Python ignores. It is written for humans reading the code. Good comments explain why, not what.
# This is a single-line comment
score = 0 # Track the player's total score
# BAD comment (explains what, not why):
x = x + 1 # Add 1 to x
# GOOD comment (explains why):
x = x + 1 # Increment frame counter for animation
Critical Python rule
Indentation Is Not Optional
In Python, indentation (spaces at the start of a line) is part of the syntax. It tells Python which lines belong inside a block (function, loop, condition).
❌ IndentationError
if score > 50:
print("You passed!") # no indent
✅ Correct
if score > 50:
print("You passed!") # 4 spaces
Convention: Always use 4 spaces per indent level. Never mix tabs and spaces.
Reading errors
Python Error Messages Are Your Friends
When Python fails, it tells you: the file, the line number, and the type of error. Learning to read error messages is a crucial skill.
Traceback (most recent call last):
File "script.py", line 3, in <module>
print(message)
NameError: name 'message' is not defined
Reading this: Line 3, NameError — the variable message was used before being assigned. Always check the line number first.
Common error types
Error Type Reference
SyntaxError
Code is not valid Python syntax. Often a missing colon, bracket, or quote.
NameError
Using a variable that hasn't been defined yet.
TypeError
Performing an operation on the wrong type — e.g., adding a string to an integer.
IndentationError
Indentation is wrong — a block is not indented or has inconsistent spacing.
Quick check · 2
Which function outputs text to the screen in Python?
Ainput()
Bprint()
Coutput()
Ddisplay()
Click to reveal answer
Guided practice · 1
Write a Greeting Program
Live-code together. Follow along and type each line.
# Step 1: Ask for the user's name
name = input("Enter your name: ")
# Step 2: Ask for their age
age = int(input("Enter your age: "))
# Step 3: Print a personalised message
print("Hello,", name + "!")
print("Next year you will be", age + 1)
Extension: Add a line asking for their favourite subject and printing a personalised encouragement.
Quick check · 3
What happens if you run: age = input("Age: ") and then print(age + 5)?
AIt adds 5 to the number correctly
BTypeError — you cannot add an integer to a string
CIt prints nothing
DIt converts the string automatically
Click to reveal answer
Guided practice · 2
Spot and Fix the Bug
Each of these programs has a bug. Find it and fix it.
# Program 1
print("Hello)
# Program 2
name = input("Name: ")
print(Name)
# Program 3
age = input("Age: ")
print(age * 2)
Answers: 1) Missing closing quote. 2) NameError — Name ≠ name. 3) String × 2 = repeated string, not doubled number — use int(age).
String operations
Working with Strings
greeting = "Hello"
name = "Layla"
# Concatenation (joining)
print(greeting + ", " + name + "!")
# f-strings (modern, preferred way)
print(f"Hello, {name}! Welcome.")
# String length
print(len(name)) # outputs 5
f-strings
f-Strings: The Modern Way to Format Output
An f-string lets you embed variables directly inside a string using {curly braces}. Far cleaner than concatenation.
Old way (concatenation)
print("Name: " + name + ", Age: " + str(age))
✅ f-string (clean)
print(f"Name: {name}, Age: {age}")
f-strings also support expressions: print(f"Double: {age * 2}")
Quick check · 4
Which is the correct f-string to print "Hello, Yasmin!"?
Aprint("Hello, " + Yasmin + "!")
Bprint(f"Hello, {name}!") where name = "Yasmin"
Cprint(f"Hello, name!")
Dprint("Hello, {name}!")
Click to reveal answer
Live coding challenge
Mini Calculator Program
Build a program that asks for two numbers and prints their sum, difference, product, and quotient.
a = float(input("First number: "))
b = float(input("Second number: "))
print(f"Sum: {a + b}")
print(f"Difference: {a - b}")
print(f"Product: {a * b}")
print(f"Quotient: {a / b}")
Challenge: What happens if b = 0? Can the class predict the error before running it?
Quick check · 5
What does this print? name = "Ali" — print(f"Hello {name}! You have {len(name)} letters.")
AHello {name}! You have {len(name)} letters.
BHello Ali! You have 3 letters.
CHello Ali! You have len(name) letters.
DError — len() cannot be used in an f-string
Click to reveal answer
Pair programming
Pair Activity: Profile Card Program
In pairs, write a program that collects information and outputs a formatted "profile card".
- Ask: name, age, city, favourite subject
- Print a nicely formatted card using f-strings
- Include at least one calculation (e.g., what year they were born)
- Add comments explaining each section
Share: Pairs demonstrate to the class — teacher can ask the class to predict the output before running.
Quick check · 6
Which of these is a valid Python variable name?
A2score
Bstudent-name
Cstudent_score
Dfor
Click to reveal answer
Python in context
Where This Leads
The foundations we built today are the building blocks of everything else:
Ch5Variables, input, print
Ch6Data types & operators
Ch7Conditionals
Ch8Loops
Ch9Functions
Ch10AI models (built on all of this)
Exit ticket
What Surprised You About Python?
Think about what you expected programming to feel like before today — and what it actually felt like.
Write (or discuss): Name one thing that surprised you, one thing that was harder than expected, and one question you still have. Share with a partner.
Before you go
Today We Learned...
- Python is interpreted — runs line by line, errors found at runtime
print() outputs, input() accepts user text (always as a string)
- Variables store values; names must follow rules (snake_case convention)
- Indentation is part of Python syntax — never skip it
- f-strings make string formatting clean and readable
- Error messages point to the exact line and type — read them, don't fear them
Next chapter: Data types and operators — why 3 + "5" crashes Python, and how to fix it.