What is star syntax in Python and how is it used?
sobes.tech AI
Answer from AI
Star syntax (* and **) in Python is used for unpacking and collecting iterable objects or dictionaries in function calls or when working with collections.
Unpacking with *:
-
In function calls: Unpacks an iterable object (list, tuple) into separate positional arguments.
# Function takes three arguments def func(a, b, c): print(a, b, c) # List with three elements data = [1, 2, 3] # Unpacking list into function arguments func(*data) # Outputs: 1 2 3 -
In function definitions (collecting): Collects an arbitrary number of positional arguments passed to the function into a tuple.
# Function takes the first argument and any number of subsequent arguments def func(first, *others): print(f"First: {first}") print(f"Others: {others}") # Call with different numbers of arguments after the first func(1, 2, 3, 4) # Outputs: # First: 1 # Others: (2, 3, 4) func('a', 'b') # Outputs: # First: a # Others: ('b',) -
In expressions (unpacking): Unpacks an iterable object into other iterable objects.
list1 = [1, 2] list2 = [4, 5] # Unpacking lists into a new list combined_list = [*list1, 3, *list2] print(combined_list) # Outputs: [1, 2, 3, 4, 5] tuple1 = (10,) tuple2 = (30, 40) # Unpacking tuples into a new tuple combined_tuple = (*tuple1, 20, *tuple2) print(combined_tuple) # Outputs: (10, 20, 30, 40)
Double star (**)
-
In function calls (unpacking): Unpacks a dictionary into key-value pairs as named arguments in a function call.
# Function takes named arguments def func(x, y, z): print(f"x={x}, y={y}, z={z}") # Dictionary with keys corresponding to function parameters data = {'x': 10, 'y': 20, 'z': 30} # Unpacking dictionary into named arguments func(**data) # Outputs: x=10, y=20, z=30 -
In function definitions (collecting): Collects an arbitrary number of named arguments passed to the function into a dictionary.
# Function takes a mandatory argument and any number of named arguments def func(arg1, **kwargs): print(f"Mandatory: {arg1}") print(f"Named arguments: {kwargs}") # Call with different named arguments func('required_value', name='Python', version=3.9) # Outputs: # Mandatory: required_value # Named arguments: {'name': 'Python', 'version': 3.9} func('another_value', city='Moscow') # Outputs: # Mandatory: another_value # Named arguments: {'city': 'Moscow'} -
In expressions (unpacking): Unpacks dictionaries into another dictionary.
dict1 = {'a': 1, 'b': 2} dict2 = {'c': 3, 'd': 4} # Unpacking dictionaries into a new dictionary combined_dict = {'x': 0, **dict1, **dict2} print(combined_dict) # Outputs: {'x': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4}
Summary:
| Syntax | Usage | Result (collecting) | Example (unpacking) |
|---|---|---|---|
*args |
Positional arguments | Tuple | func(*my_list) |
**kwargs |
Named arguments | Dictionary | func(**my_dict) |
* |
Unpacking iterable objects | N/A | [*list1, *list2] |
** |
Unpacking dictionaries | N/A | {'a': 1, **dict2} |
These are powerful tools that allow writing more flexible and readable code.