You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -380,6 +380,132 @@ To dive deeper into the `string` module and its functionalities, here are some r
380
380
- Official Python Documentation: [`string`](https://docs.python.org/3/library/string.html)
381
381
382
382
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
+
defget_day_of_week(day_number):
389
+
match day_number:
390
+
case1:
391
+
return"Monday"
392
+
case2:
393
+
return"Tuesday"
394
+
case3:
395
+
return"Wednesday"
396
+
case4:
397
+
return"Thursday"
398
+
case5:
399
+
return"Friday"
400
+
case6:
401
+
return"Saturday"
402
+
case7:
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
+
defrepeat(n):
418
+
defdecorator(func):
419
+
defwrapper(*args, **kwargs):
420
+
for _ inrange(n):
421
+
result = func(*args, **kwargs)
422
+
return result
423
+
return wrapper
424
+
return decorator
425
+
426
+
@repeat(3)
427
+
defgreet(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:
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(lambdax: 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
+
defincrement():
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
+
defouter():
478
+
x =1
479
+
480
+
definner():
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
+
defouter_function(x):
496
+
definner_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!
0 commit comments