All Cheat Sheets
Py

Python Cheat Sheet

Basics

PrintOutput to console
print("Hello, World!")
VariablesDynamic typing, no declaration needed
name = "Mishu"
age = 25
pi = 3.14
CommentsSingle and multi-line
# This is a comment
"""
Multi-line string
as a comment
"""
InputRead user input
name = input("Enter your name: ")

Data Types

StringsText data
s = "hello"
s.upper()
s.replace("h", "j")
ListsOrdered, mutable sequence
nums = [1, 2, 3]
nums.append(4)
nums[0]  # 1
TuplesOrdered, immutable sequence
point = (3, 4)
x, y = point
DictionariesKey-value pairs
d = {"name": "Mishu", "age": 25}
d["name"]
d.get("key", "default")
SetsUnordered unique elements
s = {1, 2, 3}
s.add(4)
{1, 2} | {2, 3}  # union

Control Flow

If-elif-elseConditional execution
if x > 0:
    print("positive")
elif x == 0:
    print("zero")
else:
    print("negative")
For loopIterate over a sequence
for i in range(5):
    print(i)

for k, v in d.items():
    print(k, v)
While loopLoop while condition is true
while x > 0:
    x -= 1
List comprehensionConcise list creation
squares = [x**2 for x in range(10) if x % 2 == 0]

Functions

DefineFunction definition using def
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"
LambdaAnonymous function
double = lambda x: x * 2
list(map(lambda x: x**2, [1, 2, 3]))
*args, **kwargsVariable arguments
def func(*args, **kwargs):
    print(args, kwargs)