Star and Python
A star can be used in many scenarios in Python. Let's start.
1. Star as a multiplication operator
>>> 5 * 6
30
>>> 'star' * 2
starstar
>>> ['star'] * 2
['star', 'star']
2. Stars as exponential operator
>>> 2 ** 3
8
3. Star in arguments
>>> def add_num(*args):
... for arg in args: print(arg)
... return f"the sum is: {sum(args)}"
...
>>> add_num(1,2,3)
1
2
3
the sum is: 6
Here the star helps to unpack iterables; called as unpacking operator.
4. Stars in keyword arguments
>>> def add_num(**kwargs):
... return f"the sum is: {sum(kwargs.values())}"
...
>>> add_num(a=1, b=2, c=3)
the sum is: 6
Double stars can be used to unpack a dictionary
5. Stars to update two dictionaries
Merging two dictionaries can be done in many ways.
>>> x = {'a': 1, 'b': 2}
>>> y = {'a': 10, 'c': 30}
>>> yy = {'aa': 10, 'c': 30}
>>> z = x | y # Union operator introduced recently
>>> z
{'a': 10, 'b': 2, 'c': 30}
>>> x.update(y) # in place update
# as `y` value getting updated in `x`, `a` value getting overwritten from 1 to 10.
>>> x
{'a': 10, 'b': 2, 'c': 30}
>>> z = dict(**x, **yy)
>>> z
{'a': 1, 'b': 2, 'aa': 10, 'c': 30}
# same key in two dictionaries will throw error
>>> z = dict(**x, **y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: dict() got multiple values for keyword argument 'a'
Further reading
- Do you know all the usage of the Underscore in Python?
- Cool Python tricks you are not using, but you should
- https://blog.soumendrak.com/cache-heavy-computation-functions-with-a-timeout-value
- In Python, do you know double for loops with conditions in a list-comprehension?
- Optimize your Python code
Photo by Michael Kilcoyne on Unsplash