cheat sheet for python
Here is a concise cheat sheet for Python programming covering essential concepts for beginners
and developers:
Python Cheat Sheet
Basics
Print statement
print("Hello, World!")
Comments
Single line: # comment
Multi-line: ''' comment '''
Variables and Data Types
Declare variables without type:
x = 10
name = "Alice"
Common data types: int, float, str, bool, list, tuple, dict, set
Data Structures
List: my_list = [1, 2, 3]
Tuple: my_tuple = (1, 2, 3)
Dictionary: my_dict = {'key': 'value'}
Set: my_set = {1, 2, 3}
Control Flow
If-else
if x > 0:
print("Positive")
else:
print("Non-positive")
For loop
for i in range(5):
print(i)
While loop
while x < 5:
print(x)
x += 1
Functions
Define and call
def add(a, b):
return a + b
result = add(5, 3)
Classes and Objects
Simple class
class Car:
def __init__(self, color):
self.color = color
def drive(self):
print("Driving")
my_car = Car("red")
my_car.drive()
Exception Handling
Try-except block
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
Common Libraries
math – mathematical functions
os – operating system interface
sys – system-specific parameters
datetime – date and time
This cheat sheet covers the core Python fundamentals for quick reference and programming
basics.