Python Cheatsheet
Python Cheatsheet for Beginners
Table of Contents
- Introduction
- Basics
- Data Types
- Control Flow
- Functions
- Modules
- File Handling
- Object-Oriented Programming
- Error Handling
- Common Built-in Functions
- Advanced Topics
Introduction
Python is a high-level, versatile programming language known for its simplicity and readability. You can use Python for web development, data analysis, artificial intelligence, and much more.
Basics
Printing and Comments
Printing is used to display output:
print("Hello, World!") # This prints Hello, World! to the screen
Comments are notes for the programmer and ignored by Python:
# This is a single-line comment
'''
This is a
multi-line comment
'''
Variables
Variables store data. You don’t need to specify the type of a variable.
x = 5 # Integer
y = 3.14 # Float
name = "John" # String
is_happy = True # Boolean
# You can print variables like this:
print("Name:", name)
Rules for Variable Names
- Must start with a letter or an underscore.
- Can only contain letters, numbers, and underscores.
- Case-sensitive (
Nameandnameare different).
Input
The input function takes user input as a string:
user_name = input("What is your name? ")
print(f"Nice to meet you, {user_name}!")
Data Types
Python has several data types to store different kinds of data.
Numbers
a = 10 # Integer
b = 3.14 # Float
c = 2 + 3j # Complex number
Strings
Strings are text data enclosed in quotes:
text = "Hello"
print(text.upper()) # Convert to uppercase
print(text[1]) # Access character at index 1
String Concatenation:
first = "Hello"
second = "World"
combined = first + " " + second
print(combined)
String Formatting:
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Lists
Lists are ordered collections of items.
numbers = [1, 2, 3, 4]
print(numbers[0]) # Access first item
numbers.append(5) # Add an item
numbers.pop() # Remove last item
print(numbers)
Dictionaries
Dictionaries store data in key-value pairs.
person = {"name": "Alice", "age": 25}
print(person["name"]) # Access value by key
person["age"] = 26 # Update value
print(person)
Tuples
Tuples are immutable lists (cannot be changed after creation).
coordinates = (10, 20)
print(coordinates[0])
Sets
Sets are collections of unique items.
unique_items = {1, 2, 3, 2}
unique_items.add(4)
print(unique_items) # Output: {1, 2, 3, 4}
Control Flow
Control flow determines the order in which code runs.
If-Else
age = 18
if age >= 18:
print("You are an adult.")
elif age > 13:
print("You are a teenager.")
else:
print("You are a child.")
Loops
For Loop:
for i in range(5):
print(i) # Outputs 0, 1, 2, 3, 4
While Loop:
count = 0
while count < 5:
print(count)
count += 1
Functions
Functions group reusable code.
Defining Functions
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Default Parameters
def greet(name="stranger"):
return f"Hello, {name}!"
print(greet()) # Outputs: Hello, stranger!
Modules
Modules are libraries of code you can import.
Importing Modules
import math
print(math.sqrt(16))
Installing External Modules
Use pip to install libraries:
pip install requests
File Handling
Reading and Writing Files
# Write to a file
with open("example.txt", "w") as file:
file.write("Hello, file!")
# Read from a file
with open("example.txt", "r") as file:
content = file.read()
print(content)
Object-Oriented Programming
OOP allows you to model real-world things using classes and objects.
Classes and Objects
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
return f"{self.name} says Woof!"
dog = Dog("Buddy", "Golden Retriever")
print(dog.bark())
Error Handling
Handle errors gracefully with try-except blocks.
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
finally:
print("Done!")
Common Built-in Functions
Math Functions
max([1, 2, 3]) # Find max
min([1, 2, 3]) # Find min
abs(-10) # Absolute value
sum([1, 2, 3]) # Sum of elements
String Functions
len("hello") # String length
"hello".replace("e", "a") # Replace characters
Advanced Topics
List Comprehensions
squares = [x**2 for x in range(5)]
print(squares)
Lambda Functions
double = lambda x: x * 2
print(double(5))