Kiks & Learn
Start learning ↗
Python / Beginner

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.

YOUR FREE LEARNING PATH

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
quantity = 3price = 4total = quantity * priceprint(total)OUTPUT12
A small Python program producing a checked result
4 lessons · FreeOptional official free reference ↗
LESSON 01 / 30 MINUTES

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.

Values Python can work with
ExampleTypeUseful for
`"Notebook"`Text (`str`)Names and messages
`4`Whole number (`int`)Counts
`4.50`Decimal (`float`)Approximate measurements
`True`Boolean (`bool`)Yes/no conditions
TRY IT YOURSELF

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 ↗.

Loading contacts YouTube.
LESSON 02 / 30 MINUTES

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.

Condition pieces
PythonMeaning
`==`Is equal to
`!=`Is not equal to
`<` / `>=`Less than / at least
`and` / `or`Both conditions / either condition
TRY IT YOURSELF

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 ↗.

LESSON 03 / 30 MINUTES

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.

Trace the running total
Amount readTotal beforeTotal after
12012
101222
152237
TRY IT YOURSELF

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 ↗.

LESSON 04 / 30 MINUTES

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.

Small tests are useful
InputExpected resultWhy test it?
`[12, 10, 15]`37Normal case
`[]`0Empty input
`[0]`0Recorded zero
TRY IT YOURSELF

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 ↗.

KEEP BUILDING

Recommended next steps

These paths build on ideas from this course. You can start any of them whenever you are ready.

CHECK YOUR UNDERSTANDING

Your final knowledge check.

Score 80% or more to pass. Sign in to save your best result.

1. What does return do?
2. How should you finish this learning path?

Sign in and start tracking this course to submit and save your score. Use each lesson’s self-check to practice without an account.

Learn together

Sign in to leave a comment →