An Introduction to Programming

Python for All

Chapter One — The Basics

Thanasis Troboukis  ·  All Chapters

Chapter One

The Basics

Core concepts explained step by step, with live examples you can run and modify.

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.

Python · Try it

      

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.

Key idea: In programming, each instruction is called a statement. Python reads your statements from top to bottom, one at a time, and carries them out in order — much like following a recipe.

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.

Python · Try it

      

Numbers (integers)

Whole numbers in Python are called integers. You can store them in variables and do arithmetic with them using the usual symbols: +, -, *, /.

Python · Try it

      
Python · Try it

      

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.

Python · Try it

      

How to check a variable type

Use type() to see what kind of value a variable stores (for example int, str, or list).

Python · Try it

      
Note: The + 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.

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.

Python · Try it

      

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.

Python · Try it

      

You can even do calculations inside the curly braces:

Python · Try it

      
f-strings are one of Python's most readable features. They let you compose output naturally, weaving variables into sentences exactly where they belong. Prefer them over concatenation whenever you are mixing text and values.

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.

Python · Try it

      

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.

Python · Try it

      

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.

Python · Try it

      
Key idea: A list preserves the order of its items. The item you append last will always be at the end. Use a list whenever the sequence of items matters.

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.

Python · Try it

      

You can add new key–value pairs at any time, or update existing ones:

Python · Try it

      
List vs. Dictionary: Use a list when items are similar in nature and their order matters (e.g., a sequence of scores). Use a dictionary when each item has a meaningful label (e.g., the attributes of a person or record).

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.

Python · Try it

      

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.

Python · Try 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.

Python · Try it

      

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.

Python · Try it

      
Python · Try it

      

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.

Python · Try it

      
Key idea: A 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.

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.

Python · Try it

      

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:

Python · Try it

      

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.

Python · Try it

      

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.

Python · Try it

      

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:

Python · Try it

      
Comparison operators: == (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.

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.

Python · Try it

      

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.

Warning — infinite loops: If the condition never becomes false, the loop runs forever and your program freezes. Always make sure something inside the loop moves you closer to the stopping condition. In the example above, count = count + 1 does this.

Counting down

Python · Try it

      

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:

Python · Try it

      

for vs. while — when to use which

Use a for loop when you have a collection to iterate over (a list, a dictionary, a range). Use a while loop when you need to keep going until some condition changes — for example, reading input until the user types "quit", or repeating a calculation until it converges.

Chapter Navigation

Move between chapters.

Loading Python environment — this may take a moment…