← Back to Home

Python Cheat Sheet

Quick reference for Python syntax, formulas, and important concepts

Operators

Arithmetic Operators

Addition, subtraction, multiplication, division, floor division, modulo, exponentiation

SYNTAX

+ - * / // % **

EXAMPLE

10 + 5 = 15, 10 / 3 = 3.333..., 10 // 3 = 3, 10 % 3 = 1, 2 ** 3 = 8
Operators

Comparison Operators

Used to compare values and return True or False

SYNTAX

== != < > <= >=

EXAMPLE

5 == 5 returns True, 5 != 3 returns True, 5 > 3 returns True
Operators

Logical Operators

Combine or invert boolean values

SYNTAX

and or not

EXAMPLE

True and False = False, True or False = True, not True = False
Data Types

String Methods

Common string manipulation methods

SYNTAX

upper() lower() strip() split() join() replace() find()

EXAMPLE

"hello".upper() = "HELLO", "hello world".split() = ["hello", "world"]
Data Types

List Methods

Common list manipulation methods

SYNTAX

append() extend() insert() remove() pop() clear() sort() reverse()

EXAMPLE

list.append(x) adds element, list.pop() removes last element
Control Flow

If-Else Syntax

Conditional branching

SYNTAX

if condition:
    code
elif condition:
    code
else:
    code

EXAMPLE

if x > 0:
    print("positive")
elif x < 0:
    print("negative")
Control Flow

For Loop

Iterate over sequences

SYNTAX

for var in sequence:
    code

EXAMPLE

for i in range(5):
    print(i)  # prints 0 1 2 3 4
Control Flow

While Loop

Execute while condition is true

SYNTAX

while condition:
    code

EXAMPLE

i = 0
while i < 5:
    print(i)
    i += 1
Functions

Function Definition

Define and call functions

SYNTAX

def function_name(parameters):
    return value

EXAMPLE

def add(a, b):
    return a + b
print(add(3, 5))  # 8
Functions

Lambda Functions

Anonymous small functions

SYNTAX

lambda arguments: expression

EXAMPLE

square = lambda x: x ** 2
print(square(5))  # 25
OOPs

Class Definition

Define classes and objects

SYNTAX

class ClassName:
    def __init__(self, params):
        self.attr = value

EXAMPLE

class Dog:
    def __init__(self, name):
        self.name = name
Built-in Functions

Common Functions

Commonly used built-in functions

SYNTAX

len() type() str() int() float() bool() list() dict() set() tuple()

EXAMPLE

len("hello") = 5, type(5) = <class 'int'>, int("5") = 5
Built-in Functions

Useful Functions

More useful built-in functions

SYNTAX

print() input() range() sum() max() min() sorted() enumerate() zip()

EXAMPLE

sum([1,2,3]) = 6, max([1,5,3]) = 5, min([1,5,3]) = 1
Exception Handling

Try-Except Syntax

Handle runtime errors

SYNTAX

try:
    code
except Exception:
    handle
finally:
    cleanup

EXAMPLE

try:
    x = 1/0
except ZeroDivisionError:
    print("Cannot divide by zero")

📝 Print Tips:

  • • Click "Print" button to open your browser's print dialog
  • • Use "Ctrl+P" or "Cmd+P" to print with custom settings
  • • The cheat sheet is optimized for printing on A4 paper
  • • Filter by category before printing to reduce pages