DEV Community

Cover image for Python Cheat Sheet for Beginners (2025 Edition)
Nivesh Bansal
Nivesh Bansal

Posted on

Python Cheat Sheet for Beginners (2025 Edition)

✨ Whether you're just starting your Python journey or need a quick refresher, this cheat sheet is your go-to guide in 2025. Clear, simple, and beginner-friendly!

🧠 1. Basic Python Syntax

➤ Printing Something

print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

➤ Comments

# This is a single-line comment

"""
This is a
multi-line comment
"""
Enter fullscreen mode Exit fullscreen mode

🔤 2. Variables & Data Types

➤ Variable Assignment

name = "Alice"        # string
age = 25              # integer
height = 5.8          # float
is_student = True     # boolean
Enter fullscreen mode Exit fullscreen mode

➤ Type Checking

print(type(name))     # <class 'str'>
Enter fullscreen mode Exit fullscreen mode

🔁 3. Conditionals

if age >= 18:
    print("Adult")
elif age >= 13:
    print("Teen")
else:
    print("Child")
Enter fullscreen mode Exit fullscreen mode

🔄 4. Loops

➤ For Loop

for i in range(5):
    print(i)   # 0 to 4
Enter fullscreen mode Exit fullscreen mode

➤ While Loop

count = 0
while count < 5:
    print(count)
    count += 1
Enter fullscreen mode Exit fullscreen mode

📦 5. Data Structures

➤ List

fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
print(fruits[1])  # banana
Enter fullscreen mode Exit fullscreen mode

➤ Tuple (Immutable)

point = (3, 4)
Enter fullscreen mode Exit fullscreen mode

➤ Set (No duplicates)

unique_nums = {1, 2, 3, 2}
Enter fullscreen mode Exit fullscreen mode

➤ Dictionary (Key-Value)

person = {"name": "Alice", "age": 25}
print(person["name"])
Enter fullscreen mode Exit fullscreen mode

🧮 6. Functions

➤ Defining & Calling Functions

def greet(name):
    print("Hello, " + name)

greet("Nivesh")
Enter fullscreen mode Exit fullscreen mode

🛠️ 7. File Handling

➤ Writing to a File

with open("example.txt", "w") as file:
    file.write("Hello File!")
Enter fullscreen mode Exit fullscreen mode

➤ Reading from a File

with open("example.txt", "r") as file:
    print(file.read())

Enter fullscreen mode Exit fullscreen mode

🧰 8. Useful Built-In Functions

len("hello")       # 5
max([1, 5, 3])     # 5
min([1, 5, 3])     # 1
sorted([3, 1, 2])  # [1, 2, 3]
sum([1, 2, 3])     # 6
Enter fullscreen mode Exit fullscreen mode

⚙️ 9. Error Handling

try:
    x = 5 / 0
except ZeroDivisionError:
    print("You can't divide by zero!")
Enter fullscreen mode Exit fullscreen mode

🧠 10. Object-Oriented Programming (Basics)

class Person:
    def __init__(self, name):
        self.name = name

    def greet(self):
        print("Hi, I'm " + self.name)

p = Person("Krishna")
p.greet()
Enter fullscreen mode Exit fullscreen mode

📌 Bonus: Python Shortcuts and Tricks

# Swap values
a, b = 5, 10
a, b = b, a

# One-line if
status = "Pass" if score >= 50 else "Fail"

# List comprehension
squares = [i**2 for i in range(5)]

Enter fullscreen mode Exit fullscreen mode

Top comments (0)