Skip to content

Commit e6d950a

Browse files
committed
add content for week 28
1 parent d3d0c0a commit e6d950a

File tree

1 file changed

+129
-3
lines changed

1 file changed

+129
-3
lines changed

doc/newsletters/2023/WEEK_28.md

+129-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ with tempfile.TemporaryDirectory() as temp_dir:
109109
temp_file_path = os.path.join(temp_dir, 'example.txt')
110110
with open(temp_file_path, 'w') as temp_file:
111111
temp_file.write('Hello, Python-world!')
112-
112+
113113
# Perform operations in the temporary directory
114114

115115
# The temporary directory and its contents are automatically cleaned up
@@ -132,7 +132,7 @@ with tempfile.NamedTemporaryFile(suffix='.csv', delete=False) as temp_file:
132132
csv_writer.writerow(['Name', 'Age'])
133133
csv_writer.writerow(['John Doe', '30'])
134134
csv_writer.writerow(['Jane Smith', '25'])
135-
135+
136136
print(f"Temporary CSV file created: {temp_file.name}")
137137
```
138138

@@ -346,7 +346,7 @@ print("Printable ASCII Characters:", string.printable)
346346

347347
Output:
348348
```
349-
Printable ASCII Characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
349+
Printable ASCII Characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
350350
```
351351

352352
**🌟 Examples**
@@ -380,6 +380,132 @@ To dive deeper into the `string` module and its functionalities, here are some r
380380
- Official Python Documentation: [`string`](https://docs.python.org/3/library/string.html)
381381

382382

383+
### 🔢 Python Match Statement
384+
385+
Introduced in Python 3.10, the match statement is a powerful tool for pattern matching. It allows you to simplify complex if-elif-else chains by providing a concise and readable syntax. Here's an example:
386+
387+
```python
388+
def get_day_of_week(day_number):
389+
match day_number:
390+
case 1:
391+
return "Monday"
392+
case 2:
393+
return "Tuesday"
394+
case 3:
395+
return "Wednesday"
396+
case 4:
397+
return "Thursday"
398+
case 5:
399+
return "Friday"
400+
case 6:
401+
return "Saturday"
402+
case 7:
403+
return "Sunday"
404+
case _:
405+
return "Invalid day number"
406+
```
407+
408+
The match statement evaluates the input expression (`day_number` in this case) and compares it against different patterns (`case` statements). If a match is found, the corresponding block of code is executed. The `_` represents a wildcard pattern that matches anything.
409+
410+
### 🌪️ Decorators that Take Arguments
411+
412+
Building upon the [previous article](https://python-world.github.io/newsletter/newsletters/2023/WEEK_27.html#unleash-the-power-of-python-function-decorators) on decorators, we can enhance their functionality by allowing them to accept arguments. This provides greater flexibility and customization.
413+
414+
Here's an example:
415+
416+
```python
417+
def repeat(n):
418+
def decorator(func):
419+
def wrapper(*args, **kwargs):
420+
for _ in range(n):
421+
result = func(*args, **kwargs)
422+
return result
423+
return wrapper
424+
return decorator
425+
426+
@repeat(3)
427+
def greet(name):
428+
print(f"Hello, {name}!")
429+
430+
greet("John")
431+
```
432+
433+
In this example, the `repeat` decorator takes an argument `n` and returns a decorator function. This decorator function, in turn, takes the original function as an argument and returns the modified function (`wrapper`). The modified function is then executed multiple times, as specified by the `repeat` argument.
434+
435+
### 🛫 Map and Filter Functions
436+
437+
Python provides two built-in functions, `map` and `filter`, that are widely used to process iterables.
438+
439+
The `map` function applies a given function to each item in an iterable and returns an iterator with the results. Here's an example:
440+
441+
```python
442+
numbers = [1, 2, 3, 4, 5]
443+
444+
squared_numbers = map(lambda x: x ** 2, numbers)
445+
print(list(squared_numbers)) # Output: [1, 4, 9, 16, 25]
446+
```
447+
448+
The `filter` function applies a given function to each item in an iterable and returns an iterator with the items for which the function returns `True`. Here's an example:
449+
450+
```python
451+
numbers = [1, 2, 3, 4, 5]
452+
453+
even_numbers = filter(lambda x: x % 2 == 0, numbers)
454+
print(list(even_numbers)) # Output: [2, 4]
455+
```
456+
457+
### 🍁 Global and Nonlocal Variables
458+
459+
In Python, the `global` and `nonlocal` keywords allow you to modify variables outside the current scope.
460+
461+
The `global` keyword is used to indicate that a variable within a function should refer to the global variable with the same name. Here's an example:
462+
463+
```python
464+
count = 0
465+
466+
def increment():
467+
global count
468+
count += 1
469+
470+
increment()
471+
print(count) # Output: 1
472+
```
473+
474+
The `nonlocal` keyword is used to indicate that a variable within a nested function should refer to a variable from its outer scope. Here's an example:
475+
476+
```python
477+
def outer():
478+
x = 1
479+
480+
def inner():
481+
nonlocal x
482+
x += 1
483+
print(x)
484+
485+
inner()
486+
487+
outer() # Output: 2
488+
```
489+
490+
### 🫙 Closures
491+
492+
A closure is a function object that remembers values in its enclosing scope, even if they are not present in memory. This allows the function to access and manipulate variables from the outer function, even after the outer function has finished executing. Here's an example:
493+
494+
```python
495+
def outer_function(x):
496+
def inner_function(y):
497+
return x + y
498+
return inner_function
499+
500+
add_5 = outer_function(5)
501+
print(add_5(3)) # Output: 8
502+
```
503+
504+
In this example, `outer_function` returns `inner_function`, which remembers the value of `x` even after `outer_function` has completed. The returned `inner_function` can be called later, providing the remembered value `x` and accepting an additional parameter `y` to perform the desired computation.
505+
506+
These are just a few examples of Python's powerful features. Each of these concepts has its own unique applications and can greatly enhance your Python programming skills. Experiment with these concepts and explore their potential to unlock even more possibilities in your projects!
507+
508+
383509
## Upcoming Events
384510

385511

0 commit comments

Comments
 (0)