Python Basics Cheatsheet
Essential Python syntax and concepts
Python Basics Cheatsheet
Quick reference for Python fundamentals.
Variables & Data Types
# Strings
name = "Soul"
multiline = """This is
a multiline string"""
# Numbers
integer = 42
floating = 3.14
complex_num = 1 + 2j
# Boolean
is_active = True
is_empty = False
# None
nothing = NoneLists & Dictionaries
# Lists
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
first = fruits[0] # "apple"
last = fruits[-1] # "date"
# Dictionary
person = {
"name": "John",
"age": 30,
"city": "NYC"
}
person["email"] = "john@example.com"Control Flow
# If-Else
if x > 10:
print("Large")
elif x > 5:
print("Medium")
else:
print("Small")
# For Loop
for item in fruits:
print(item)
# While Loop
count = 0
while count < 5:
print(count)
count += 1Functions
# Basic function
def greet(name):
return f"Hello, {name}!"
# Lambda
square = lambda x: x ** 2
# Args and Kwargs
def flexible(*args, **kwargs):
print(args) # tuple
print(kwargs) # dictList Comprehensions
# Basic
squares = [x**2 for x in range(10)]
# With condition
evens = [x for x in range(20) if x % 2 == 0]
# Dictionary comprehension
squared_dict = {x: x**2 for x in range(5)}More cheatsheets coming soon! 🚀