Free interactive tutorial

Learn Python

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.

  • 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

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.
  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.
  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.
  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.
  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.
  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.
  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.
  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.
Part 2

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.
  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.
  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.
  4. 12 Sets Learn Python sets for storing unique values. Remove duplicates, test membership quickly, and use union, intersection and difference to compare groups.
  5. 13 For Loops Repeat code with Python for loops. Loop over lists, strings and dictionaries, use range(), enumerate() and zip(), and build up totals and new lists.
  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.
  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.
Part 3

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