Learn Python by running it

143 reference pages and 6 free courses, from first steps to pandas. Every example runs right in the page: change the code, press Run, see what happens.

Start the free course Open the playground

Try it: press Runeditable
prices = {"coffee": 3.20, "bagel": 2.50, "juice": 4.10}
order = ["coffee", "bagel", "coffee"]

total = sum(prices[item] for item in order)
print(f"{len(order)} items, total: €{total:.2f}")
Output
3 items, total: €8.90
( )

Reference

Built-in functions, string, list, dict and set methods and statements, each with runnable examples.

143 pages →
1·2·3

Courses

Python for beginners, Intermediate Python, Data analysis with pandas, Statistical modeling with Python, Automate everyday tasks with Python, Python for Excel users: lessons with exercises that check your answer.

88 lessons →
▶

Playground

An online Python editor that runs in your browser. Nothing to install.

Write code →

Most searched

all()Returns True when every item in a list or other iterable is true, and False as soon as one item is false. An empty iterable gives True.any()Returns True when at least one item in a list or other iterable is true, and False when every item is false or the iterable is empty.chr()Turns a Unicode code point, a whole number such as 65, into the character it stands for, such as A. It is the opposite of ord().enumerate()Adds a counter to any loop, giving you each item's position and the item itself, so you never need range(len(...)) again.format()Turns a value into nicely formatted text with a format spec: decimals, thousands separators, percentages, padding, alignment or binary.input()Shows an optional prompt, waits for the user to type a line and press Enter, and returns what they typed as a string.int()Turns text like "42" or a decimal number like 3.9 into a whole number, and can read binary, hexadecimal and other bases too.len()Returns how many items something holds: characters in a string, elements in a list or tuple, keys in a dictionary or members of a set.map()Calls a function on every item of a list or other iterable and gives back the results, for example to turn a list of strings into numbers.max()Returns the largest item of a list or of several arguments, with a key function to choose what to compare and a default for empty data.min()Returns the smallest item of a list or of several arguments, with a key function to decide what smallest means and a default for empty data.open()Opens a file so you can read its text, write new text, or add lines to the end. Use it with a with block so the file closes itself.ord()Turns one character into its Unicode code point, the whole number that stands for it, such as 65 for A. chr() goes the other way.print()Writes text, numbers and any other values to the screen, with control over the separator, the line ending and where the output goes.range()Produces a sequence of whole numbers from a start to an end, with an optional step, mostly used to repeat a for loop a set number of times.round()Rounds a number to the nearest whole number or to a chosen number of decimals, with two surprises: halves go to the even side and floats are not exact.sorted()Returns a new sorted list from any collection, smallest first or reversed, with a key function to sort by length, by a field or ignoring case.str()Turns numbers, lists and any other value into readable text, so you can join it with other strings, print it or save it to a file.sum()Adds up all the numbers in a list, tuple or any other iterable, with an optional starting value, in one short and fast call.type()Tells you the type of any value, such as int, str, list or dict, which helps when a program does not behave the way you expect.zip()Pairs up items from two or more lists by position, so you can loop over them together, build a dictionary, or split pairs apart again.str.count()Counts how many times a piece of text appears in a string, without overlaps, in the whole string or in a part of it.str.find()Returns the position where a piece of text first appears in a string, or -1 when it is not there at all.str.format()Fills values into the {} placeholders of a string, with control over decimals, width, alignment and thousand separators.

Browse the reference

Built-in functions49Functions you can use anywhere without importing anything: print, len, range, input, sorted and more.String methods33Clean, split, search and reformat text: split, join, strip, replace, find, upper and more.List methods11Add, remove, sort and search the items of a list: append, extend, pop, sort and more.Dictionary methods11Look up, loop over and update key-value pairs: get, items, keys, update, pop and more.Set methods13Unique items and set maths: add, discard, union, intersection, difference and more.Statements and syntax26The building blocks of every program: if, for, while, def, try, with, comprehensions and f-strings.

Spotted a mistake on this page? Tell us