Python: your first practical programs
Write your first useful Python programs using variables, conditions, functions, and loops, with beginner-friendly examples you can run and change.
Learn it here, one small step at a time.
4 guided lessons · About 2h 30m including practice · No subscription
- Use variables and basic data types
- Write reusable functions and safe conditions
- Process a list with a loop and check the result
Store values and read the output
A variable gives a value a readable name. Use = to assign: price = 4. Numbers can be added as numbers; text must be written in quotes. print() shows a value so you can inspect it. Python distinguishes uppercase and lowercase, so price and Price are different names.
Run one small line at a time. If Python displays an error, read the line number and message; an error is information about what needs fixing, not a verdict on your ability.
| Example | Type | Useful for |
|---|---|---|
| `"Notebook"` | Text (`str`) | Names and messages |
| `4` | Whole number (`int`) | Counts |
| `4.50` | Decimal (`float`) | Approximate measurements |
| `True` | Boolean (`bool`) | Yes/no conditions |
Set `item = "Notebook"` and `quantity = 3`. Print a sentence that includes both values. Then calculate `quantity * 4` and print the result.
Reveal a suggested answer
`4` is a number; `"4"` is text. Text must be converted before doing arithmetic.
Optional further reading
Core lesson and practice are on this page. For more detail, visit the official free reference ↗.
Make decisions and write a function
An if statement chooses a path when a condition is true. Indentation marks which instructions belong to that path. A function packages a small task under a name; return sends its result back to the place where it was called.
def line_total(quantity, price):
if quantity < 0 or price < 0:
return None
return quantity * price
print(line_total(3, 4)) # 12
Clear names and one job per function make mistakes easier to find.
| Python | Meaning |
|---|---|
| `==` | Is equal to |
| `!=` | Is not equal to |
| `<` / `>=` | Less than / at least |
| `and` / `or` | Both conditions / either condition |
Run the example. Change the quantity to `-1`; add a message when the function returns `None` so the user knows why no total was produced.
Reveal a suggested answer
A returned value can be reused in another calculation; printed text is only shown to the person running the program.
Optional further reading
Core lesson and practice are on this page. For more detail, visit the official free reference ↗.
Repeat a task with a loop
A list keeps several values in order: sales = [12, 10, 15]. A for loop works through each value once. Use an accumulator when you want a total, starting at zero and adding each value. An empty list should naturally produce a total of zero.
total = 0
for amount in sales:
total = total + amount
print(total) # 37
Test one normal case, an empty list, and a list containing zero. Those small tests help reveal assumptions.
| Amount read | Total before | Total after |
|---|---|---|
| 12 | 0 | 12 |
| 10 | 12 | 22 |
| 15 | 22 | 37 |
Write the loop by hand and trace `[4, -2, 8]`. If you want to add only positive values, where does the `if amount > 0` check go?
Reveal a suggested answer
Zero. The loop does not run, so the accumulator stays at its starting value of zero.
Optional further reading
Core lesson and practice are on this page. For more detail, visit the official free reference ↗.
Build and test a tiny sales summary
Combine the skills into a tiny report. Store sales as a list, calculate the total with a function, and print a clear result. Separate the work into small steps: create data, calculate, format output, then test. Do not worry about building a full application yet.
def total_sales(values):
total = 0
for value in values:
total += value
return total
sales = [12, 10, 15]
print(f"Total sales: ${total_sales(sales)}")
A test is a specific input and an expected result. Check ordinary values, an empty list, and a value that should not be accepted.
| Input | Expected result | Why test it? |
|---|---|---|
| `[12, 10, 15]` | 37 | Normal case |
| `[]` | 0 | Empty input |
| `[0]` | 0 | Recorded zero |
Change one number and predict the answer before running the program. Then write one sentence explaining what the function does.
Reveal a suggested answer
A prediction helps you notice a surprising output instead of accepting it without checking.
Optional further reading
Core lesson and practice are on this page. For more detail, visit the official free reference ↗.
Recommended next steps
These paths build on ideas from this course. You can start any of them whenever you are ready.
Your final knowledge check.
Score 80% or more to pass. Sign in to save your best result.
