Python

Variables & Types

x = 10
y = 3.14
name = "Alice"
flag = True
z = None
a, b = 1, 2

print(type(x))

Strings & f-strings

s = "hello world"
s.upper()
s.split()
s.replace("hello", "hi")
s.startswith("hello")
", ".join(["a", "b", "c"])

name = "Alice"
age = 30
f"{name} is {age} years old"
f"{'centered':^20}"
f"{3.14159:.2f}"

Lists & Tuples

nums = [1, 2, 3]
nums.append(4)
nums.extend([5, 6])
nums.pop()
nums.sort()
nums.reverse()
nums[1:3]
nums[-1]

point = (10, 20)
x, y = point
len(point)

Dicts & Sets

d = {"name": "Alice", "age": 30}
d["name"]
d.get("email", "N/A")
d.keys()
d.values()
d.items()
d.update({"age": 31})

s = {1, 2, 3}
s.add(4)
s.discard(2)
a | b
a & b
a - b

Control Flow

if x > 0:
    print("positive")
elif x == 0:
    print("zero")
else:
    print("negative")

for i in range(10):
    print(i)

for item in iterable:
    if skip_condition:
        continue
    if stop_condition:
        break

while x > 0:
    x -= 1

match value:
    case 1:
        print("one")
    case _:
        print("other")

Functions

def greet(name, greeting="Hello"):
    return f"{greeting}, {name}"

def add(*args, **kwargs):
    return sum(args)

add(1, 2, 3, extra=4)

square = lambda x: x ** 2

def outer(x):
    def inner(y):
        return x + y
    return inner

List/Dict Comprehensions

squares = [x**2 for x in range(10)]
evens = [x for x in range(20) if x % 2 == 0]

matrix = [[1, 2], [3, 4]]
flat = [x for row in matrix for x in row]

word_len = {w: len(w) for w in ["hello", "world"]}
unique = {x for x in [1, 2, 2, 3]}

Classes

class Animal:
    count = 0

    def __init__(self, name):
        self.name = name
        Animal.count += 1

    def speak(self):
        raise NotImplementedError

    def __repr__(self):
        return f"Animal({self.name})"

class Dog(Animal):
    def speak(self):
        return f"{self.name} says Woof!"

d = Dog("Rex")
isinstance(d, Animal)
issubclass(Dog, Animal)

Error Handling

try:
    result = 1 / 0
except ZeroDivisionError as e:
    print(f"Error: {e}")
except (TypeError, ValueError):
    print("Type or value error")
else:
    print("No error")
finally:
    print("Always runs")

raise ValueError("invalid value")

File I/O

with open("file.txt", "r") as f:
    content = f.read()
    lines = f.readlines()

with open("out.txt", "w") as f:
    f.write("hello\n")

import json
data = json.load(open("data.json"))
json.dumps(data, indent=2)

import csv
with open("data.csv") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row)

Modules & Packages

import os
from os.path import join, exists
from collections import Counter, defaultdict

dir(module)
help(module.func)
__name__ == "__main__"

import sys
sys.path.append("/custom/path")

Decorators

import functools

def timer(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        print(f"{func.__name__}: {time.time() - start:.4f}s")
        return result
    return wrapper

@timer
def slow_func():
    time.sleep(1)

@staticmethod
def utility():
    pass

@classmethod
def create(cls, value):
    return cls(value)

@property
def name(self):
    return self._name

Generators

def count_up(n):
    i = 0
    while i < n:
        yield i
        i += 1

gen = (x**2 for x in range(10))

def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

from itertools import count, chain, islice

Context Managers

class ManagedResource:
    def __enter__(self):
        print("acquired")
        return self
    def __exit__(self, *exc):
        print("released")

with ManagedResource() as r:
    pass

from contextlib import contextmanager

@contextmanager
def temp_dir():
    d = tempfile.mkdtemp()
    yield d
    shutil.rmtree(d)

Common Standard Library

from collections import Counter, defaultdict, deque
from datetime import datetime, timedelta
from pathlib import Path
from dataclasses import dataclass
from typing import Optional, List, Dict

p = Path("file.txt")
p.exists()
p.read_text()
p.stem

now = datetime.now()
now.strftime("%Y-%m-%d %H:%M")

Counter("abracadabra").most_common(3)
dd = defaultdict(list)

变量与类型

x = 10
y = 3.14
name = "Alice"
flag = True
z = None
a, b = 1, 2

print(type(x))

字符串与 f-string

s = "hello world"
s.upper()
s.split()
s.replace("hello", "hi")
s.startswith("hello")
", ".join(["a", "b", "c"])

name = "Alice"
age = 30
f"{name} is {age} years old"
f"{'centered':^20}"
f"{3.14159:.2f}"

列表与元组

nums = [1, 2, 3]
nums.append(4)
nums.extend([5, 6])
nums.pop()
nums.sort()
nums.reverse()
nums[1:3]
nums[-1]

point = (10, 20)
x, y = point
len(point)

字典与集合

d = {"name": "Alice", "age": 30}
d["name"]
d.get("email", "N/A")
d.keys()
d.values()
d.items()
d.update({"age": 31})

s = {1, 2, 3}
s.add(4)
s.discard(2)
a | b
a & b
a - b

控制流

if x > 0:
    print("positive")
elif x == 0:
    print("zero")
else:
    print("negative")

for i in range(10):
    print(i)

for item in iterable:
    if skip_condition:
        continue
    if stop_condition:
        break

while x > 0:
    x -= 1

match value:
    case 1:
        print("one")
    case _:
        print("other")

函数

def greet(name, greeting="Hello"):
    return f"{greeting}, {name}"

def add(*args, **kwargs):
    return sum(args)

add(1, 2, 3, extra=4)

square = lambda x: x ** 2

def outer(x):
    def inner(y):
        return x + y
    return inner

列表/字典推导式

squares = [x**2 for x in range(10)]
evens = [x for x in range(20) if x % 2 == 0]

matrix = [[1, 2], [3, 4]]
flat = [x for row in matrix for x in row]

word_len = {w: len(w) for w in ["hello", "world"]}
unique = {x for x in [1, 2, 2, 3]}

class Animal:
    count = 0

    def __init__(self, name):
        self.name = name
        Animal.count += 1

    def speak(self):
        raise NotImplementedError

    def __repr__(self):
        return f"Animal({self.name})"

class Dog(Animal):
    def speak(self):
        return f"{self.name} says Woof!"

d = Dog("Rex")
isinstance(d, Animal)
issubclass(Dog, Animal)

错误处理

try:
    result = 1 / 0
except ZeroDivisionError as e:
    print(f"Error: {e}")
except (TypeError, ValueError):
    print("Type or value error")
else:
    print("No error")
finally:
    print("Always runs")

raise ValueError("invalid value")

文件读写

with open("file.txt", "r") as f:
    content = f.read()
    lines = f.readlines()

with open("out.txt", "w") as f:
    f.write("hello\n")

import json
data = json.load(open("data.json"))
json.dumps(data, indent=2)

import csv
with open("data.csv") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row)

模块与包

import os
from os.path import join, exists
from collections import Counter, defaultdict

dir(module)
help(module.func)
__name__ == "__main__"

import sys
sys.path.append("/custom/path")

装饰器

import functools

def timer(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        print(f"{func.__name__}: {time.time() - start:.4f}s")
        return result
    return wrapper

@timer
def slow_func():
    time.sleep(1)

@staticmethod
def utility():
    pass

@classmethod
def create(cls, value):
    return cls(value)

@property
def name(self):
    return self._name

生成器

def count_up(n):
    i = 0
    while i < n:
        yield i
        i += 1

gen = (x**2 for x in range(10))

def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

from itertools import count, chain, islice

上下文管理器

class ManagedResource:
    def __enter__(self):
        print("acquired")
        return self
    def __exit__(self, *exc):
        print("released")

with ManagedResource() as r:
    pass

from contextlib import contextmanager

@contextmanager
def temp_dir():
    d = tempfile.mkdtemp()
    yield d
    shutil.rmtree(d)

常用标准库

from collections import Counter, defaultdict, deque
from datetime import datetime, timedelta
from pathlib import Path
from dataclasses import dataclass
from typing import Optional, List, Dict

p = Path("file.txt")
p.exists()
p.read_text()
p.stem

now = datetime.now()
now.strftime("%Y-%m-%d %H:%M")

Counter("abracadabra").most_common(3)
dd = defaultdict(list)