Free · Runs in your browser · No install

Free Python lessons

Short lessons, from your first print() to advanced Python. Edit and run every example right here, then try a “Your turn” task that checks your output.

Popular:

Where do you want to start?

Free course

Python Fundamentals

Never coded before? Follow the Python Fundamentals course from your first print() to functions, lists and dictionaries.

31 lessons · 37 exercises in Python Fundamentals that check your code

  1. Printing Output

    print() displays a value on the screen. It is how you see what your program is doing.

    2 exercises

  2. Variables

    A variable is a name that stores a value. You create one with =, then use the name to get the value back later.

    3 exercises

  3. String Formatting

    A string is a text value written inside matching quotes. An f-string lets you insert values into text by putting them inside {}.

    5 exercises

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

    2 exercises

  5. Data Validation

    Data validation checks that data follows your rules before you use it, such as "a whole number from 0 to 120".

    6 exercises

  6. Data Types and Operations

    Python numbers come in two data types: integers for whole numbers like 7, and floats for decimals like 3.75. Arithmetic operators do maths with them.

    4 exercises

Python fundamentals

Practise these in the free Python Fundamentals course →

Printing output

Variables

User input

Strings

Arithmetic

Comparisons and logical operators

Indexing

Lists

Defining functions

if / elif / else

while loops

for loops

Dictionaries

Parameters & return

Nested loops & ifs

Decomposition

Data validation

Tracing code

Debugging

More lessons

Turtle Graphics

Practise these in the free Python Basics with Turtle course →

  1. Turtle graphics in Python

    Turtle graphics is Python's way to draw: a turtle is a pen you steer with commands like forward() and left(), and it draws a line wherever it goes.

    1 exercise

  2. Turtle backward(): going back to the start

    backward() moves the turtle back the way it came, undoing a forward(). With right() to undo a left(), you can bring the turtle back to its start.

  3. Turtle coordinates: goto() and position()

    Turtle coordinates are the x and y numbers for each point in the turtle window. (0, 0) is the centre, x grows to the right and y grows upwards.

  4. penup() and pendown()

    penup() lifts the turtle's pen so it moves without drawing, and pendown() puts the pen back down so it draws again.

  5. Turtle colours: pencolor(), pensize() and bgcolor()

    pencolor() sets the colour of the lines a turtle draws, pensize() sets how thick they are, and bgcolor() colours the window behind them.

    1 exercise

  6. Filling shapes: begin_fill() and end_fill()

    A turtle fill colours in a shape: start the fill, draw the shape, end the fill, and the turtle paints the area inside its moves with its fill colour.

  7. Drawing shapes with turtle loops

    A for loop draws a turtle shape by repeating one side: the same forward() and left() run again for every side, so a square takes three lines, not eight.

  8. Turtle circles: circle() and dot()

    circle(radius) makes the turtle drive round a circle, and dot(size) puts a round, filled spot where the turtle stands.

  9. Turtle text and stamps: write(), shape() and stamp()

    write() puts text on the turtle window, shape() changes what the turtle looks like, and stamp() leaves a copy of that shape behind.

  10. The turtle screen: Screen(), speed() and tracer()

    turtle.Screen() gives you the turtle window itself. Use it to set the window's title, size and colour, to control drawing speed, and to hold several turtles.

Advanced concepts

A free account saves your progress and gives AI hints in every course →

Printing Output

  1. Python Escape Characters: \n, \t, Quotes and Raw Strings
  2. Python print() sep and end Parameters

Variables and Assignment

  1. Python += and -=: Updating and Swapping Variables
  2. Python Names and References: Aliasing, is and copy()

Numbers and Arithmetic

  1. Python round(): Round to 2 Decimal Places (and Why 0.1 + 0.2 ≠ 0.3)
  2. Python Operators: Floor Division //, Modulo % and Powers **

Working with Lists

  1. Python List Slicing, + and *: Build New Lists
  2. Python List Methods: insert(), pop(), sort(), index() and count()
  3. Python Tuple Unpacking: *rest, First and Last Values

Working with Dictionaries

  1. Python Dictionary Values: Mixed Types, Lists and Totals
  2. Python Dictionary get(): Defaults and Counting Items1 exercise
  3. Python sorted() with key: Rank a Dictionary by Value1 exercise

Working with Strings

  1. Python Number Formatting: Decimals, Commas, % and Alignment
  2. Python String Methods: Chaining strip(), lower() and More1 exercise
  3. Python String find(), count() and startswith(): Search Text
  4. Python String Slicing: Start, Stop, Step and Reverse

Reading Input

  1. Python input(): Strip, Lowercase and Split User Input

Data Validation

  1. Python Type Conversion: Checking Text Before int() and float()

Comparisons and Booleans

  1. Python Comparison Operators: Chaining and Comparing Strings
  2. Python Booleans: Truthy and Falsy Values

For Loops

  1. Python for else: Run Code When a Loop Finds Nothing

Nested Loops & Ifs

  1. Python Pairs Without Repeats: Nested Loops and itertools

While Loops

  1. Python while True with break
  2. Python Walrus Operator (:=) in while Loops

The range() Function

  1. Python range(): len(), Indexing, in and reversed()

The Accumulator Pattern

  1. Accumulator Starting Values: 1, Empty Text and Dictionaries
  2. Python sum(), max() and min(): Built-in Accumulators
  3. Python max() and min() with key=: Choose the Best Item

Making Decisions

  1. Python Ternary Operator: x if condition else y
  2. Python match case: Python's Switch Statement

Random Numbers

  1. Python random.choice(), shuffle() and sample()
  2. Python random.seed(): Repeatable Random Numbers

Debugging

  1. Python Traceback: How to Read Error Messages
  2. Python assert Statement: Check Assumptions While Debugging

Iteration and Functional Tools

  1. Python Iterators: iter(), next() and How for Loops Work
  2. Python Generators: Your First Generator and next()1 exercise
  3. Python yield: Write a Generator Function1 exercise
  4. Infinite Generators in Python: Running State1 exercise
  5. Python Generator close(): Stopping a Generator1 exercise
  6. Python Generators and yield: Lazy Iteration Explained
  7. Python Generator in a for Loop: StopIteration Explained1 exercise
  8. Python yield vs return: Lazy Evaluation with Generators1 exercise
  9. Python Generator Function: Filter Lines with yield1 exercise
  10. Python Generator Pipeline: Take First N Items (islice)1 exercise
  11. Python Read File Line by Line with a Generator1 exercise
  12. Parse a Log File in Python with Generators1 exercise
  13. Python enumerate(): Loop with Index and Value3 exercises
  14. Python zip(): Loop Over Lists Together1 exercise
  15. Python Lambda Functions Explained with Examples
  16. Python map() and filter() (and When to Use a Comprehension)

Comprehensions

  1. Python List Comprehension: Transform and Filter Lists1 exercise
  2. Python Dictionary Comprehension (Plus Set Comprehension)
  3. Python Generator Expressions Explained
  4. Python Generator Expression vs List: Memory (getsizeof)1 exercise

Functions, Closures and Decorators

  1. Python Docstrings: Documenting Functions
  2. Python Default Arguments and Keyword Arguments
  3. Python Variable Scope: Local and Global Variables
  4. Python Function Call Order and the Call Stack
  5. Python First-Class Functions and Closures
  6. Python Decorators Explained: @ Syntax and functools.wraps

Modules and Imports

  1. Python Import: Modules, from…import and as2 exercises
  2. How to Create Your Own Python Module

Files and JSON

  1. Python read() and readlines(): Read a Whole File
  2. How to Write to a Text File in Python1 exercise
  3. Python JSON: loads, dumps, load and dump1 exercise

Web APIs

  1. API basics with requests5 exercises
  2. Inspecting API responses4 exercises

Dates and Times

  1. Python datetime: Dates, Times and Comparing Them
  2. Python timedelta: Days Between Two Dates
  3. Python strftime and strptime: Format and Parse Dates

Regular Expressions

  1. Python Regex Basics: re.search, re.findall and Patterns
  2. Python Regex Character Classes, Anchors and re.fullmatch
  3. Python Regex Groups and re.sub: Capture and Replace

Object Oriented Programming

  1. Python Classes and Objects: __init__ and Instances3 exercises
  2. Python Methods and self: Reading and Changing Object State2 exercises
  3. Python Inheritance: Subclasses and Parent Classes
  4. Python super() and Method Overriding Explained
  5. Python Dataclasses: @dataclass, Defaults and Free Methods5 exercises
  6. Python Dataclass Methods: Add Behaviour to Fields3 exercises
  7. Python OOP Composition with Dataclasses: Objects Inside Objects2 exercises
  8. Python Dataclass Validation with __post_init__2 exercises
  9. Python Frozen Dataclasses and replace2 exercises
  10. Python Dataclass Inheritance and Polymorphism2 exercises
  11. Python Dataclass asdict Examples1 exercise
  12. Dataclasses vs Pydantic

Exceptions and Error Handling

  1. Python Try Except: Catching Errors with else and finally4 exercises
  2. How to Raise Exceptions in Python (and Pick the Right One)2 exercises
  3. Python Custom Exceptions: Subclassing Exception

Context Managers

  1. Python Context Managers: Write Your Own with @contextmanager2 exercises
  2. Python try finally: Cleanup Code That Always Runs2 exercises
  3. Python with Statement: as, Yielded Values and Multiple Context Managers4 exercises
  4. Context Manager Classes3 exercises

Type Hints and Protocols

  1. Python Type Hints: Annotating Variables and Functions1 exercise
  2. Python Optional and Union Types: int | None and X | Y
  3. Python Protocol: Structural Typing with runtime_checkable

The Agent Game

Put your Python to work

Write an agent that plays a strategy game, then watch it take on our bots. No account needed.

  • Play Greedy Pig →

    Play Greedy Pig, the push-your-luck dice game, by writing a Python agent that decides when to bank. Edit, run and test it free in your browser — no sign-up.

  • Play Lineup 4 →

    Write a Python agent that plays Lineup 4, a connect-four game, and test it against five bots free in your browser — no sign-up, no install.