๐Ÿ Python Quick Reference

Essential Python syntax, functions, and code snippets

Quick Jump:

Basics

Variables and Types

# Variables (dynamic typing)
x = 5           # int
y = 3.14        # float
name = 'Alice'  # str
is_valid = True # bool

# Type conversion
int('123')      # => 123
str(123)        # => '123'
float('3.14')   # => 3.14
โ–ถ Run

String Operations

# String methods
text = 'Hello, World!'
text.upper()         # => 'HELLO, WORLD!'
text.lower()         # => 'hello, world!'
text.replace('World', 'Python')  # => 'Hello, Python!'
text.split(', ')     # => ['Hello', 'World!']

# F-strings (Python 3.6+)
name = 'Alice'
age = 30
f'My name is {name} and I am {age} years old'
โ–ถ Run

Data Structures

Lists

# Creating lists
fruits = ['apple', 'banana', 'cherry']
numbers = [1, 2, 3, 4, 5]

# List methods
fruits.append('orange')     # Add to end
fruits.insert(1, 'mango')   # Insert at index
fruits.remove('banana')     # Remove by value
fruits.pop()                # Remove last item
fruits.sort()               # Sort in place
len(fruits)                 # Length
โ–ถ Run

Dictionaries

# Creating dictionaries
person = {
    'name': 'Alice',
    'age': 30,
    'city': 'New York'
}

# Dictionary operations
person['email'] = 'alice@example.com'  # Add/update
person.get('phone', 'N/A')             # Get with default
person.keys()                          # Get all keys
person.values()                        # Get all values
person.items()                         # Get key-value pairs
โ–ถ Run

Functions

Function Definition

# Basic function
def greet(name):
    return f'Hello, {name}!'

# Function with default parameter
def power(base, exponent=2):
    return base ** exponent

# Multiple return values
def get_stats(numbers):
    return min(numbers), max(numbers), sum(numbers)
โ–ถ Run

Lambda Functions

# Lambda (anonymous) functions
square = lambda x: x ** 2
add = lambda a, b: a + b

# Using with map/filter
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
evens = list(filter(lambda x: x % 2 == 0, numbers))
โ–ถ Run

Comprehensions

List Comprehensions

# Basic list comprehension
squares = [x**2 for x in range(10)]

# With condition
evens = [x for x in range(20) if x % 2 == 0]

# Nested comprehension
matrix = [[i*j for j in range(5)] for i in range(5)]
โ–ถ Run

Dictionary Comprehensions

# Dictionary comprehension
squares_dict = {x: x**2 for x in range(10)}

# From two lists
keys = ['a', 'b', 'c']
values = [1, 2, 3]
result = {k: v for k, v in zip(keys, values)}
โ–ถ Run

File I/O

Reading Files

# Read entire file
with open('file.txt', 'r') as f:
    content = f.read()

# Read line by line
with open('file.txt', 'r') as f:
    for line in f:
        print(line.strip())

# Read all lines into list
with open('file.txt', 'r') as f:
    lines = f.readlines()
โ–ถ Run

Writing Files

# Write to file (overwrites)
with open('file.txt', 'w') as f:
    f.write('Hello, World!')

# Append to file
with open('file.txt', 'a') as f:
    f.write('New line\n')

# Write multiple lines
lines = ['Line 1', 'Line 2', 'Line 3']
with open('file.txt', 'w') as f:
    f.writelines('\n'.join(lines))
โ–ถ Run

Discover another handy tool from EditPDF.pro