Different Nature of Asterisks in Python
The asterisk which we generally use to do multiplication can also be used for doing many other operations. In this article, I’m gonna explain the different uses of an asterisk in python.
Basic Multiplication
2 * 3 # 6 (Output)
A very basic use is multiplication.
As an Exponential Operator
2 ** 3 # 8 (Output)
Asterisk can also be used as an exponential operator. The above code snippet shows 2³ = 8
Repeat a string “n” number of times
n = 2;
s = "Programming";print(s * n);# Output
# ProgrammingProgramming
The word “Programming” is repeated twice. With a number ‘*’ works as multiplication, but with a string it repeats the string ‘n’ number of times.
Receive “n” number of arguments in a method.
def print_genius(*names):
for n in names:
print(n)
The above method can now receive the “n” number of arguments for the parameter names.
print_genius('Elon Mask', 'Mark Zuckerberg ', 'Yang Zhou')
Try calling the method by passing as many arguments as you want.
By convention, we define a function like the following if the number of its arguments cannot be determined:
def func(*args, **kwargs):
pass
Unpacking Iterables
A = [1, 2, 3]
B = (4, 5, 6)
C = {7, 8, 9}
L = [*A, *B, *C]
print(L)# Output
# [1, 2, 3, 4, 5, 6, 8, 9, 7]
It can also be used to store a list into a variable.
A = [1, 2, 3]
B = (4, 5, 6)
C = {7, 8, 9}
L = [*A, *B, *C]
print(L)# [1, 2, 3, 4, 5, 6, 8, 9, 7]
Asterisk is very convenient if you expect optional element:
without_optional = 'some string'
*optional = without_optional.split(':')
In the above case, optional will be an empty list and in the below case optional will be [‘other’] (look below code):
with_optional = 'some string:other'
*optional = with_optional.split(':')# optional = ['other']
Some other usages
numbers = [2, 1, 3, 4, 7]
more_numbers = [*numbers, 11, 18]
print(*more_numbers, sep=', ')# 2, 1, 3, 4, 7, 11, 18
Keyword-only arguments without positional arguments
def with_previous(iterable, *, fillvalue=None):
"""Yield each iterable item along with the item before it."""
previous = fillvalue
for item in iterable:
yield previous, item
previous = itemlist(with_previous([2, 1, 3], fillvalue=0))# Output
# [(0, 2), (2, 1), (1, 3)]
This function accepts an iterable
argument, which can be specified positionally (as the first argument) or by its name and a fillvalue
argument which is a keyword-only argument. This function accepts two arguments and one of them, fillvalue
must be specified as a keyword argument. If you try to call the method without specifying an argument, then it will throw an error:
>>> list(with_previous([2, 1, 3], 0))Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: with_previous() takes 1 positional argument but 2 were given
You don’t learn by putting information in your head, you learn by attempting to retrieve information from your head. So you’ve just read an article on something new, but you haven’t learned yet.
I highly recommend you write some code that you uses *
and **
in a number of different ways today and then quiz yourself on the different ways to use these operators tomorrow.
If you know some more cool usages of Asterisk, then let us know in the comments.
See you in the next article. Till then HAPPY LEARNING!!