Related Articles

Related Articles

Python – Star or Asterisk operator ( * )
  • Last Updated : 26 Nov, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# using asterisk
mul = 5 * 7
print (mul)

chevron_right


Output:

35
  • Exponentiation :
    Using two(**) Start Operator we can get the exponential value of any integer value.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

a = 5
b = 3
  
# using asterisk
result = a ** b
print(result)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# using asterisk
list = ['geeks '] * 3
  
print(list)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

arr = ['sunday', 'monday', 'tuesday', 'wednesday']
  
# without using asterisk
print(' '.join(map(str,arr))) 
  
# using asterisk
print (*arr)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# using asterisk
def addition(*args):
  return sum(args)
  
print(addition(5, 10, 20, 6))

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# using asterisk
def food(**kwargs):
  for items in kwargs:
    print(f"{kwargs[items]} is a {items}")
      
      
food(fruit = 'cherry', vegetable = 'potato', boy = 'srikrishna')

chevron_right


Output:

cherry is a fruit
potato is a vegetable
srikrishna is a boy

Just another example of using **kwargs, for much better understanding.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# using asterisk
def food(**kwargs):
  for items in kwargs:
    print(f"{kwargs[items]} is a {items}")
      
      
dict = {'fruit' : 'cherry', 'vegetable' : 'potato', 'boy' : 'srikrishna'}
# using asterisk
food(**dict)

chevron_right


Output:

cherry is a fruit
potato is a vegetable
srikrishna is a boy

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :