Course Content
Intermediate Python Techniques
Intermediate Python Techniques
1. Mastering Packing and Unpacking in Python
2. Mastering Function Arguments in Python
Python Function Arguments: Overview of Parameters and ArgumentsUsing *args in Python: Handling Variable-Length Positional ArgumentsChallenge: Calculating the Average Mark with *argsUsing **kwargs in Python: Flexible Keyword Arguments for Dynamic FunctionsChallenge: Mastering **kwargs in Python Functions
4. Understanding Variable Scope in Python
Global Variables in Python: Accessing and Modifying Global DataLocal Variables in Python: Understanding Function-Level ScopeChallenge: Modifying a Global Variable in PythonNested Functions in Python: Scope and AccessibilityNonlocal Variables in Python: Working with Enclosed ScopesPython Closures: Retaining State in Nested FunctionsChallenge: Implementing a Threshold Checker with Closures
5. Mastering Python Decorators
Introduction to Python DecoratorsPython Decorator Syntax: Writing and Applying DecoratorsChallenge: Create Your First Python DecoratorUsing Decorators with Parameters in PythonChaining Multiple Decorators: Advanced Function ModificationsChallenge: Basic Smores RecipePractical Examples of Python Decorator Usage in Real Applications
Using **kwargs in Python: Flexible Keyword Arguments for Dynamic Functions
Now, let's move on to keyword arbitrary arguments
or **kwargs
. The principle of how **kwargs
works is the same as for *args
, but it accepts keyword arguments instead of positional ones. **kwargs
packs information into a dictionary, so we will work with it accordingly.
def personal_info(name, **kwargs): print(f"Name: {name}") for key, value in kwargs.items(): print(f"{key.capitalize()}: {value}") personal_info("Sarah", surname="Conor", son="John") personal_info("Natalie", cats="3", breed="Maine Coon")
The correct order for the arguments is as follows:
- Positional
- Optional
- *args
- **kwargs
def new_func(a, b=0, *args, **kwargs): print(f"a = {a}, b = {b}, args = {args}, kwargs = {kwargs}") new_func(1, 2, "Love", "Hope", name="Anna", age=20)
If you want to unpack dictionaries, you need to use **
before the dictionary variable.
1. What does **kwargs in a Python function signature represent?
2. What will print_details(name="Alice", age=30) output?
3. Given the function definition below, which call is valid?
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 4