Part One
Hello, World
Every programming language has a tradition: the very first thing you learn to do is make the computer say "Hello, World!" It sounds trivial — and it is — but it teaches you something profound: you can give a computer a precise instruction, and it will follow it immediately.
In Python, this requires exactly one line. Press Run below.
The word print is a function — a built-in command that tells Python to display something on screen. The text inside the quotation marks is called a string: any sequence of characters. Try changing the text to your own name and running it again.
Part Two
Variables — Giving Names to Things
What if you want to save a value and use it again later? That is the purpose of a variable. A variable is a named container that stores a value. You create one by writing a name, an equals sign, and the value you want to store.
Variable name rules
In Python, variable names can use letters (a-z, A-Z), numbers (0-9), and underscores (_). A name cannot start with a number. It must start with a letter or underscore. Variable names are also case-sensitive, so age and Age are different variables.
Numbers (integers)
Whole numbers in Python are called integers. You can store them in variables and do arithmetic with them using the usual symbols: +, -, *, /.
Text (strings)
Text is stored in what programmers call a string — a sequence of characters wrapped in quotation marks. Strings can hold names, sentences, or anything written in words.
How to check a variable type
Use type() to see what kind of value a variable stores (for example int, str, or list).
+ operator does different things depending on the type. For numbers, it adds. For strings, it concatenates — it joins them end to end. Try combining three or four words.
Part Three
The print() Function — Talking to the Outside World
You have already used print(), but there is more to it. You can print multiple values at once by separating them with commas — Python will automatically place a space between them.
A more elegant approach uses f-strings. Place an f before the opening quote, and embed variable names directly inside the text using curly braces {}. Python will substitute each variable with its value.
You can even do calculations inside the curly braces:
Part Four
Lists — Collections in Order
So far, every variable has held a single value. But what if you need to store a collection of items — a list of student names, a series of exam scores? Python has a built-in type for this: the list.
A list is written with square brackets [], and its items are separated by commas. A list can hold any type of value — numbers, strings, or a mix of both.
You can access individual items by their index — their position in the list. In Python, counting starts at zero. The first item is at index 0, the second at 1, and so on. This surprises many beginners but quickly becomes natural.
Lists can grow. The .append() method adds a new item to the end. The built-in len() function tells you how many items are in the list.
Part Five
Dictionaries — Labeled Data
A list stores items accessed by position. A dictionary stores items accessed by name. Think of a real dictionary: you look up a word (the key) and find its definition (the value). Every entry in a Python dictionary is a key–value pair.
Dictionaries are written with curly braces {}. Each pair is written as key: value, separated by commas.
You can add new key–value pairs at any time, or update existing ones:
Part Six
For Loops — Doing Things Repeatedly
One of programming's most powerful ideas is repetition. Instead of writing the same instruction many times, you write it once and tell Python to repeat it. This is the job of a loop.
The for loop goes through a collection — a list, a dictionary, or any sequence — one item at a time, and runs the same block of code for each one.
Read the loop out loud: "For each fruit in fruits, print that fruit." Notice the indentation — the four spaces before print. In Python, indentation is not decoration; it tells Python which lines belong inside the loop. Every indented line will run once per item.
Looping over numbers
You can loop over a range of numbers using range(). range(5) produces 0, 1, 2, 3, 4 — it starts at zero and stops before reaching the number you give it.
Looping over a dictionary
When you loop over a dictionary, you get its keys. Use each key to look up its value. Or use .items() to get both the key and value at once — this is the most common pattern.
break and continue
Inside loops, break and continue give you extra control. break stops the loop immediately. continue skips the current iteration and moves to the next one.
In this example, n % 2 means "the remainder when n is divided by 2". If that remainder is 0, then n is an even number, so continue skips it. That is why only odd numbers are printed. Odd numbers are numbers like 1, 3, 5, 7 that are not divisible by 2.
Putting it all together
Here is a small program that uses everything you have learned: variables, a list, a dictionary, a loop, and f-strings. Read it carefully before running it.
for loop is used when you know in advance what collection you want to iterate over. When you need to keep looping until some condition changes, you will want a while loop — covered in Part Eight.
Part Seven
If Statements — Making Decisions
Programs rarely do the same thing every time. Usually they need to react to data: if a score is high, print "Pass"; if it is low, print "Fail". This is called conditional logic, and in Python it is expressed with the if statement.
The line if score >= 50: is a condition. Python evaluates it — either it is True or it is False. If it is True, the indented block runs. If it is False, Python skips over it entirely. Try changing the score to 30 and running again.
else — the alternative path
You can tell Python what to do when the condition is false using else:
elif — more than two possibilities
When there are more than two outcomes, use elif (short for "else if") to check additional conditions in sequence. Python stops at the first condition that is true.
Combining conditions
You can combine conditions with and and or. Use and when both conditions must be true; use or when at least one must be true.
If statements inside loops
Conditions and loops are often used together. Here, a loop goes through every student and an if decides what to print for each one:
== (equal to), != (not equal), > (greater than), < (less than), >= (greater than or equal), <= (less than or equal). Note that equality uses two equals signs — a single = is for assignment.
Part Eight
While Loops — Repeating Until Done
A for loop is ideal when you know in advance what collection to iterate over. A while loop is different: it keeps repeating as long as a condition remains true. You use it when you do not know how many repetitions will be needed.
Read it as: "While count is less than or equal to 5, print count, then add 1 to count." Each pass through the loop is called an iteration. After five iterations, the condition becomes false and the loop stops.
count = count + 1 does this.
Counting down
Accumulating a result
A common pattern is to build up a result across iterations. Here, a while loop sums all the numbers from 1 to 100:
for vs. while — when to use which
Chapter Navigation
Move between chapters.