🐍

Python Cheat Sheet

Beginner basics β€” everything from your first print() to strings and f-strings. Copy-paste the examples and learn by doing.

Jump to: First programprint()VariablesData types InputType conversionMathStrings Slicingf-stringsString methods Comparisonif / elif / elseLogical ops While loopsFor loopsLists List methods2D listsTuples UnpackingDictionariesFunctions CommentsClassesConstructors InheritanceModulesPackagesRandom Format specifiersDefault args*args / **kwargs ScopeComprehensionsSets ExceptionsFilesmatch-caseDunder methods Installing librariesExcel automationMachine learning

Your first program

Save a file as app.py, then run it. Python runs your code line by line, from the top.

print("Hello, world!")
print("My name is Mosh")

print()

print("Hello")        # text (a "string")
print("*" * 10)        # ********** (repeat a string)
print("A", "B", "C")  # A B C   (multiple values)

Variables

A variable is a labelled box that stores a value in memory. = assigns a value.

price = 10          # create / set
price = 20          # reset to a new value
print(price)        # 20
πŸ“ Naming: use lowercase, separate words with _ (e.g. first_name), and pick descriptive names. Python is case-sensitive (Price β‰  price).

Data types

TypeExampleWhat it is
intage = 20Whole number
floatrating = 4.9Number with a decimal
strname = "Mosh"Text (a string)
boolis_new = TrueTrue / False (capital T/F!)
name = "Mosh"
age = 20
is_new = True
print(type(age))   # <class 'int'> β€” check a type

Receiving input

input() shows a prompt and returns whatever the user types β€” always as a string.

name = input("What is your name? ")
print("Hi " + name)   # join strings with +

Type conversion

Because input() gives a string, convert it before doing math.

birth_year = input("Birth year: ")
age = 2026 - int(birth_year)  # int() turns "1982" into 1982
print(age)
FunctionConverts to
int(x)integer
float(x)decimal number
str(x)string
bool(x)True / False
⚠️ "1982" (string) is not the same as 1982 (number). Mixing them gives a TypeError β€” convert first.

Math operators

OpMeaningExample
+ - * /add, subtract, multiply, divide10 / 3 β†’ 3.33
//integer (floor) division10 // 3 β†’ 3
%remainder (modulus)10 % 3 β†’ 1
**power2 ** 3 β†’ 8

Strings

Use single or double quotes. Use the other kind when your text contains a quote.

a = 'Python'
b = "Python's course"     # ' inside, so use "
c = 'He said "hi"'          # " inside, so use '
msg = """Hi John,
Thanks for joining.
The Team"""                 # triple quotes = multi-line

Indexing & slicing

Characters are numbered from 0. Negative numbers count from the end.

course = "Python for Beginners"
course[0]      # 'P'  (first char)
course[-1]     # 's'  (last char)
course[0:3]    # 'Pyt' (index 0,1,2 β€” stop is excluded)
course[1:]     # 'ython for Beginners' (to the end)
course[:5]     # 'Pytho' (from the start)
course[:]      # a full copy of the string
πŸŽ“ The [start:stop] slice includes start, excludes stop. This shows up on a lot of Python tests!

Formatted strings (f-strings)

Prefix with f and drop variables into { } β€” much cleaner than joining with +.

first = "John"
last = "Smith"
msg = f"{first} [{last}] is a coder"
print(msg)   # John [Smith] is a coder

String methods

Methods belong to a value and are called with a dot: course.upper(). They return a new string (the original is unchanged).

course = "Python for Beginners"
len(course)              # 20  (length β€” a general function)
course.upper()           # 'PYTHON FOR BEGINNERS'
course.lower()           # 'python for beginners'
course.title()           # 'Python For Beginners'
course.strip()           # remove spaces at the ends
course.find("o")         # 4   (index of first match, -1 if none)
course.replace("Beginners", "Pros")
"Python" in course      # True  (does it contain this?)

Comparison operators

These compare two values and produce a boolean (True/False).

OpMeansExample
==equal totemp == 30
!=not equalname != "Mosh"
> >=greater / or equaltemp > 30
< <=less / or equalage <= 18
⚠️ == compares, = assigns. temp = 30 sets a value; temp == 30 asks a question.

if / elif / else

Run code only when a condition is true. The indented block belongs to the if. elif = "otherwise if", else = "otherwise".

temp = 35
if temp > 30:
    print("It's a hot day")
    print("Drink water")
elif temp < 10:
    print("It's a cold day")
else:
    print("It's a lovely day")
# Example: down payment depends on credit
price = 1_000_000
has_good_credit = True
if has_good_credit:
    down = 0.1 * price          # 10%
else:
    down = 0.2 * price          # 20%
print(f"Down payment: ${down}")

Logical operators

Combine conditions: and (both true), or (at least one true), not (flips True↔False).

if has_high_income and has_good_credit:
    print("Eligible for a loan")      # both must be True

if has_high_income or has_good_credit:
    print("Eligible")               # at least one True

if has_good_credit and not has_criminal_record:
    print("Eligible")               # not False -> True

While loops

Repeat a block while a condition stays true. Always change something inside, or you get an infinite loop.

i = 1
while i <= 5:
    print(i)
    i += 1          # same as i = i + 1
print("Done")         # 1 2 3 4 5 Done

break jumps out of a loop early. A while … else runs the else only if the loop finished without a break:

secret = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
    guess = int(input("Guess: "))
    guess_count += 1
    if guess == secret:
        print("You won!")
        break
else:
    print("Sorry, you failed")

For loops & range()

A for loop goes through each item in a collection (a string, a list, a range…).

for letter in "Python":
    print(letter)      # P y t h o n (each on a line)

for name in ["Mosh", "John", "Sarah"]:
    print(name)

for i in range(5):      # 0 1 2 3 4 (stop excluded)
    print(i)
range()Produces
range(5)0, 1, 2, 3, 4
range(5, 10)5, 6, 7, 8, 9
range(5, 10, 2)5, 7, 9 (step of 2)

Lists

A list holds many values in [ ]. Loop over it to process every item.

prices = [10, 20, 30]
total = 0
for price in prices:
    total += price
print(total)        # 60

Find the largest number

numbers = [3, 6, 2, 8, 4, 10]
max = numbers[0]
for n in numbers:
    if n > max:
        max = n
print(max)        # 10

List methods

Operations you can do on a list (call them with a dot):

numbers = [5, 2, 1, 7, 4]
numbers.append(20)        # add to the end
numbers.insert(0, 10)     # add at an index
numbers.remove(5)         # remove a value
numbers.pop()              # remove the last item
numbers.clear()           # remove everything
numbers.index(7)          # position of a value (error if missing)
7 in numbers              # True/False β€” safer existence check
numbers.count(5)          # how many times 5 appears
numbers.sort()            # sort ascending (in place)
numbers.reverse()         # reverse the order
copy = numbers.copy()     # an independent copy

Remove duplicates

numbers = [2, 2, 4, 6, 6, 3, 1]
uniques = []
for n in numbers:
    if n not in uniques:
        uniques.append(n)
print(uniques)     # [2, 4, 6, 3, 1]

2D lists (a list of lists)

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]
matrix[0][1]            # 2  (row 0, column 1)
matrix[0][1] = 20       # change a cell
for row in matrix:      # nested loops
    for item in row:
        print(item)

Tuples

Like a list, but immutable β€” you can't add, remove or change items. Use ( ).

point = (1, 2, 3)
point[0]            # 1  (reading is fine)
point[0] = 10       # ❌ TypeError β€” tuples can't change
# only .count() and .index() are available

Unpacking

Assign several variables from a list/tuple in one line.

coordinates = (1, 2, 3)
x, y, z = coordinates    # x=1, y=2, z=3
print(x, y, z)

Dictionaries

Store key β†’ value pairs in { }. Keys must be unique.

customer = {
    "name": "John Smith",
    "age": 30,
    "is_verified": True,
}
customer["name"]                 # 'John Smith'
customer.get("birthdate")         # None if missing (no error)
customer.get("birthdate", "N/A")  # a default value
customer["name"] = "Jack"        # update
customer["phone"] = "1234"        # add a new pair
πŸ’‘ dict[key] errors if the key is missing; dict.get(key, default) is safer. "good morning :)".split(" ") turns a string into a list of words β€” handy with dictionaries (emoji converters, phone-number spellers, etc.).

Functions

Group reusable code with def. Define before you call. Parameters are the placeholders; arguments are the values you pass.

def greet_user(first_name, last_name):
    print(f"Hi {first_name} {last_name}")
    print("Welcome aboard")

greet_user("John", "Smith")              # positional args
greet_user(last_name="Smith", first_name="John")  # keyword args (order-free)

Return a value

Use return to send a result back. A function with no return gives None.

def square(number):
    return number * number

result = square(3)
print(result)      # 9
πŸ“Œ Rules: keyword arguments must come after positional ones. Add two blank lines after a function (PEP 8 style). Use descriptive names like calculate_cost.

Comments

Lines starting with # are ignored by Python. Use them to explain why (not what) β€” and don't overdo it.

# Tax rate assumed at 10% for 2026
price = 100  # base price before tax

Classes & objects

A class defines a new type (a blueprint). An object is an instance of it. Class names use PascalCase. Every method's first parameter is self (the current object).

class Point:
    def move(self):
        print("move")
    def draw(self):
        print("draw")

point1 = Point()        # create an object (instance)
point1.draw()           # call a method
point1.x = 10          # attributes = data on the object
print(point1.x)        # 10

Constructors (__init__)

__init__ runs automatically when you create an object β€” use it to set up (initialize) attributes so they always exist.

class Person:
    def __init__(self, name):
        self.name = name           # self = this object
    def talk(self):
        print(f"Hi, I am {self.name}")

john = Person("John Smith")     # name is passed to __init__
john.talk()                    # Hi, I am John Smith

Inheritance

A class can reuse another class's methods by inheriting from it β€” avoids repeating code (DRY). Use pass for an empty body.

class Mammal:
    def walk(self):
        print("walk")

class Dog(Mammal):     # Dog inherits walk()
    def bark(self):
        print("bark")

class Cat(Mammal):     # Cat inherits walk() too
    pass

dog1 = Dog()
dog1.walk()              # inherited
dog1.bark()              # Dog's own method

Modules

A module is just a .py file. Split related functions/classes into modules, then import them.

# converters.py has kg_to_lbs()
import converters
converters.kg_to_lbs(70)

# or import just what you need:
from converters import kg_to_lbs
kg_to_lbs(70)          # no prefix needed

Packages

A package is a folder of modules (it contains an __init__.py file). Import using dots.

# ecommerce/shipping.py has calculate_shipping()
import ecommerce.shipping
ecommerce.shipping.calculate_shipping()

from ecommerce.shipping import calculate_shipping
from ecommerce import shipping   # import the whole module

Random values (a built-in module)

Python ships with a big standard library. random is one example β€” no install needed.

import random
random.random()             # a float 0.0–1.0
random.randint(1, 6)         # whole number 1–6 (like a die)
random.choice(["Jon", "Mary", "Bob"])  # pick a random item
# Dice class that rolls two dice -> a tuple
import random
class Dice:
    def roll(self):
        return random.randint(1, 6), random.randint(1, 6)

dice = Dice()
print(dice.roll())      # e.g. (3, 5)

Format specifiers (pretty f-strings)

Inside an f-string, add : and a format spec to control how a value looks.

pi = 3.14159
name = "Mosh"
f"{pi:.2f}"          # '3.14'   (2 decimal places)
f"{1000000:,}"      # '1,000,000'  (thousands commas)
f"{0.25:.0%}"       # '25%'    (percent)
f"{name:>10}"       # right-align in 10 spaces
f"{name:^10}"       # center-align

Default & keyword arguments

Give a parameter a default so callers can skip it. Pass by name for clarity.

def greet(name, greeting="Hello"):   # greeting has a default
    print(f"{greeting}, {name}")

greet("Mosh")                 # Hello, Mosh
greet("Mosh", "Hi")           # Hi, Mosh
greet(greeting="Hey", name="Bob")  # keyword args (any order)

*args & **kwargs (any number of arguments)

def add(*numbers):       # *args  -> a tuple of all positional args
    return sum(numbers)
add(1, 2, 3, 4)          # 10

def profile(**info):     # **kwargs -> a dict of keyword args
    print(info)
profile(name="Mosh", age=30)  # {'name': 'Mosh', 'age': 30}

Variable scope

Variables made inside a function are local (gone when it ends). Use global to change a top-level variable from inside a function.

total = 0              # global
def add():
    global total      # without this you'd make a NEW local 'total'
    total += 1

List comprehensions

A short way to build a list from another sequence β€” one line instead of a loop.

nums = [1, 2, 3, 4, 5]
squares = [n * n for n in nums]            # [1, 4, 9, 16, 25]
evens = [n for n in nums if n % 2 == 0]    # [2, 4]

Sets

An unordered collection of unique items, in { }. Great for removing duplicates.

s = {1, 2, 3, 3, 2}       # {1, 2, 3}  (dupes dropped)
s.add(4)
{1, 2, 3} & {2, 3, 4}     # {2, 3}     intersection
{1, 2} | {3, 4}        # {1,2,3,4}  union
{1, 2, 3} - {2}        # {1, 3}     difference
set([1, 1, 2])           # {1, 2}  remove duplicates from a list

Exception handling (try / except)

Catch errors so your program doesn't crash. else runs if there was no error; finally always runs.

try:
    age = int(input("Age: "))
    print(100 / age)
except ValueError:
    print("Please enter a number")
except ZeroDivisionError:
    print("Age can't be zero")
else:
    print("No errors!")
finally:
    print("Done")             # always runs

File handling

Use with open(...) β€” it closes the file for you. Modes: "r" read, "w" write (overwrites), "a" append.

with open("notes.txt", "w") as f:
    f.write("Hello\n")
    f.write("Second line\n")

with open("notes.txt", "r") as f:
    content = f.read()        # whole file as one string
    # for line in f:  print(line)   # or line by line

match-case (Python 3.10+)

A clean alternative to a long if / elif chain.

command = "start"
match command:
    case "start":
        print("Starting…")
    case "stop":
        print("Stopping…")
    case _:                # _ = anything else (default)
        print("Unknown command")

Magic / dunder methods

Special methods named with double underscores let your objects work with built-in syntax (printing, ==, etc.).

class Point:
    def __init__(self, x):
        self.x = x
    def __str__(self):           # controls print(p)
        return f"Point({self.x})"
    def __eq__(self, other):      # controls p1 == p2
        return self.x == other.x

p = Point(5)
print(p)               # Point(5)
print(p == Point(5))    # True

Installing third-party libraries

Beyond the standard library, install packages from PyPI with pip (run in your terminal, not in Python).

pip install openpyxl
pip install pandas scikit-learn jupyter
πŸ’‘ For data/ML work, Anaconda bundles Python + Jupyter + pandas/numpy/scikit-learn so you don't install them one by one. You write ML code in a Jupyter Notebook (cells you run one at a time) β€” great for inspecting data.

Project: automate Excel (openpyxl)

Read a spreadsheet, change values, and save β€” perfect for boring repetitive tasks across thousands of files.

import openpyxl as xl
from openpyxl.chart import BarChart, Reference

wb = xl.load_workbook("transactions.xlsx")
sheet = wb.active                 # or wb["Sheet1"]

cell = sheet.cell(1, 1)          # row 1, col 1  (or sheet["a1"])
print(cell.value)

for row in range(2, sheet.max_row + 1):   # skip header row 1
    price = sheet.cell(row, 3).value
    corrected = price * 0.9           # 10% off
    sheet.cell(row, 4).value = corrected  # write into a new column

wb.save("transactions2.xlsx")

Add a chart

values = Reference(sheet, min_row=2, max_row=sheet.max_row, min_col=4, max_col=4)
chart = BarChart()
chart.add_data(values)
sheet.add_chart(chart, "e2")        # top-left corner of the chart
🧹 Pro tip from the course: wrap it in a process_workbook(filename) function, then loop over every file in a folder to update thousands of spreadsheets in seconds.

Project: machine learning (pandas + scikit-learn)

The ML workflow: import β†’ prepare β†’ train β†’ predict. Example: predict the music genre someone likes from their age & gender.

import pandas as pd
from sklearn.tree import DecisionTreeClassifier

# 1. import data (a CSV -> a DataFrame, like a spreadsheet)
music = pd.read_csv("music.csv")
music.shape          # (rows, columns)
music.describe()     # quick stats per column

# 2. prepare: split into input (X) and output (y)
X = music.drop(columns=["genre"])   # everything except the answer
y = music["genre"]                  # the answer column

# 3. build & train a model
model = DecisionTreeClassifier()
model.fit(X, y)

# 4. predict (21-yr-old male, 22-yr-old female)
predictions = model.predict([[21, 1], [22, 0]])
print(predictions)   # e.g. ['HipHop' 'Dance']
πŸ“Š Common ML libraries: pandas (data frames), numpy (arrays), matplotlib (plots), scikit-learn (algorithms like decision trees). Always split data into a training set and a testing set, then measure your model's accuracy.
πŸŽ‰ That's the entire Python course on one page β€” from print() to OOP, modules, automation and machine learning. You're ready to build real things! Bookmark this page and come back whenever you need a quick reminder.

← Back to all cheat sheets