The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Generate a Python cheat sheet

python
# Python Cheat Sheet 🐍 ############################# # BASIC SYNTAX & VARIABLES ############################# # Comments # This is a comment # Variables x = 5 # Integer y = 3.14 # Float name = "John" # String is_on = True # Boolean # Multiple assignment a, b, c = 1, 2, 3 # Type checking type(x) # <class 'int'> ############################# # DATA TYPES ############################# # Strings greet = "Hello" greet[0] # 'H' greet.upper() # 'HELLO' greet.lower() # 'hello' greet.replace("l", "x") # 'Hexxo' "world" in greet # False # Numbers 2 + 3 # 5 2 ** 3 # 8 (exponent) 5 // 2 # 2 (floor division) 5 % 2 # 1 (modulus) # Lists my_list = [1, 2, 3] my_list.append(4) my_list[0] # 1 my_list[-1] # 4 my_list[1:3] # [2, 3] # Tuples (immutable) my_tuple = (1, 2, 3) # Sets my_set = {1, 2, 2, 3} # {1, 2, 3} my_set.add(4) # Dictionaries person = {"name": "Alice", "age": 30} person["name"] # 'Alice' person.get("age") # 30 person["city"] = "NYC" ############################# # CONTROL FLOW ############################# # If statements if x > 0: print("Positive") elif x == 0: print("Zero") else: print("Negative") # For loop for i in range(5): print(i) # While loop count = 0 while count < 5: print(count) count += 1 # Loop control break # exits the loop continue # skips to the next iteration ############################# # FUNCTIONS ############################# def greet(name): return f"Hello, {name}!" def add(a, b=0): return a + b # Lambda function square = lambda x: x ** 2 ############################# # CLASSES & OBJECTS ############################# class Dog: def __init__(self, name): self.name = name def speak(self): return f"{self.name} says woof!" fido = Dog("Fido") fido.speak() # 'Fido says woof!' ############################# # EXCEPTIONS ############################# try: 1 / 0 except ZeroDivisionError: print("Cannot divide by zero") finally: print("Done") ############################# # FILE I/O ############################# # Write to file with open("file.txt", "w") as f: f.write("Hello World") # Read from file with open("file.txt", "r") as f: content = f.read() ############################# # COMMON MODULES ############################# import math math.sqrt(16) # 4.0 import random random.randint(1, 10) from datetime import datetime now = datetime.now() import os os.getcwd() # Get current directory ############################# # COMPREHENSIONS ############################# # List comprehension squares = [x**2 for x in range(10)] # Dictionary comprehension nums = {x: x*x for x in range(5)} # Set comprehension unique = {char for char in "hello"} ############################# # ADVANCED TOPICS (BASICS) ############################# # Generators def gen(): for i in range(3): yield i # Decorators def decorator(func): def wrapper(): print("Before") func() print("After") return wrapper @decorator def say_hello(): print("Hello") # Context Managers with open("file.txt") as f: data = f.read() # Type hints def add(a: int, b: int) -> int: return a + b
Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About