Open In App

Python – Star or Asterisk operator ( * )

Last Updated : 07 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

There are a many places you’ll see * and ** used in Python. Many Python Programmers even at the intermediate level are 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.
Python
# using asterisk
mul = 5 * 7
print (mul)

Output:

35
  • Exponentiation :
    Using two(**) Star Operators we can get the exponential value of any integer value.
Python
a = 5
b = 3

# using asterisk
result = a ** b
print(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.
Python
# using asterisk
list = ['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 an iterable.  
Python
arr = ['sunday', 'monday', 'tuesday', 'wednesday']

# without using asterisk
print(' '.join(map(str,arr))) 

# using asterisk
print (*arr) 

Output:

sunday monday tuesday wednesday
sunday monday tuesday wednesday
  • Passing a Function Using with an arbitrary number of positional arguments
    Here a 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.
Python
# using asterisk
def addition(*args):
  return sum(args)

print(addition(5, 10, 20, 6))

Output:

41
  • Passing a  Function Using with an arbitrary number of keyword arguments
    Here a double asterisk( ** ) is also used as **kwargs, the double asterisks allow passing keyword arguments. This special symbol is used to pass a keyword arguments and variable-length argument lists. It has many uses, one such example is illustrated below
     
Python
# using asterisk
def 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.

Python
# 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)

Output:

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


Previous Article
Next Article

Similar Reads

bokeh.plotting.figure.asterisk() function in Python
Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, html and server. The Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with default axes, grids, tools, etc. bokeh.plotting.f
4 min read
What does the Double Star operator mean in Python?
Double Star or (**) is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python Language. It is also known as Power Operator. What is the Precedence of Arithmetic Operators? Arithmetic operators follow the same precedence rules as in mathematics, and they are: exponential is performed first, multiplication and division are performed ne
3 min read
Benefits of Double Division Operator over Single Division Operator in Python
The Double Division operator in Python returns the floor value for both integer and floating-point arguments after division. C/C++ Code # A Python program to demonstrate use of # "//" for both integers and floating points print(5//2) print(-5//2) print(5.0//2) Output: 2 -3 2.0 The time complexity of the program is O(1) as it conta
2 min read
Why import star in Python is a bad idea
Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import. Also it becomes very difficult at some times to say from w
3 min read
Python - Draw Star Using Turtle Graphics
In this article, we will learn how to make a Star using Turtle Graphics in Python. For that let's first know what is Turtle Graphics. Turtle graphics Turtle is a Python feature like a drawing board, which let us command a turtle to draw all over it! We can use many turtle functions which can move the turtle around. Turtle comes into the turtle libr
2 min read
Draw Colourful Star Pattern in Turtle - Python
In this article we will use Python's turtle library to draw a spiral of stars, filled with randomly generated colours. We can generate different patterns by varying some parameters. modules required: turtle: turtle library enables users to draw picture or shapes using commands, providing them with a virtual canvas. turtle comes with Python's Standa
2 min read
How to draw color filled star in Python-Turtle?
Prerequisite: Turtle Programming Basics, Draw Color Filled Shapes in Turtle Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move the turtle, there are some functions i.e forward(), backward(), etc. Approach: The following st
2 min read
Draw Spiraling Star using Turtle in Python
Prerequisite: Python Turtle Basics Turtle is an inbuilt module of python. It enables us to draw any drawing by a turtle and methods defined in the turtle module and by using some logical loops. To draw something on the screen(cardboard) just move the turtle(pen).To move turtle(pen) there are some functions i.e forward(), backward(), etcApproach to
1 min read
Star fractal printing using Turtle in Python
Prerequisite: Turtle Programming Basics Fractals are objects that tend to have self-similar structures repeated a finite number of times. The objective of this article is to draw a star fractal where a star structure is drawn on each corner of the star and this process is repeated until the input size reduces to a value of 10. For achieving this st
2 min read
Star Graph using Networkx Python
In this article, we are going to see Star Graph using Networkx Python. A Star graph is a special type of graph in which n-1 vertices have degree 1 and a single vertex have degree n – 1. This looks like that n – 1 vertex is connected to a single central vertex. A star graph with total n – vertex is termed as Sn. Properties of Star Graph: It has n+1
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg