Python Cheatsheet 🚀
1️⃣ Variables & Data Types
x = 10 (Integer)
y = 3.14 (Float)
name = “Python” (String)
is_valid = True (Boolean)
items = [1, 2, 3] (List)
data = (1, 2, 3) (Tuple)
person = {“name”: “Alice”, “age”: 25} (Dictionary)
2️⃣ Operators
Arithmetic: +, -, *, /, //, %, **
Comparison: ==, !=, >, <, >=, <=
Logical: and, or, not
Membership: in, not in
3️⃣ Control Flow
If-Else:
if age > 18:
print(“Adult”)
elif age == 18:
print(“Just turned 18”)
else:
print(“Minor”)
Loops:
for i in range(5):
print(i)
while x < 10:
x += 1
4️⃣ Functions
Defining & Calling:
def greet(name):
return f”Hello, {name}”
print(greet(“Alice”))
Lambda Functions: add = lambda x, y: x + y
5️⃣ Lists & Dictionary Operations
Append: items.append(4)
Remove: items.remove(2)
List Comprehension: [x**2 for x in range(5)]
Dictionary Access: person[“name”]
6️⃣ File Handling
Read File:
with open(“file.txt”, “r”) as f:
content = f.read()
Write File:
with open(“file.txt”, “w”) as f:
f.write(“Hello, World!”)
7️⃣ Exception Handling
try:
result = 10 / 0
except ZeroDivisionError:
print(“Cannot divide by zero!”)
finally:
print(“Done”)
8️⃣ Modules & Packages
Importing:
import math
print(math.sqrt(25))
Creating a Module (mymodule.py):
def add(x, y):
return x + y
Usage: from mymodule import add
9️⃣ Object-Oriented Programming (OOP)
Defining a Class:
class Person:
def init(self, name, age):
self.name = name
self.age = age
def greet(self):
return f”Hello, my name is {self.name}”
Creating an Object: p = Person(“Alice”, 25)
🔟 Useful Libraries
NumPy: import numpy as np
Pandas: import pandas as pd
Matplotlib: import matplotlib.pyplot as plt
Requests: import requests
Python Resources: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L
ENJOY LEARNING 👍👍

LET’S KEEP IN TOUCH!

We’d love to keep you updated with our latest news and offers

We don’t spam! Read our privacy policy for more info.