Python Cheatsheet
Python Cheatsheet
Real Python Python is dynamically typed It’s recommended to use double-quotes for strings Arithmetic Operators
Pocket Reference Use None to represent missing or optional values Use "\n" to create a line break in a string
10 + 3 # 13
Use type() to check object type To write a backslash in a normal string, write "\\" 10 - 3 # 7
Visit realpython.com to Check for a specific type with isinstance() 10 * 3 # 30
issubclass() checks if a class is a subclass Creating Strings 10 / 3 # 3.3333333333333335
turbocharge your 10 // 3 # 3
single = 'Hello'
Python learning with Type Investigation double = "World"
10 % 3
2 ** 3
#
#
1
8
in‑depth tutorials, type(42) # <class 'int'> multi = """Multiple
line string"""
real‑world examples, type(3.14)
type("Hello")
#
#
<class
<class
'float'>
'str'> Useful Functions
and expert guidance. type(True) # <class 'bool'>
String Operations abs(-5) # 5
type(None) # <class 'NoneType'>
round(3.7) # 4
greeting = "me" + "ow!" # "meow!" round(3.14159, 2) # 3.14
isinstance(3.14, float) # True
repeat = "Meow!" * 3 # "Meow!Meow!Meow!" min(3, 1, 2) # 1
issubclass(int, object) # True - everything inherits from object
length = len("Python") # 6 max(3, 1, 2) # 3
Getting Started sum([1, 2, 3]) # 6
Type Conversion String Methods
Follow these guides to kickstart your Python journey: int("42") # 42 "a".upper() # "A" Learn More on realpython.com/search:
realpython.com/what-can-i-do-with-python float("3.14") # 3.14 "A".lower() # "a" math ∙ operators ∙ built in functions
realpython.com/installing-python str(42) # "42" " a ".strip() # "a"
realpython.com/python-first-steps bool(1) # True "abc".replace("bc", "ha") # "aha"
list("abc") # ["a", "b", "c"] "a b".split() # ["a", "b"] Conditionals
"-".join(["a", "b"]) # "a-b"
Start the Interactive Shell
Learn More on realpython.com/search: Python uses indentation for code blocks
$ python data types ∙ type checking ∙ isinstance ∙ issubclass String Indexing & Slicing Use 4 spaces per indentation level
text = "Python"
Quit the Interactive Shell text[0] # "P" (first) If-Elif-Else
Variables & Assignment
>>> exit() text[-1] # "n" (last) if age < 13:
text[1:4] # "yth" (slice) category = "child"
Variables are created when first assigned
text[:3] # "Pyt" (from start) elif age < 20:
Run a Script Use descriptive variable names text[3:] # "hon" (to end) category = "teenager"
Follow snake_case convention text[::2] # "Pto" (every 2nd) else:
$ python my_script.py
text[::-1] # "nohtyP" (reverse) category = "adult"
Basic Assignment
Run a Script in Interactive Mode
name = "Leo" # String String Formatting Comparison Operators
$ python -i my_script.py age = 7 # Integer # f-strings
height = 5.6 # Float x == y # Equal to
name = "Aubrey"
is_cat = True # Boolean x != y # Not equal to
Learn More on realpython.com/search: age = 2
x < y # Less than
flaws = None # None type f"Hello, {name}!" # "Hello, Aubrey!"
interpreter ∙ run a script ∙ command line f"{name} is {age} years old" # "Aubrey is 2 years old" x <= y # Less than or equal
f"Debug: {age=}" # "Debug: age=2" x > y # Greater than
Parallel & Chained Assignments x >= y # Greater than or equal
Comments # Format method
x, y = 10, 20 # Assign multiple values template = "Hello, {name}! You're {age}."
a = b = c = 0 # Give same value to multiple variables template.format(name="Aubrey", age=2) # "Hello, Aubrey! You're 2." Logical Operators
Always add a space after the #
Use comments to explain “why” of your code if age >= 18 and has_car:
Augmented Assignments Raw Strings print("Roadtrip!")
Write Comments counter += 1 # Normal string with an escaped tab
if is_weekend or is_holiday:
numbers += [4, 5] "This is:\tCool." # "This is: Cool."
# This is a comment print("No work today.")
permissions |= write
# print("This code will not run.") # Raw string with escape sequences
print("This will run.") # Comments are ignored by Python if not is_raining:
r"This is:\tCool." # "This is:\tCool." print("You can go outside.")
Learn More on realpython.com/search:
Learn More on realpython.com/search: variables ∙ assignment operator ∙ walrus operator
Learn More on realpython.com/search:
Learn More on realpython.com/search:
comment ∙ documentation strings ∙ string methods ∙ slice notation ∙ raw strings
conditional statements ∙ operators ∙ truthy falsy