Python – Star or Asterisk operator ( * )
There are a many places you’ll see * and ** used in Python. Many Python Programmer even at the intermediate level is often puzzled when it comes to the asterisk ( * ) character in Python.
After studying this article, you will have a solid understanding of the asterisk ( * ) operator in Python and become a better coder in the process!
Below are the various uses of the asterisk ( * ) operator in Python:
- Multiplication :
In Multiplication, we multiply two numbers using Asterisk / Star Operator as infix an Operator.
Python3
# using asteriskmul = 5 * 7print (mul) |
Output:
35
- Exponentiation :
Using two(**) Start Operator we can get the exponential value of any integer value.
Python3
a = 5b = 3 # using asteriskresult = a ** bprint(result) |
Output:
125
- Multiplication of a list :
With the help of ‘ * ‘ we can multiply elements of a list, it transforms the code into single line.
Python3
# using asterisklist = ['geeks '] * 3 print(list) |
Output:
['geeks ', 'geeks ', 'geeks ']
- Unpacking a function using positional argument.
This method is very useful while printing your data in a raw format (without any comma and brackets ). Many of the programmer try to remove comma and bracket by using a convolution of functions, Hence this simple prefix asterisk can solve your problem in unpacking them.
Python3
arr = ['sunday', 'monday', 'tuesday', 'wednesday'] # without using asteriskprint(' '.join(map(str,arr))) # using asteriskprint (*arr) |
Output:
sunday monday tuesday wednesday sunday monday tuesday wednesday
- Passing a Function Using with an arbitrary number of positional argument
Here single asterisk( * ) is also used in *args. It is used to pass a variable number of arguments to a function, it is mostly used to pass a non-key argument and variable-length argument list.
It has many uses, one such example is illustrated below, we make an addition function that takes any number of arguments and able to add them all together using *args.
Python3
# using asteriskdef addition(*args): return sum(args) print(addition(5, 10, 20, 6)) |
Output:
41
- Passing a Function Using with an arbitrary number of positional argument
Here double asterisk( ** ) is also used as **kwargs, the double asterisks allow passing keyword argument. This special symbol is used to pass a keyword arguments and variable-length argument list. It has many uses, one such example is illustrated below
Python3
# using asteriskdef food(**kwargs): for items in kwargs: print(f"{kwargs[items]} is a {items}") food(fruit = 'cherry', vegetable = 'potato', boy = 'srikrishna') |
Output:
cherry is a fruit potato is a vegetable srikrishna is a boy
Just another example of using **kwargs, for much better understanding.
Python3
# using asteriskdef food(**kwargs): for items in kwargs: print(f"{kwargs[items]} is a {items}") dict = {'fruit' : 'cherry', 'vegetable' : 'potato', 'boy' : 'srikrishna'}# using asteriskfood(**dict) |
Output:
cherry is a fruit potato is a vegetable srikrishna is a boy


.png)
