Skip to content

Commit 3094060

Browse files
committed
docs: add a python cheatsheet
1 parent 877f8f8 commit 3094060

3 files changed

Lines changed: 349 additions & 0 deletions

File tree

1.25 MB
Loading
Lines changed: 349 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,349 @@
1+
2+
<br>
3+
<br>
4+
<br>
5+
<br>
6+
<br>
7+
<br>
8+
<br>
9+
<br>
10+
11+
# Advanced Python Playbook
12+
13+
Practical Techniques Used in Real Systems
14+
15+
> A curated reference of modern Python capabilities every serious developer should understand.
16+
17+
18+
![alt text](Gemini_Generated_Image_y61jgmy61jgmy61j.png)
19+
20+
<p align = "right"><b>Written By</b>: <i>Tanu Nanda Prabhu</i></p>
21+
22+
23+
<br>
24+
<br>
25+
<br>
26+
<br>
27+
<br>
28+
<br>
29+
<br>
30+
<br>
31+
<br>
32+
<br>
33+
34+
# 1. List Comprehensions
35+
36+
Compact syntax for generating lists efficiently.
37+
38+
## Example
39+
40+
```python
41+
squares = [x**2 for x in range(6)]
42+
print(squares)
43+
```
44+
45+
## Output
46+
47+
```json
48+
[0, 1, 4, 9, 16, 25]
49+
```
50+
51+
## Conditional Example
52+
53+
```python
54+
even_numbers = [x for x in range(10) if x % 2 == 0]
55+
print(even_numbers)
56+
```
57+
58+
## Output
59+
60+
```json
61+
[0, 2, 4, 6, 8]
62+
```
63+
64+
65+
---
66+
67+
# 2. Generators
68+
69+
> Produce values lazily instead of storing them in memory.
70+
71+
## Example
72+
73+
```python
74+
def count_up(n):
75+
for i in range(n):
76+
yield i
77+
78+
for value in count_up(5):
79+
print(value)
80+
```
81+
82+
<br>
83+
<br>
84+
85+
## Output
86+
87+
```json
88+
0
89+
1
90+
2
91+
3
92+
4
93+
```
94+
95+
## Benefits
96+
97+
| Advantage | Description |
98+
|:--------------- | :---------------------- |
99+
| Memory Efficient | Generates values on demand |
100+
| Faster Pipelines | Ideal for streaming data |
101+
| Clean Iteration | Works naturally with loops |
102+
103+
104+
---
105+
106+
# 3. Decorators
107+
108+
> Extend funtion behaviour without modifying original code.
109+
110+
## Example
111+
112+
```python
113+
def logger(func):
114+
def wrapper():
115+
print("Execution started")
116+
func()
117+
print("Execution finished")
118+
return wrapper
119+
120+
@logger
121+
def greet():
122+
print("Hello Python")
123+
124+
greet()
125+
```
126+
127+
## Output
128+
129+
```json
130+
Execution started
131+
Hello Python
132+
Execution finished
133+
```
134+
135+
---
136+
137+
# 4. Lambda Functions
138+
139+
> Short anonymous functions useful for quick operations.
140+
141+
## Example
142+
143+
```python
144+
add = lambda a, b: a + b
145+
print(add(5, 3))
146+
```
147+
148+
## Output
149+
150+
```json
151+
8
152+
```
153+
154+
## Sorting Example
155+
156+
```python
157+
data = [(1,3), (2,1), (4,2)]
158+
159+
data.sort(key=lambda x: x[1])
160+
print(data)
161+
```
162+
163+
## Output
164+
165+
```json
166+
[(2, 1), (4, 2), (1, 3)]
167+
```
168+
169+
---
170+
171+
# 5. Context Managers
172+
173+
> Automatically handle resource managment.
174+
175+
## Example
176+
177+
```python
178+
with open("file.txt", "w") as f:
179+
f.write("Hello Python")
180+
```
181+
182+
<br>
183+
<br>
184+
<br>
185+
186+
## Why It Matters
187+
188+
| Problem | Solution |
189+
|:-------------- |:---------------- |
190+
| File left open | Auto closing |
191+
| Memory leaks | Resource cleanup |
192+
| Boilerplate | Cleaner syntax |
193+
194+
---
195+
196+
197+
# 6. Dataclasses
198+
199+
> Reduce boilerplate when creating data containers.
200+
201+
## Example
202+
203+
```python
204+
from dataclasses import dataclass
205+
206+
@dataclass
207+
class User:
208+
name: str
209+
age: int
210+
211+
user = User("Alice", 25)
212+
213+
print(user)
214+
```
215+
216+
217+
## Output
218+
219+
```json
220+
User(name='Alice', age=25)
221+
```
222+
223+
224+
---
225+
226+
# 7. Enumerations
227+
228+
> Define named constants for better readability.
229+
230+
## Example
231+
232+
```python
233+
from enum import Enum
234+
235+
class Status(Enum):
236+
SUCCESS = 1
237+
FAILED = 2
238+
PENDING = 3
239+
240+
print(Status.SUCCESS)
241+
```
242+
243+
## Output
244+
245+
```json
246+
Status.SUCCESS
247+
```
248+
249+
---
250+
251+
# 8. Multiprocessing
252+
253+
> Use multiple CPU cores to speed up tasks.
254+
255+
## Example
256+
257+
```python
258+
from multiprocessing import Process
259+
260+
def task():
261+
print("Process executing")
262+
263+
p = Process(target=task)
264+
265+
p.start()
266+
p.join()
267+
```
268+
269+
## Output
270+
271+
```json
272+
Process executing
273+
```
274+
275+
---
276+
277+
# 9. Caching with lru_cache
278+
279+
> Optimize expensive recursive operations.
280+
281+
## Example
282+
283+
```python
284+
from functools import lru_cache
285+
286+
@lru_cache(maxsize=None)
287+
def fibonacci(n):
288+
if n < 2:
289+
return n
290+
return fibonacci(n-1) + fibonacci(n-2)
291+
292+
print(fibonacci(10))
293+
```
294+
295+
## Output
296+
297+
```json
298+
55
299+
```
300+
301+
---
302+
303+
## 10. Flexible Arguments
304+
305+
> Handle variable funtion inputs.
306+
307+
## Example
308+
309+
```python
310+
def example(*args, **kwargs):
311+
print(args)
312+
print(kwargs)
313+
314+
example(1, 2, 3, name="Alice", age=25)
315+
```
316+
317+
## Output
318+
319+
```json
320+
(1, 2, 3)
321+
{'name': 'Alice', 'age': 25}
322+
```
323+
324+
325+
---
326+
327+
# Real World Applications
328+
329+
| Domain | Usage |
330+
| :------------------------ | :------------------ |
331+
| Backend Development | Decorators, caching |
332+
| Data Engineering | Generators |
333+
| Automation | Flexible arguments |
334+
| APIs | Dataclasses |
335+
| High Performance Systems | Multiprocessing |
336+
337+
---
338+
339+
<br>
340+
<br>
341+
342+
# Key Takeaways
343+
344+
- Python offers powerful abstractions for writing clean code.
345+
- Advanced concepts drastically improve performance and maintainability.
346+
- Mastery of these patterns separates intermediate developers from experts.
347+
348+
---
349+
1.34 MB
Binary file not shown.

0 commit comments

Comments
 (0)