Learn Python Coding
Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills. Admin: @HusseinSheikho || @Hussein_Sheikho
显示更多📈 Telegram 频道 Learn Python Coding 的分析概览
频道 Learn Python Coding (@pythonre) 英语 语言赛道中的 是活跃参与者。目前社区聚集了 39 155 名订阅者,在 技术与应用 类别中位列第 3 508,并在 印度 地区排名第 10 563 位。
📊 受众指标与增长动态
自 невідомо 创建以来,项目保持高速增长,吸引了 39 155 名订阅者。
根据 08 六月, 2026 的最新数据,频道保持稳定运转。过去 30 天订阅人数变化为 425,过去 24 小时变化为 11,整体触达仍然可观。
- 认证状态: 未认证
- 互动率 (ER): 平均受众互动率为 2.56%。内容发布后 24 小时内通常能获得 1.00% 的反应,占订阅者总量。
- 帖子覆盖: 每篇帖子平均可获得 1 003 次浏览,首日通常累积 391 次浏览。
- 互动与反馈: 受众积极参与,单帖平均反应数为 4。
- 主题关注点: 内容集中在 math, harvard, oxford, supervision, waybienad 等核心主题上。
📝 描述与内容策略
作者将该频道定位为表达主观观点的平台:
“Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho”
凭借高频更新(最新数据采集于 09 六月, 2026),频道始终保持新鲜度与高覆盖。分析显示受众积极互动,使其成为 技术与应用 类别中的关键影响点。
isinstance() for Type Checking
(It's safer and more robust than type() because it correctly handles inheritance.)
Cluttered Way (brittle, fails on subclasses):
class MyList(list): pass
my_list_instance = MyList()
if type(my_list_instance) == list:
print("It's a list!") # This will not print
Clean Way (correctly handles subclasses):
class MyList(list): pass
my_list_instance = MyList()
if isinstance(my_list_instance, list):
print("It's an instance of list or its subclass!") # This prints
10. Use the else Block in try/except
(Clearly separates the code that runs on success from the try block being monitored.)
Cluttered Way:
try:
data = my_ risky_operation()
# It's not clear if this next part can also raise an error
process_data(data)
except ValueError:
handle_error()
Clean Way:
try:
data = my_risky_operation()
except ValueError:
handle_error()
else:
# This code only runs if the 'try' block succeeds with NO exception
process_data(data)
#Python #CleanCode #Programming #BestPractices #CodeReadability
━━━━━━━━━━━━━━━
By: @DataScience4 ✨if/else block for simple assignments.)
Cluttered Way:
is_adult = False
age = 20
if age >= 18:
is_adult = True
Clean Way:
age = 20
is_adult = True if age >= 18 else False
2. Use str.join() for Concatenating Strings in a List
(More efficient and readable than using + in a loop.)
Cluttered Way:
words = ["hello", "world", "this", "is", "python"]
sentence = ""
for word in words:
sentence += word + " "
Clean Way:
words = ["hello", "world", "this", "is", "python"]
sentence = " ".join(words)
3. Use collections.defaultdict for Grouping or Counting
(Avoids manual key checking when appending to lists or incrementing counters.)
Cluttered Way:
data = [('fruit', 'apple'), ('veg', 'carrot'), ('fruit', 'banana')]
grouped = {}
for category, item in data:
if category not in grouped:
grouped[category] = []
grouped[category].append(item)
Clean Way:
from collections import defaultdict
data = [('fruit', 'apple'), ('veg', 'carrot'), ('fruit', 'banana')]
grouped = defaultdict(list)
for category, item in data:
grouped[category].append(item)
4. Use collections.namedtuple for Simple Data Objects
(Provides readable attribute access instead of relying on numeric indices.)
Cluttered Way:
point = (10, 20)
# Unclear what point[0] or point[1] represents
if point[0] > 5:
print("X is greater than 5")
Clean Way:
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
point = Point(10, 20)
if point.x > 5:
print("X is greater than 5")
5. Use Argument Unpacking (* and **)
(Passes all items from a list or dictionary as arguments to a function.)
Cluttered Way:
def print_coords(x, y, z):
print(f"X: {x}, Y: {y}, Z: {z}")
coords = [1, 2, 3]
print_coords(coords[0], coords[1], coords[2])
Clean Way:
def print_coords(x, y, z):
print(f"X: {x}, Y: {y}, Z: {z}")
coords_list = [1, 2, 3]
print_coords(*coords_list)
coords_dict = {'x': 4, 'y': 5, 'z': 6}
print_coords(**coords_dict)
6. Use any() and all() for Boolean Checks on Iterables
(More declarative and concise than manual loops with a flag variable.)
Cluttered Way:
numbers = [-1, -2, 5, -4]
has_positive = False
for num in numbers:
if num > 0:
has_positive = True
break
Clean Way:
numbers = [-1, -2, 5, -4] has_positive = any(num > 0 for num in numbers)7. Prefer Generator Expressions for Large Datasets (They don't store the entire sequence in memory, making them highly efficient.) Cluttered Way (can cause high memory usage):
total = sum([i * i for i in range(1000000)])
Clean Way (memory efficient):
total = sum(i * i for i in range(1000000))
8. Use Destructuring for More Powerful Unpacking
(A clean way to assign elements from a sequence to multiple variables.)
Cluttered Way:
numbers = [1, 2, 3, 4, 5]
first = numbers[0]
last = numbers[-1]
Clean Way:
numbers = [1, 2, 3, 4, 5] first, *middle, last = numbers # first = 1, middle = [2, 3, 4], last = 5
def process_data(data):
if data is None:
return "Error: No data provided."
if not isinstance(data, list) or not data:
return "Error: Invalid data format."
# ... logic is now at the top level ...
print("Processing data...")
return "Done"
#Python #CleanCode #Programming #BestPractices #CodingTips
━━━━━━━━━━━━━━━
By: @DataScience4 ✨for loops for creating lists.)
Cluttered Way:
squares = []
for i in range(10):
squares.append(i * i)
Clean Way:
squares = [i * i for i in range(10)]
2. Use enumerate for Index and Value
(Avoids manual index tracking with range(len(...))).)
Cluttered Way:
items = ['a', 'b', 'c']
for i in range(len(items)):
print(i, items[i])
Clean Way:
items = ['a', 'b', 'c']
for i, item in enumerate(items):
print(i, item)
3. Use Context Managers for Resources
(Ensures resources like files are properly closed, even if errors occur.)
Cluttered Way:
f = open('my_file.txt', 'w')
try:
f.write('hello world')
finally:
f.close()
Clean Way:
with open('my_file.txt', 'w') as f:
f.write('hello world')
4. Use Dictionary .get() for Safe Key Access
(Prevents KeyError and avoids verbose if key in dict checks.)
Cluttered Way:
my_dict = {'name': 'Alice'}
if 'age' in my_dict:
age = my_dict['age']
else:
age = 0
Clean Way:
my_dict = {'name': 'Alice'}
age = my_dict.get('age', 0)
5. Use F-Strings for Formatting
(More readable and often faster than other string formatting methods.)
Cluttered Way:
name = "Bob"
age = 30
message = "Hello, " + name + "! You are " + str(age) + " years old."
# Or: message = "Hello, {}! You are {} years old.".format(name, age)
Clean Way:
name = "Bob"
age = 30
message = f"Hello, {name}! You are {age} years old."
6. Use Unpacking to Swap Variables
(A concise, Pythonic way to swap values without a temporary variable.)
Cluttered Way:
a = 5 b = 10 temp = a a = b b = tempClean Way:
a = 5 b = 10 a, b = b, a7. Check for Empty Sequences Correctly (Leverages Python's "truthiness" for more readable code.) Cluttered Way:
my_list = []
if len(my_list) == 0:
print("List is empty!")
Clean Way:
my_list = []
if not my_list:
print("List is empty!")
8. Use Underscores for Unused Variables
(Signals to other developers that a variable is intentionally ignored.)
Cluttered Way:
# 'i' is created but never used
for i in range(5):
print("Hello")
Clean Way:
for _ in range(5):
print("Hello")
9. Chain Comparison Operators
(Makes numerical range checks more intuitive and readable.)
Cluttered Way:
x = 10
if x > 5 and x < 15:
print("x is between 5 and 15")
Clean Way:
x = 10
if 5 < x < 15:
print("x is between 5 and 15")
10. Return from a Function Early
(Reduces nesting and improves readability by handling edge cases or invalid states first.)
Cluttered Way:
def process_data(data):
if data is not None:
if isinstance(data, list) and len(data) > 0:
# ... deep nested logic here ...
print("Processing data...")
return "Done"
else:
return "Error: Invalid data format."
else:
return "Error: No data provided."
Clean Way:# Check if `n > 0` and `(n & (n - 1)) == 0`.• Pow(x, n): Implement
pow(x, n).
# Use exponentiation by squaring for an O(log n) solution.• Majority Element:
# Boyer-Moore Voting Algorithm for an O(n) time, O(1) space solution.• Excel Sheet Column Number:
# Base-26 conversion from string to integer.• Valid Number:
# Use a state machine or a series of careful conditional checks.• Integer to English Words:
# Handle numbers in chunks of three (hundreds, tens, ones) with helper functions.• Sqrt(x): Compute and return the square root of x.
# Use binary search or Newton's method.• Gray Code:
# Formula: `i ^ (i >> 1)`.• Shuffle an Array:
# Implement the Fisher-Yates shuffle algorithm.IX. Python Concepts • Explain the GIL (Global Interpreter Lock):
# Conceptual: A mutex that allows only one thread to execute Python bytecode at a time in CPython.• Difference between
__str__ and __repr__:
# __str__ is for end-users (readable), __repr__ is for developers (unambiguous).• Implement a Context Manager (
with statement):
class MyContext:
def __enter__(self): # setup
return self
def __exit__(self, exc_type, exc_val, exc_tb): # teardown
pass
• Implement itertools.groupby logic:
# Iterate through the sorted iterable, collecting items into a sublist until the key changes.#Python #CodingInterview #DataStructures #Algorithms #SystemDesign ━━━━━━━━━━━━━━━ By: @DataScience4 ✨
# Greedy. Add profit `prices[i] - prices[i-1]` whenever it's positive.• Coin Change 2: Number of combinations that make up an amount.
# Unbounded knapsack DP. `dp[i]` is the number of ways to make amount `i`.• Minimum Path Sum: Find the min path sum from top-left to bottom-right in a grid.
# 2D DP. `dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])`.• House Robber II: Houses are in a circle.
# Run the standard House Robber algorithm twice: once excluding the first house, once excluding the last.• Palindromic Substrings: Count how many substrings are palindromic.
# Expand from center for every possible center (2n-1 centers).• Longest Palindromic Subsequence:
# 2D DP. `dp[i][j]` = length of LPS in `s[i:j+1]`.• Target Sum: Find ways to assign
+ or - to numbers to reach a target.
# DP or backtracking with memoization.• Letter Combinations of a Phone Number:
# Classic backtracking problem.• Knight's Shortest Path on a Chessboard:
# Use Breadth-First Search (BFS).• Generate All Subsequences of a String:
# Backtracking: at each character, either include it or don't.• Interleaving String:
# 2D DP. `dp[i][j]` is true if `s1[:i]` and `s2[:j]` can form `s3[:i+j]`.• Paint House:
# Simple 1D DP. Track the min cost to paint the current house each of the three colors.VI. Sorting, Searching & Heaps • K Closest Points to Origin:
# Use a max-heap of size k or `heapq.nsmallest`.• Find Kth Largest Element in a Stream:
# Maintain a min-heap of size k.• Median of Two Sorted Arrays:
# Binary search on the smaller array to find the correct partition.• Find Median from Data Stream:
# Use two heaps: a max-heap for the lower half and a min-heap for the upper half.• Time Based Key-Value Store:
# Store values in a list sorted by timestamp for each key. Use binary search (`bisect_right`) for lookups.• Reorganize String: Rearrange so no two adjacent characters are the same.
# Greedy approach with a max-heap or by counting character frequencies.• Wiggle Sort II: Reorder so
nums[0] < nums[1] > nums[2] < ....
# Find the median, then use a three-way partition and clever index mapping.• Pancake Sorting:
# Find the max, flip it to the front, then flip it to its correct sorted position. Repeat.• Custom Sort String:
# Use a hash map to count chars in the string, then build the result based on the custom order.VII. Design Questions • Design a Logger Rate Limiter:
# Use a hash map to store the last printed timestamp for each message.• Design Tic-Tac-Toe:
# Store counts for rows, columns, and diagonals to check for a win in O(1).• Design an In-Memory File System:
# Use a Trie-like structure where each node is a directory/file.• Design a Hit Counter:
# Use a queue or deque to store timestamps of hits within the last 5 minutes.• Design TinyURL:
# Map a long URL to a unique hash or incrementing ID, then convert to a base-62 string.• Design a Web Crawler:
# Use BFS to explore links and a set to keep track of visited URLs.• Design Twitter's Data Structures:
# Hash maps for users, tweets. A list/deque for a user's own tweets. A more complex structure for the news feed.• Design Search Autocomplete System:
# Use a Trie (Prefix Tree) where nodes store search frequency.VIII. Bit Manipulation & Math • Power of Two:
# Use a hash map for O(1) key lookups and a doubly linked list to manage recency.III. Stacks & Queues • Largest Rectangle in Histogram:
# Use a monotonic increasing stack to track indices of bars.• Car Fleet: Calculate the number of car fleets that will arrive at the destination.
# Sort cars by position. Use a stack to merge fleets based on arrival time.• Asteroid Collision:
# Use a stack to simulate collisions, handling different sizes and directions.• Decode String (e.g.,
3[a2[c]]):
# Use a stack to keep track of counts and previous strings.• Simplify Path (Unix-style):
# Split by '/' and use a stack to handle '..' and '.'.• Moving Average from Data Stream:
# Use a `collections.deque` with a fixed `maxlen` to efficiently calculate the sum.• Basic Calculator II: Implement a calculator with
+, -, *, /.
# Use a stack to handle operator precedence, processing `*` and `/` immediately.IV. Trees & Graphs • Binary Tree Zigzag Level Order Traversal:
# BFS with a deque. Alternate the direction of appending to the level result list.• Construct Binary Tree from Preorder and Inorder Traversal:
# Use preorder to find the root and inorder to find the left/right subtrees. Recurse.• Populating Next Right Pointers in Each Node:
# Use level-order traversal (BFS) or a clever DFS/recursive approach.• Binary Tree Maximum Path Sum:
# Recursive DFS. At each node, return the max path down, but update a global max with the full path through that node.• Balanced Binary Tree Check:
# DFS. For each node, get the height of left/right subtrees. If diff > 1, it's unbalanced.• Symmetric Tree: Check if a tree is a mirror of itself.
# Use a recursive helper function that compares two nodes `is_mirror(t1, t2)`.• Path Sum II: Find all root-to-leaf paths that sum to a target.
# Backtracking (DFS) with a path list and current sum.• Validate IP Address:
# String parsing with careful checks for IPv4 (numeric ranges) and IPv6 (hex, length).• Find Leaves of Binary Tree: Collect leaves, remove them, repeat.
# Use a modified DFS that returns the height of a node. Group nodes by height.• Graph Valid Tree: Check if a graph is a valid tree.
# A valid tree has `n-1` edges and is fully connected (no cycles). Use DFS/BFS or Union-Find.• Pacific Atlantic Water Flow:
# Start DFS/BFS from all cells on the ocean borders and find the intersection of reachable cells.• Redundant Connection: Find a redundant edge in a graph that makes a cycle.
# Use Union-Find. The first edge that connects two nodes already in the same set is redundant.• Network Delay Time: Find the time for a signal to reach all nodes.
# Dijkstra's algorithm to find the shortest path from the source to all other nodes.• Cheapest Flights Within K Stops:
# Modified Dijkstra's or Bellman-Ford, keeping track of the number of stops.• Alien Dictionary: Find the order of characters from a sorted list of words.
# Build a dependency graph, then perform a topological sort.V. Dynamic Programming & Recursion • Decode Ways: Number of ways to decode a string of digits.
# DP: `dp[i]` is the number of ways to decode `s[:i]`. Consider one and two-digit decodings.• Partition Equal Subset Sum:
# DP (knapsack variation). Check if a subset sums to `total_sum / 2`.• Best Time to Buy and Sell Stock II: You can buy and sell multiple times.
# Sort the array, then use a two-pointer approach for each element.• Rotate Image: Rotate an NxN matrix by 90 degrees in-place.
# Transpose the matrix, then reverse each row.• Set Matrix Zeroes: If an element is 0, set its entire row and column to 0.
# Use the first row/col as markers to avoid extra space.• Spiral Matrix: Traverse a matrix in a spiral order.
# Use four pointers for boundaries (top, bottom, left, right) and shrink them inward.• Sort Colors (Dutch National Flag problem): Sort an array of 0s, 1s, and 2s.
# Use three pointers (low, mid, high) to partition in a single pass.• Find Peak Element: Find an element that is greater than its neighbors.
# Use binary search. Compare mid with its right neighbor to decide which way to go.• Maximum Product Subarray: Find the max product of a contiguous subarray.
# Track current max and min product because two negatives make a positive.• Longest Consecutive Sequence: Find the length of the longest run of consecutive numbers.
# Use a hash set for O(1) lookups. Only start counting from the beginning of a sequence.• Insert Interval: Insert a new interval into a list of non-overlapping intervals.
# Iterate through intervals, add non-overlapping ones, merge with overlapping ones.• Merge Intervals: Merge all overlapping intervals.
# Sort intervals by start time, then iterate and merge.• Jump Game: Can you reach the last index?
# Greedy approach. Keep track of the maximum reachable index.• Next Permutation: Find the next lexicographically greater permutation.
# Find the first decreasing element from the right, swap it with the next largest, then reverse the rest.• Find the Duplicate Number: Array
n+1 integers from 1 to n. Find the one duplicate.
# Use Floyd's Tortoise and Hare (cycle detection) algorithm.• Word Search: Find a word in a 2D grid of characters.
# Backtracking (DFS) from each cell. Mark visited cells to avoid reuse.• Task Scheduler: Schedule tasks with a cooling period.
# Greedy approach using a frequency map or a max-heap.• Is Subsequence: Check if one string is a subsequence of another.
# Simple two-pointer approach.• Dot Product of Two Sparse Vectors:
# Store non-zero elements in a hash map or list of pairs to optimize.II. Linked Lists • Remove Duplicates from Sorted List II: Delete all nodes that have duplicate numbers.
# Use a sentinel/dummy head and a predecessor pointer.• Rotate List: Rotate the list to the right by k places.
# Find length, connect tail to head to make a cycle, then break at the new tail.• Partition List: Partition around a value
x.
# Use two pointers to build two separate lists (less than `x` and >= `x`), then link them.• Swap Nodes in Pairs: Swap every two adjacent nodes.
# Use careful pointer manipulation with a dummy head.• Odd Even Linked List: Group odd nodes followed by even nodes.
# Use two pointers, one for the odd list tail and one for the even list tail.• Flatten a Multilevel Doubly Linked List:
# Use DFS. When a child is found, insert its flattened list between the current node and its next.• LRU Cache: Design a Least Recently Used cache.
ForeignKey objects in the same query.
entries = Entry.objects.select_related('author').all()
• Fetch related ManyToManyField objects in a separate efficient query.
entries = Entry.objects.prefetch_related('tags').all()
• Load only specific model fields.
entries = Entry.objects.only('headline')
• Defer loading of specific model fields.
entries = Entry.objects.defer('body_text')
• Execute raw, unmanaged SQL.
authors = Author.objects.raw('SELECT * FROM myapp_author')
• Get results as a list of tuples.
Entry.objects.values_list('headline', 'pub_date')
XV. Transactions
• Import the transaction module.
from django.db import transaction
• Run a block of code within a database transaction.
with transaction.atomic():
# All database operations here are either committed together or rolled back.
author.save()
entry.save()
XVI. Managers & Model Methods
• Create a custom Manager for common queries.
class PublishedEntryManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')
• Add a custom method to a QuerySet via its Manager.
Entry.objects.get_queryset().by_author("John Doe")
• Add a custom method to a model for object-specific logic.
class Entry(models.Model):
#...
def is_recent(self):
return self.pub_date > timezone.now() - timedelta(days=1)
#Python #Django #ORM #Database #Backend
━━━━━━━━━━━━━━━
By: @DataScience4 ✨entry = Entry.objects.get(pk=1)
entry.rating = 5
entry.save()
• Update multiple objects in a single query.
Entry.objects.filter(pub_date__year=2022).update(rating=0)
• Remove an object from a ManyToManyField.
entry.tags.remove(tag1)
VII. Deleting Objects
• Delete a single object.
entry = Entry.objects.get(pk=1)
entry.delete()
• Delete multiple objects in a single query.
Entry.objects.filter(rating=0).delete()
VIII. Spanning Relationships
• Filter based on a related model's field.
Entry.objects.filter(author__name="John Doe")
• Use a field lookup on a related model.
Entry.objects.filter(author__name__startswith="John")
• Access related objects from the "one" side of a relationship.
author = Author.objects.get(name="John Doe")
authors_entries = author.entry_set.all()
IX. Aggregation
(Requires from django.db.models import ...)
• Calculate aggregate values over an entire queryset.
from django.db.models import Avg
avg_rating = Entry.objects.aggregate(Avg('rating'))
• Average (Avg).
Entry.objects.aggregate(average_rating=Avg('rating'))
• Sum (Sum).
Entry.objects.aggregate(total_ratings=Sum('rating'))
• Maximum (Max) and Minimum (Min).
Entry.objects.aggregate(max_r=Max('rating'), min_r=Min('rating'))
• Count (Count).
Entry.objects.aggregate(num_entries=Count('pk'))
• Count distinct values.
Entry.objects.aggregate(num_authors=Count('author', distinct=True))
X. Annotation
• Add a calculated field to each object in a queryset.
from django.db.models import Count
authors = Author.objects.annotate(entry_count=Count('entry'))
• Access an annotated value.
for author in authors:
print(author.name, author.entry_count)
• Get results as a list of dictionaries.
Entry.objects.values('headline', 'author__name')
XI. F() Expressions
• Import F() expressions.
from django.db.models import F
• Update a field based on its own value.
Entry.objects.filter(pk=1).update(rating=F('rating') + 1)
• Filter by comparing two fields on the same model.
Entry.objects.filter(rating__gt=F('author__rating'))
XII. Q() Objects
• Import Q() objects for complex queries.
from django.db.models import Q
• Create an OR query.
Entry.objects.filter(Q(headline__startswith='A') | Q(headline__startswith='B'))
• Create a NOT query.
Entry.objects.filter(~Q(headline__icontains='boring'))
XIII. QuerySet Evaluation
• Get the count of objects efficiently.
count = Entry.objects.count()
• Check if any objects exist efficiently.
exists = Entry.objects.filter(rating=5).exists()
• Get the count (evaluates the full queryset).
count = len(Entry.objects.all())
• Check for existence (evaluates the full queryset).
if Entry.objects.filter(rating=5): # True if queryset is not empty
...
• Iteration (evaluates the queryset).
for entry in Entry.objects.all():
print(entry.headline)
XIV. Optimizationmodels.py)
• Define a character field.
from django.db import models
name = models.CharField(max_length=100)
• Define a foreign key relationship (many-to-one).
author = models.ForeignKey('Author', on_delete=models.CASCADE)
• Define a many-to-many relationship.
tags = models.ManyToManyField('Tag')
• Define a model's Meta class for default ordering.
class Meta:
ordering = ['-pub_date']
• Define the string representation of an object.
def __str__(self):
return self.headline
II. Creating Objects
• Create and save an object in one step.
entry = Entry.objects.create(headline="New Title", author=some_author)
• Create an instance and save it separately.
new_author = Author(name="John Doe")
new_author.save()
• Create multiple objects in a single query.
Author.objects.bulk_create([Author(name="A"), Author(name="B")])
• Add an object to a ManyToManyField.
entry.tags.add(tag1, tag2)III. Retrieving Objects (Basic QuerySets) • Retrieve all objects.
all_entries = Entry.objects.all()
• Retrieve a single object by a unique constraint (e.g., PK).
entry = Entry.objects.get(pk=1)
• Filter objects that match criteria.
entries = Entry.objects.filter(pub_date__year=2023)
• Exclude objects that match criteria.
entries = Entry.objects.exclude(headline__startswith="Old")
• Get the first object from a queryset.
first_entry = Entry.objects.order_by('pub_date').first()
• Get the last object from a queryset.
last_entry = Entry.objects.order_by('pub_date').last()
• Get an object, or create it if it doesn't exist.
obj, created = Author.objects.get_or_create(name="Unique Name")
IV. Field Lookups for Filtering
• Exact match (case-sensitive).
Entry.objects.filter(headline__exact="Hello World")
• Case-insensitive exact match.
Entry.objects.filter(headline__iexact="hello world")
• Case-sensitive contains.
Entry.objects.filter(headline__contains="World")
• Case-insensitive contains.
Entry.objects.filter(headline__icontains="world")
• Match against a list of values.
Entry.objects.filter(pk__in=[1, 3, 5])
• Greater than (__gt) or greater than or equal (__gte).
Entry.objects.filter(rating__gt=4)
• Less than (__lt) or less than or equal (__lte).
Entry.objects.filter(rating__lte=3)
• Starts with or ends with a string.
Entry.objects.filter(headline__startswith="The")
• Match within a range.
Entry.objects.filter(pub_date__range=["2023-01-01", "2023-03-31"])
• Filter by a date component.
Entry.objects.filter(pub_date__year=2023, pub_date__month=5)
• Check if a field is NULL.
Entry.objects.filter(pub_date__isnull=True)
V. Slicing & Ordering QuerySets
• Slice a queryset (similar to Python lists).
first_ten = Entry.objects.all()[:10]
• Order results in ascending order.
Entry.objects.order_by('headline')
• Order results in descending order.
Entry.objects.order_by('-pub_date')
• Order results randomly.
Entry.objects.order_by('?')
VI. Modifying Objects
• Update a single object's fields.@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.filename, "content_type": file.content_type}
• Set a response cookie.
from fastapi import Response
@app.post("/cookie/")
def create_cookie(response: Response):
response.set_cookie(key="fakesession", value="fake-cookie-session-value")
return {"message": "Cookie set"}
• Read a request cookie.
from fastapi import Cookie
@app.get("/read-cookie/")
def read_cookie(fakesession: Optional[str] = Cookie(None)):
return {"session": fakesession}
X. Error Handling & Advanced Responses
• Handle HTTPException.
from fastapi import HTTPException
@app.get("/items/{item_id}")
def read_item(item_id: str):
if item_id not in items_db:
raise HTTPException(status_code=404, detail="Item not found")
• Return a custom JSONResponse.
from fastapi.responses import JSONResponse
@app.get("/custom/")
def custom_response():
return JSONResponse(status_code=202, content={"message": "Accepted"})
• Return an HTMLResponse.
from fastapi.responses import HTMLResponse
@app.get("/page", response_class=HTMLResponse)
def read_page():
return "<h1>Hello World</h1>"
• Define a WebSocket endpoint.
from fastapi import WebSocket
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Hello from WebSocket")
await websocket.close()
• Handle a path operation that may not exist.
@app.get("/{full_path:path}")
def read_catch_all(full_path: str):
return {"path": full_path}
• Mount a static files directory.
from fastapi.staticfiles import StaticFiles
app.mount("/static", StaticFiles(directory="static"), name="static")
• Add a custom exception handler.
from fastapi import Request
from fastapi.responses import JSONResponse
class MyCustomException(Exception): pass
@app.exception_handler(MyCustomException)
async def custom_exception_handler(request: Request, exc: MyCustomException):
return JSONResponse(status_code=418, content={"message": "I'm a teapot"})
• Read a request header.
from fastapi import Header
@app.get("/headers/")
async def read_headers(user_agent: Optional[str] = Header(None)):
return {"User-Agent": user_agent}
• Get a raw Request object.
from fastapi import Request
@app.get("/request-info")
def get_request_info(request: Request):
return {"client_host": request.client.host}
#Python #FastAPI #API #WebDevelopment #Pydantic
━━━━━━━━━━━━━━━
By: @DataScience4 ✨from fastapi import Depends
async def common_parameters(q: Optional[str] = None):
return {"q": q}
• Inject a dependency into an endpoint.
@app.get("/users/")
async def read_users(commons: dict = Depends(common_parameters)):
return commons
• Create a dependency with yield (for setup/teardown).
async def get_db():
db = DBSession()
try:
yield db
finally:
db.close()
• Use a class as a dependency.
class CommonQueryParams:
def __init__(self, q: Optional[str] = None):
self.q = q
@app.get("/search/")
async def search(commons: CommonQueryParams = Depends()):
return commons
VI. Security
• Setup OAuth2 password flow.
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
• Protect an endpoint with a security dependency.
@app.get("/users/me")
async def read_users_me(token: str = Depends(oauth2_scheme)):
return {"token": token}
• Get the current authenticated user.
async def get_current_user(token: str = Depends(oauth2_scheme)):
# logic to get user from token
return user
@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
return current_user
VII. Advanced Routing
• Create an APIRouter.
from fastapi import APIRouter
router = APIRouter()
@router.get("/users/")
def get_users():
return [{"username": "user1"}]
• Include a router in the main app.
app.include_router(router, prefix="/api/v1", tags=["users"])
• Add tags to endpoints for documentation.
@app.get("/items/", tags=["items"])
• Add a summary and description to an endpoint.
@app.get("/", summary="Root Endpoint", description="Returns a welcome message.")
• Deprecate an endpoint.
@app.get("/old-path", deprecated=True)
VIII. Middleware, Events & Background Tasks
• Create an app startup event.
@app.on_event("startup")
async def startup_event():
print("Application startup")
• Create an app shutdown event.
@app.on_event("shutdown")
def shutdown_event():
print("Application shutdown")
• Add custom middleware.
from fastapi import Request
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
response = await call_next(request)
return response
• Define a background task.
from fastapi import BackgroundTasks
def write_notification(email: str, message=""):
# send email logic
pass
• Add a background task to an endpoint.
@app.post("/send/{email}")
def send_notification(email: str, background_tasks: BackgroundTasks):
background_tasks.add_task(write_notification, email, message="some notification")
return {"message": "Notification sent"}
IX. Handling Forms, Files & Cookies
• Handle form data.
from fastapi import Form
@app.post("/login/")
def login(username: str = Form(...), password: str = Form(...)):
return {"username": username}
• Handle file uploads.
from fastapi import File, UploadFile
@app.post("/files/")
async def create_file(file: bytes = File(...)):
return {"file_size": len(file)}
• Handle UploadFile (better for large files).from fastapi import FastAPI
app = FastAPI()
• Define a GET endpoint.
@app.get("/")
def read_root():
return {"Hello": "World"}
• Define a POST endpoint.
@app.post("/items/")
def create_item(item: dict):
return item
• Define a path parameter.
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id}
• Define a query parameter.
@app.get("/search/")
def search_items(q: str):
return {"query": q}
• Define an optional query parameter.
from typing import Optional
@app.get("/list/")
def list_items(skip: int = 0, limit: Optional[int] = None):
return {"skip": skip, "limit": limit}
II. Request Body & Pydantic Models
• Create a Pydantic model for request body.
from pydantic import BaseModel
class Item(BaseModel):
name: str
price: float
• Use a Pydantic model in a POST request.
@app.post("/items/")
def create_item(item: Item):
return item
• Use a Pydantic model with a path parameter.
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_id": item_id, **item.dict()}
• Define a nested Pydantic model.
class User(BaseModel):
username: str
class FullItem(Item):
owner: User
III. Data Validation & Metadata
• Add validation to a query parameter.
from fastapi import Query
@app.get("/items/")
def read_items(q: str = Query(..., min_length=3, max_length=50)):
return {"q": q}
• Add validation to a path parameter.
from fastapi import Path
@app.get("/users/{user_id}")
def read_user(user_id: int = Path(..., gt=0, le=1000)):
return {"user_id": user_id}
• Add metadata (title, description) to a parameter.
q: str = Query(None, title="Search Query", description="Query string")
• Provide an example for a Body field.
from fastapi import Body
item: Item = Body(..., example={"name": "Foo", "price": 35.4})
• Use an alias for a model field.
from pydantic import Field
class Item(BaseModel):
item_name: str = Field(..., alias="itemName")
IV. Response Models & Status Codes
• Set a response status code.
from fastapi import status
@app.post("/items/", status_code=status.HTTP_201_CREATED)
def create_item(item: Item):
return item
• Use response_model to filter output.
class ItemOut(BaseModel):
name: str
@app.post("/items/", response_model=ItemOut)
def create_item(item: Item):
return item
• Exclude fields from response_model.
# Returns all fields from Item model except 'price'
@app.get("/items/{item_id}", response_model=Item, response_model_exclude={"price"})
• Return a direct Response.
from fastapi import Response
@app.get("/legacy/")
def get_legacy_data():
return Response(content="<data>some legacy xml</data>", media_type="application/xml")
V. Dependencies
现已上线!2025 年 Telegram 研究 — 年度关键洞察 
