Menu

Python Cheat Sheet

Last updated

Basics & printing

Variables, comments, and getting output on the screen.

OperationSyntax
Assign a variablex = 10
Print a valueprint("Hello")
Print multiple valuesprint("x =", x)
Single-line comment# this is a comment
Read inputname = input("Name: ")
Multiple assignmenta, b = 1, 2
Check the typetype(x)

Data types

The core built-in types and how to convert between them.

TypeExample
Integer (int)age = 25
Float (float)price = 9.99
String (str)name = "Ada"
Boolean (bool)is_active = True
Listnums = [1, 2, 3]
Tuple (immutable)point = (4, 5)
Dictionaryuser = {"id": 1}
None / convertvalue = None, int("7"), str(42)

Strings & f-strings

Format and manipulate text.

OperationSyntax
f-string interpolationf"Hi {name}, you are {age}"
Lengthlen(text)
Upper / lower casetext.upper(), text.lower()
Strip whitespacetext.strip()
Replacetext.replace("a", "b")
Split into a listtext.split(",")
Join a list", ".join(items)
Slice characterstext[0:3]
Contains"py" in text

Lists

Ordered, mutable sequences.

OperationSyntax
Createnums = [1, 2, 3]
Access by indexnums[0], last: nums[-1]
Add to the endnums.append(4)
Insert at indexnums.insert(0, 9)
Remove a valuenums.remove(2)
Pop by indexnums.pop()
Slicenums[1:3]
Sort in placenums.sort()
Lengthlen(nums)

Dictionaries

Key-value pairs for fast lookups.

OperationSyntax
Createuser = {"id": 1, "name": "Ada"}
Access a valueuser["name"]
Safe access (no error)user.get("age", 0)
Add / update a keyuser["age"] = 25
Delete a keydel user["age"]
Check for a key"name" in user
Iterate keys & valuesfor k, v in user.items():
All keys / valuesuser.keys(), user.values()

Control flow

Conditionals and loops.

OperationSyntax
If / elif / elseif x > 0:elif x == 0:else:
For loop over a listfor item in items:
For loop over a rangefor i in range(5):
While loopwhile x < 10:
Loop with indexfor i, v in enumerate(items):
Break / continuebreak, continue
Ternary expressiony = 1 if x else 0

Functions

Define reusable blocks of code.

OperationSyntax
Define a functiondef greet(name):
Return a valuereturn name.upper()
Default argumentdef greet(name="World"):
Keyword argumentsgreet(name="Ada")
Variable argsdef f(*args, **kwargs):
Lambda (anonymous)square = lambda x: x * x
Docstring"""What this does."""

List comprehensions

Build lists, sets, and dicts in a single expression.

OperationSyntax
Map a list[x * 2 for x in nums]
Filter a list[x for x in nums if x > 0]
Map and filter[x * 2 for x in nums if x > 0]
Nested loop[(i, j) for i in a for j in b]
Set comprehension{x % 3 for x in nums}
Dict comprehension{k: v * 2 for k, v in d.items()}

Common built-ins & standard library

Functions and modules you use constantly.

FunctionWhat it does
len(x)Length of a string, list, or dict
range(start, stop, step)Sequence of numbers
sum(nums) / max() / min()Total, largest, smallest
sorted(items)Return a new sorted list
zip(a, b)Pair up two iterables
map(f, items) / filter(f, items)Apply / keep by a function
import mathMath functions, e.g. math.sqrt(9)
import randomRandom values, e.g. random.randint(1, 6)

The Python syntax, data types, and built-ins you reach for most, on one page. This Python cheat sheet is a quick reference for everyday Python 3 - printing, strings and f-strings, lists and dictionaries, control flow, functions, and comprehensions.

Everything here is standard Python 3 that runs anywhere. Copy what you need, or try any snippet live in the Python playground - a real interpreter in your browser, nothing to install.

Python cheat sheet FAQ

Is this Python cheat sheet free?
Yes. This Python cheat sheet is completely free, with no sign-up required. Bookmark it and come back whenever you need to look up syntax, a method, or a built-in.
What is the difference between a list and a tuple in Python?
A list is mutable - you can append, remove, and reassign its items - and is written with square brackets: [1, 2, 3]. A tuple is immutable, written with parentheses: (1, 2, 3), so once created it cannot change. Use a list for a collection that grows or changes, and a tuple for fixed groups of values like coordinates or a row of data.
What is an f-string in Python?
An f-string is a string literal prefixed with f that lets you embed expressions directly inside curly braces, like f"Hello {name}, you have {count} messages". Python evaluates each expression and inserts its value. They are the clearest and fastest way to format strings in Python 3.6+.
Can I practice Python online?
Yes. Open the Python playground to run any snippet from this cheat sheet in your browser - a real interpreter, nothing to install. When you want structure, Coddy's free interactive Python course takes you from variables and loops to functions and comprehensions step by step.
Is this cheat sheet good for beginners?
Yes. It is organized from the basics (printing, data types, strings) down to comprehensions and the standard library, so you can use the top sections on day one and grow into the rest.
Coddy programming languages illustration

Learn Python with Coddy

GET STARTED