Python lessons

Short lessons on the Python fundamentals, each with examples you can edit and run in your browser and a “Your turn” task that checks your output. They are grouped by concept in the order they are taught — see the concept map for how the concepts connect and where they sit in the Australian Curriculum.

Printing output

  • Printing Output — print() displays a value on the screen. It is how you see what your program is doing — and your main tool for finding out why something went wrong.

Variables

  • Variables — A variable is a name you assign to a value using =. You can then use that name to refer to the value later.

User input

  • Reading Input — input() waits for someone to type an answer and press Enter. It gives your program that answer as text. Store it in a variable so you can use it later.

Strings

  • String Formatting — A string is a text value. You can write one inside matching quotes. An f-string lets you insert values into text by putting expressions inside {}.
  • String Methods — A method is an operation you call on a value. Write the value, a dot, the method name and parentheses, as in name.upper(). String methods can change the case…

Arithmetic

  • Data Types and Operations — In these exercises, you will use integers for whole numbers such as 7, and floats for numbers such as 3.75. Arithmetic operators combine these values.
  • Modulo and Divisibility — The % operator — modulo — gives the remainder after a division. A remainder of 0 means the division was exact, which is how Python programs test whether one…

Comparisons and logical operators

  • Comparisons and Logical Operators — A boolean is one of just two values: True or False. Comparisons produce booleans, and booleans drive every decision your program makes.
  • Membership Testing — in asks a yes/no question: is this value inside that collection? It works on strings, lists and dictionaries, and it always gives back True or False.

Indexing

  • Indexing — A string is a sequence of characters, and every character has a position, called its index. Square brackets pull one character out by its index.

Lists

  • Lists — A list holds several values in order, inside square brackets. It is Python's everyday container for "a bunch of things".
  • Unpacking Values — Unpacking pulls several values out of a list into named variables in a single line — much easier to read than indexing each position by hand.
  • Nested Data — A list or dictionary can contain other lists or dictionaries. This is called nested data. Read it one layer at a time: first select a container, then an item…

Defining functions

  • Defining Functions — A function is a named, reusable block of code. Defining it describes a job; calling it runs that job.

if / elif / else

  • if, elif, else — Programs often need to choose what to do. A phone, for example, can show a warning when its battery is low and a different message when it has plenty of charge.

while loops

  • While Loops — A while loop repeats its body while a condition is true. Python checks the condition before each repetition. If it is false at the first check, the body does…

for loops

  • For Loops — A for loop runs the same block of code once for each item in a collection. It saves you from writing the same lines over and over.
  • The range() Function — range() produces a sequence of numbers to loop over — handy when you want to repeat something a set number of times, or step through a run of values.
  • The Accumulator Pattern — An accumulator stores a result built up as a loop runs. Initialize it before the loop, update it when needed inside the loop, and use the completed result…
  • break and continue — break stops a loop immediately. continue skips the rest of this turn and moves on to the next one. Both let a loop react to what it finds.
  • Tracking the Best Candidate — A loop can remember the best candidate examined so far. Decide what “best” means, choose an initial state, and replace the candidate only when a better one…

Dictionaries

  • Dictionaries — A dictionary stores pairs: each key maps to a value. You look things up by key instead of by position, which makes it perfect for "this name has this score"…

Parameters & return

  • Parameters & Return — Parameters are the names a function uses for values supplied by its caller. return supplies a result back to that caller. Read Defining Functions first.
  • Stopping a Search with return — A search checks candidates until it finds an answer or runs out of candidates. Inside a function, return can stop the search as soon as the answer is known.

Nested loops & ifs

  • Nested Loops & Ifs — A nested loop is a loop inside another loop. Each time execution reaches the inner loop, it starts a new traversal. In the examples here, that traversal…

Decomposition

  • Decomposition — Decomposition means dividing a program into smaller jobs. Give each function a clear responsibility and a name that describes it. One function can call…

Data validation

  • Data Validation — Data validation checks whether data meets the rules for its intended use. Data can come from a person, a file, a list or another program. Decide the rules…

Tracing code

  • Tracing Code — Tracing means following a program by hand, one executed statement at a time. Write down changing values and output before running the program. Trace what the…

Debugging

  • Debugging — Debugging means finding why a program behaves differently from its intended behaviour, fixing the cause, and checking the fix.

More lessons

  • Logical Operators — merged lesson — Logical operators are now taught together with comparisons in Comparisons and Logical Operators.
  • Numbers and Math — Python has two kinds of numbers: integers (whole numbers like 7) and floats (decimals like 3.75). You combine them with math operators.
  • Minimum, Total and Average — A report often needs several facts from the same list. You can update more than one accumulator during one loop.
  • Reading Text Files — A text file stores characters that a program can read later. In these exercises, the supplied files use UTF-8 encoding. A relative filename is resolved from…
  • Sets and Tuples — A tuple is a fixed group of values in round brackets. A set is a collection in curly brackets that never holds the same value twice.
  • Sorting — This is an extension beyond the core Python concepts. Read Lists and Dictionaries first.

Ready to practise? Start the free Python course.