Know some Python? Write your own interactive tutorial, with runnable examples and exercises, and share it with a link. Write and share your own tutorial.

Start writing

Free interactive tutorial

Learn Python

Learn Python online for free with short, hands-on lessons that take you from your first print() to classes and generators. Every example and exercise runs right here in your browser, so there's nothing to install.

  • Run code instantly, nothing to install
  • Exercises with answers
  • 100% free

31 lessons in 6 parts · each with runnable examples, exercises and hints

Part 1

Python Basics

Write your first programs and learn the building blocks every Python program uses.

  1. 1 Hello, World! Write and run your first Python program. Learn how print() works, how to write strings and comments, and how to read your first error message. Hello, World! Quiz
  2. 2 Variables and Types Learn how to store values in Python variables, the core data types (int, float, str, bool, None), how to check and convert types, and how to build text with f-strings. Variables and Types Quiz
  3. 3 Numbers and Math Do arithmetic in Python with int and float, learn operator precedence, rounding, useful built-ins like abs, min and max, and the math module. Numbers and Math Quiz
  4. 4 Strings Work with text in Python. Create strings, join and repeat them, count characters with len(), and pick out characters and slices with indexes. Strings Quiz
  5. 5 String Methods Change and inspect text with Python string methods. Learn upper, lower, strip, replace, find, count, split and join, and check for text with the in operator. String Methods Quiz
  6. 6 User Input Make interactive Python programs with input(). Learn how to convert input to numbers, handle bad input, and read several values at once. User Input Quiz
  7. 7 Booleans and Comparisons Learn Python's True and False values, comparison operators, the logical operators and, or and not, and how truthiness works. Booleans and Comparisons Quiz
  8. 8 If / Elif / Else Make decisions in Python with if, elif and else. Learn indentation, nested conditions, the conditional expression and the most common mistakes. If / Elif / Else Quiz
Part 2

Python Collections and Loops

Store many values at once and repeat work without repeating code.

  1. 9 Lists Store many values in one variable with Python lists. Learn indexing, slicing, adding and removing items, sorting, and the most useful list methods. Lists Quiz
  2. 10 Tuples Learn Python tuples, the ordered, unchangeable sequence. Create tuples, index them, unpack them into variables and see when to use a tuple instead of a list. Tuples Quiz
  3. 11 Dictionaries Store data as key-value pairs with Python dictionaries. Learn to add, update, look up and remove entries, use get() safely, and nest dictionaries. Dictionaries Quiz
  4. 12 Sets Learn Python sets for storing unique values. Remove duplicates, test membership quickly, and use union, intersection and difference to compare groups. Sets Quiz
  5. 13 For Loops Repeat code with Python for loops. Loop over lists, strings and dictionaries, use range() with start, stop and step, enumerate() and zip(), and build up totals and new lists. For Loops Quiz
  6. 14 While Loops Repeat code until a condition changes with Python while loops. Learn counters, input validation, break, continue, while True loops and how to avoid infinite loops. While Loops Quiz
  7. 15 List Comprehensions Build lists in a single readable line with Python list comprehensions. Add conditions, use if-else inside them, and write dictionary and set comprehensions. List Comprehensions Quiz
Part 3

Python Functions

Package code into reusable pieces with inputs and outputs.

  1. 16 Functions Write your own Python functions with def. Learn parameters, return values, docstrings, returning several values, and why functions make code easier to reuse and read. Functions Quiz
  2. 17 Function Arguments Master Python function arguments. Learn default values, keyword arguments, *args for any number of positional arguments, **kwargs for keyword arguments, and unpacking. Function Arguments Quiz
  3. 18 Scope Understand variable scope in Python. Learn the difference between local and global variables, the LEGB rule, and when to use global and nonlocal. Scope Quiz
  4. 19 Lambda, map and filter Write small anonymous functions with Python lambda, and use them with sorted, map and filter. Learn when a comprehension is the clearer choice. Lambda, map and filter Quiz
  5. 20 Recursion Learn recursion in Python, where a function calls itself. Understand base cases, see factorial, Fibonacci and countdown examples, and learn about the recursion limit. Recursion Quiz
Part 4

Working with Code and Data

Handle errors, use the standard library and work with files and dates.

  1. 21 Errors and Exceptions Handle errors gracefully in Python with try, except, else and finally. Learn common exception types, how to raise your own errors, and how to validate user input safely. Errors and Exceptions Quiz
  2. 22 Modules and Imports Use Python modules and the standard library. Learn import, from-import and aliases, explore random, math, statistics and collections, and create your own module. Modules and Imports Quiz
  3. 23 Reading and Writing Files Read and write files in Python with open() and the with statement. Learn file modes, reading line by line, appending, handling missing files, and working with CSV and JSON. Reading and Writing Files Quiz
  4. 24 String Formatting Deep Dive Format output like a pro with Python f-strings. Control decimal places, width, alignment, padding, thousands separators, percentages and signs, and build neat text tables. String Formatting Deep Dive Quiz
  5. 25 Dates and Times Work with dates and times in Python using the datetime module. Get today's date, create dates, format and parse them, do date arithmetic with timedelta, and compare dates. Dates and Times Quiz
Part 5

Object-Oriented Python

Model real things with classes, objects and inheritance.

  1. 26 Classes and Objects Start object-oriented programming in Python. Define classes, create objects, use the __init__ method, attributes and methods, and understand self and class attributes. Classes and Objects Quiz
  2. 27 Inheritance Reuse and extend Python classes with inheritance. Create subclasses, override methods, call the parent with super(), use isinstance, and see polymorphism in action. Inheritance Quiz
  3. 28 Special Methods Make your Python classes behave like built-in types with special (dunder) methods. Learn __str__, __repr__, __eq__, __lt__, __len__, __add__, __getitem__ and __contains__. Special Methods Quiz
Part 6

Going Further

Powerful features that make your code cleaner and more Pythonic.

  1. 29 Iterators and Generators Understand how Python for loops really work with iterators, iter() and next(), and write memory-friendly generators with yield and generator expressions. Iterators and Generators Quiz
  2. 30 Decorators Learn Python decorators step by step. See how functions can wrap other functions, write your own decorators with the @ syntax, keep names with functools.wraps, and use built-in decorators. Decorators Quiz
  3. 31 Type Hints Make Python code clearer with type hints. Annotate variables, function parameters and return values, use list, dict and optional types, and learn what type hints do and don't do. Type Hints Quiz

How this Python course works

Most people learn to program by doing, not by reading. Every lesson in this free Python tutorial is short, explains one idea at a time, and lets you run and change the code as you go.

  • Run real Python in your browser

    Each example is a live editor. Press Run to see the output, change a line and run it again. Python 3 runs on your device, so there's nothing to download or set up.

  • Practise with exercises

    Lessons end with exercises that check your output for you. Stuck? Open a hint, and when you're done, compare your code with the solution.

  • Test yourself with quizzes

    Every topic has a quiz: predict the output, spot the bug, put the lines in order or write a little code. Each answer comes with an explanation and a link back to the right section.

  • Learn in the right order

    The 31 lessons build on each other, and each one only uses what came before. Follow them in order, or use the search box to jump to the topic you need.

Who this Python tutorial is for

It's written for complete beginners: you don't need any programming experience, maths background or software. It also works well if you know another language and want to learn Python quickly. Skim the basics, then use the quizzes to find what's new to you.

What you'll learn

  • Write and run Python programs, and work with numbers, text and user input
  • Make decisions with if, and repeat work with for and while loops
  • Store data in lists, tuples, dictionaries and sets, and transform it with comprehensions
  • Organise code into functions, and understand arguments, scope, lambdas and recursion
  • Handle errors, import modules, read and write files, and format text and dates
  • Build classes with inheritance and special methods, and use generators, decorators and type hints

Why learn Python?

Python is one of the most popular programming languages in the world, and one of the easiest to start with. It's used for automating everyday tasks, analysing data, building websites, machine learning and teaching programming. Its clear syntax means you spend your time on ideas rather than punctuation, and the same skills carry over when you learn other languages later.

Frequently asked questions

Is this Python tutorial really free?

Yes. Every lesson, exercise and quiz is free, and you don't need an account. The site is supported by ads.

Do I need to install Python?

No. The examples and exercises run in your browser with Pyodide, a full version of Python 3 compiled to WebAssembly. When you want to write bigger programs, you can use the Online Python IDE on this site or install Python from python.org.

Is Python a good first programming language?

Yes. Python code is short and reads almost like English, so you can focus on ideas such as variables, loops and functions instead of punctuation. It is also one of the most widely used languages for automation, data analysis, web development and teaching, so what you learn stays useful.

How long does it take to learn Python?

Most beginners pick up the basics in a few weeks. Each lesson here takes about 15 to 30 minutes, so at one or two lessons a day you can finish all 31 lessons in about a month. Getting comfortable building your own projects takes a few months of regular practice.

Do I need any programming experience?

No. The first lesson starts with print(), and every lesson only uses ideas from the lessons before it. If you already know another language, skim the basics and use the quizzes to find the gaps.

How do the quizzes work?

Every lesson has a quiz drawn at random from a bank of 40 or more questions: predict the output, find the bug, arrange the code, fill in the blanks and more. Each answer is checked instantly with an explanation, and your best score is saved in your browser.

Can I learn Python on my phone?

Yes. The lessons, the code runner and the quizzes all work on phones and tablets. A keyboard makes the longer exercises easier, but you can read, run and change every example on a phone.

What should I learn after this course?

Build small projects with what you know, such as a quiz game, a budget tracker or a file organiser. Then pick a direction: web development with Flask or Django, data analysis with pandas, or automation with the standard library.