Kursinnhold
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
Packing in Python: Grouping Multiple Values into a Single Variable
To pack multiple variables, you need to use the *
iterable unpacking operator. Simply place an asterisk *
before the variable, and it will pack any number of variables. Packing a variable is only possible within a tuple or a list.
a, b, *c = 1, 2, 3, 4, 5 # a = 1, b= 2, c = [3, 4, 5]
a, *b, c = 1, 2, 3, 4, 5 # a = 1, b = [2, 3, 4], c = 5
a, b, *c = 1, 2 # a = 1, b = 2, c = []
*a, b = 1, 2, 3 # a = [1, 2], b = 3
*a, = 1, 2, 3 # a = [1, 2, 3]
(*a,) = 1, 2, 3 # a = [1, 2, 3]
[*a] = 1, 2, 3 # a = [1, 2, 3]
*a = 1, 2, 3 # SyntaxError: starred assignment target must be in a list or tuple
But the SyntaxError will occur if to use more than one unpacking operator.
*a, *b = 1, 2, 3, 4
*a, *b, *c = 1, 2, 3
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 2