What is the Asterisk Operator in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Explanation

In Python, the symbols * and ** are used frequently. Even intermediate-level Python programmers frequently have trouble understanding the asterisk (*) character there in Python.

Asterisks are mostly used as power and multiplication operators, but they can also perform a wide variety of operations in Python when used as a prefix operator. You will have a clear understanding of Python's asterisk (*) operator after going through this article.

Syntax

Use an asterisk(*) according to the operation to be performed.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example

Output

In the given code above a variable number of parameters are passed to the sumto function and then this *nums works as an *args keyword which stores that series of numbers and then the sum function is performed over that series of numbers.

What are the Uses of Asterisk in Python?

In Python, asterisks have a wide variety of applications. The operators like multiplication and power are generally known to us that can be done using this asterisk(*). In different circumstances, it can carry out additional operations like unpacking and a variable number of arguments passing, etc. Let's look at how asterisks are generally used first.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

1. Multiplication

When multiplying two numbers, we use the asterisk (*) operator as the infix operator.

Output

2. Exponentiation

We can determine an exponential value of the integer using the two (**) asterisk operators.

Output

3. List Multiplication

The symbol "*" allows us to multiply list items and reduces the code to a single line.

Output

4. Unpacking a Function Using the Positional Argument

This technique is extremely helpful when we have to print information in a raw format that is without any brackets and commas. This prefix asterisk can help you in unpacking them because many programmers attempt to remove the bracket and comma by using a convolution of functions.

Output

5. Passing a Function Using an Arbitrary Number of Positional Arguments

Here, *args also uses a single asterisk (*). It is used to pass the variable number of parameters to the function, typically non-key arguments and argument lists of varying lengths. There are many applications for it, and one of them is shown in the example below. We create an additional function that can add any number of parameters using the *args keyword.

Output

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

6. Passing a Function Using an Arbitrary Number of Keyword Arguments

While passing a dictionary as an argument to the function, the ** symbol is used before an argument. This syntax is used to execute the code correctly when we don't know how many keyword arguments will be provided to the function.

Output

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

7. List Packing up

In some situations, we can use asterisks for both packs and unpack lists of items. For instance, the syntax given below can be used to get three separate items from a list by using 3 variables.

Output

Let's say that in the scenario mentioned above, it doesn't concern who my friends are, all that matters is that my name comes last and the list of names stays the same. In this instance, we can pack up all of the other elements using an asterisk.

Output

More Examples of Different Applications of Asterisk in Python

Unpacking Dictionary

A double asterisk must be placed before the dictionary variable to allow it to be unpacked. As for Python 3.5, asterisks also can unpack the dictionaries. Combining several dictionaries would be one of the most popular applications. Let's say we want to combine the following two dictionaries. We could unpack them both and place them within a different, empty dictionary.

Output

Unpack Range

A Python range is one of the rare applications for utilizing asterisks for unpacking. To specify how many times a for-loop should loop, we typically use range(). It can also be used to generate the following sequence of integers.

Output

Conclusion

  • Whenever Python is updated(when the new version is released), developers add new functionality.
  • Special symbols like asterisks shorten various sections of the code.
  • Without asterisk(*), we are unable to accept multiple positional arguments, keyword-only arguments also require asterisk(*).
  • Asterisk(*) may function as an operator or even an indicator for passing varying numbers of parameters.  In addition to packing them up, it is also able to unpack dictionaries, lists, and range objects.

Learn More:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more