Skip to main content
HomeAbout PythonLearn Python

Usage of Asterisks in Python

Many Python users are familiar with using asterisks for multiplication and power operators, but in this tutorial, you'll find out additional ways on how to apply the asterisk.
Jun 2019  · 9 min read

Most of us use asterisks as multiplication and power operators, but they also have a wide range of operations being used as a prefix operator in Python. After reading this article, you will get to know the full usage of asterisks.

Asterisks have many particular use cases in Python. In general, we are familiar with the multiplication and power operators. It can perform some other operations like unpacking, arguments passing, etc.., in different situations. First, let's see the general usage of asterisks.

General Usage Of * and **

* is used as multiplication operator whereas ** is used as a power operator.

## declaring variables
a, b = 2,3

## multiplication
print("Multiplication of 2 and 3 is {}".format(a * b))

## as power operator
print("2 power 3 is {}".format(a ** b))
Multiplication of 2 and 3 is 6
2 power 3 is 8

Now you have seen the general and most commonly used asterisks. There are however asterisks use cases which you may not know. Let's move to extract the hidden usage of asterisks.

1. Unpacking Using *

Unpacking the elements from a list/tuple/set using the *. We are going to use unpacking in future code to implement some other features of asterisks. Unpacking is the basic idea behind using the asterisks as prefix operators.

Let's assume that we want to assign the first element of list/tuple/set to a and remaining elements to b. In general, we will accomplish this using slicing.

## general approach
nums = [i for i in range(1, 6)]
a = nums[0]
b = nums[1:]
print(a, b)


## hard approach
squares = [i ** 2 for i in range(1, 6)]
a = squares[0]
b = []
for i in range(1, len(squares)):
print(a, b)
1 [2, 3, 4, 5]
1 [4, 9, 16, 25]

Now, we are going to use the * operator to implement this in a single line of code.

nums = [i for i in range(1, 6)]

## a will be 1 and b will be a list containing remaining elements
a, *b = nums
print(a, b)
1 [2, 3, 4, 5]

Here a is a type of variable that it gets from the list and b is a list. Unpacking can work on tuples and sets the same as it does with the lists. Let's see some examples of it.

## using tuple
## here first element will assign to a and last element to c
a, *b, c = (1, 2, 3, 4, 5)
print(a, b, c)


## using sets
a, *b, c = {1, 4, 9, 16, 25}
print(a, b, c)
1 [2, 3, 4] 5
1 [4, 9, 16] 25

1.1. Combining Different Iterables

We can add two different iterables into list/sets/tuples using unpacking. In general, we would have to convert the iterables and combine them into one. It's not necessary though if we use the *. Let's see an example.

## combining a tuple, list, set
nums = [1, 2, 3]
nums2 = (4, 5, 6)
nums3 = {7, 8, 9}

## we convert the combined elements into any iterable we want
## here i am converting into a list
_list = [*nums, *nums2, *nums3]
_tuple = (*nums, *nums2, *nums3)
_set = {*nums, *nums2, *nums3}

<class 'list'>
[1, 2, 3, 4, 5, 6, 8, 9, 7]
<class 'tuple'>
(1, 2, 3, 4, 5, 6, 8, 9, 7)
<class 'set'>
{1, 2, 3, 4, 5, 6, 7, 8, 9}

1.2. Nested Unpacking

Nested unpacking is taking out the elements from the first level of extracted items. See the example to get an idea of what I am talking about.

languages = ["Python", "HTML", "CSS", "JS"]

## unpacking
[[first_letter, *remaining], *other] = languages

print(first_letter, remaining, other)
P ['y', 't', 'h', 'o', 'n'] ['HTML', 'CSS', 'JS']

2. Unpacking Using **

As we saw earlier, ** is used to unpack the elements from a dictionary. We must know the keys of the dictionary to extract the values during unpacking. Let's see an example.

## sample dictionary
person = {"name":"John", "age":19, "year_of_passing":"2021"}
string = "Name:-{name} Year Of Graduation:-{year_of_passing} Age:-{age}".format(**person)
Name:-John Year Of Graduation:-2021 Age:-19

It's not common to use the ** to extract the elements from a dictionary.

3. Asterisks in Functions

We can pass and capture the arguments using asterisks in functions to reduce the lines of code.

3.1. Unpacking in Functions

The * operator is used to call a function by unpacking an iterable. Let's say we have a list and have to pass all the elements of it to the print() separately. What will you do? I think you are thinking of a loop. But, * makes this very easy for us.

Just pass the iterable as *iterable_name. It unpacks the iterable and passes all the elements separately.

nums = [i for i in range(1, 6)]
## passsing list using the *
1 2 3 4 5
nums = (i for i in range(1, 6))
## passsing tuple using the *
print(*nums, sep = ", ")
1, 2, 3, 4, 5

3.2. Packing Elements

Packing means capturing multiple elements at once. We use this feature in function calls.

def average(*nums):
    return sum(nums) / len(nums)

Now, we can pass any number of integers to calculate the average. The function average captures all the arguments as a list.

## calling the average with some numbers
print(average(1, 2, 3, 4, 5))

We have seen how to capture the multiple elements into a list using *. Now, we will learn how to capture various key-value pair elements using **.

def _object(name, **properties):
    print(name, properties)

_object("Car", color="Red", cost=999999, company="Ferrari")
Car {'ceo': 'Louis', 'color': 'Red', 'cost': 999999, 'company': 'Ferrari'}

4. Keyword-Only Arguments with Positional Arguments

Arguments with the name are called keyword arguments. And the other arguments are called positional arguments. First, you have to understand the difference between keyword and positional arguments. Let's see an example of it.

## name is a positional argument
## color and cost are keyword arguments
def sample(car, color = None, cost = None):
    print("Car:-{} Color:-{}, Cost:-{}".format(car, color, cost))

We can use the name of the parameter to pass the arguments while calling a function for keyword arguments. If we don't give anything for the keyword arguments, they will take the default value provided in the function definition.

We can specify the keyword arguments using the keywords or position of the arguments.

sample("Ferrari", "Red", 999999)
Car:-Ferrari Color:-Red, Cost:-999999
sample("Ferrari", cost = 999999, color = "Red")
Car:-Ferrari Color:-Red, Cost:-999999

The order of keyword arguments is not essential if we call them using the names defined in the function.

sample("Ferrari", color = "Green")
Car:-Ferrari Color:-Green, Cost:-None

Now, you have an idea about positional and keyword arguments. Let's enter into our main topic.

Keyword-Only arguments only are specified by the keywords. We can't set them positionally as we have seen earlier. We have to declare the keyword arguments after the * arguments to capture keyword-only.

def keyword_only(*items, _list, default = False):

We have to use the _list and default keywords while calling the function as they come after the *items. If we don't use the keywords to call them, we will get an error. Let's see both cases.

nums = [i ** 2 for i in range(1, 6)]
## calling the function
keyword_only(1, 2, 3, 4, 5, _list = nums, default = True)
(1, 2, 3, 4, 5)
[1, 4, 9, 16, 25]

What if we call the function keyword_only using the positional arguments.

nums = [i ** 2 for i in range(1, 6)]
## calling the function will raise an error
keyword_only(1, 2, 3, 4, 5, nums, False)

TypeError                                 Traceback (most recent call last)

<ipython-input-85-bf737d8a8bfc> in <module>()
      1 nums = [i ** 2 for i in range(1, 6)]
----> 2 keyword_only(1, 2, 3, 4, 5, nums, False)

TypeError: keyword_only() missing 1 required keyword-only argument: '_list'

We have an error stating that we are missing one required keyword-only argument. So, we have to use the keywords while calling the keyword-only arguments. Keyword-Only arguments are defined only after the * arguments in functions.

5. Keyword-Only Arguments without Positional Arguments

If we want to accept only Keyword-Only arguments without any positional arguments, Python allows us to use * in function parameters to achieve this. Let's see an example.

def _sample(*, name):

The above function takes only the keyword-only arguments. We have to pass the argument with the keyword. Otherwise, we will get an error. * in the function definition is to make sure that the function only takes arguments using the keywords. Any parameters we define after the * parameter in the function must specify with the keywords during the function call.

## calling the function using keyword 'name'
_sample(name = "Datacamp")
## calling the function without using keyword 'name'
## we will get a TypeError

TypeError                                 Traceback (most recent call last)

<ipython-input-12-fe0d3718b56c> in <module>()
      1 ## calling the function without using keyword 'name'
      2 ## we will get a TypeError
----> 3 samp("Datacamp")

TypeError: samp() takes 0 positional arguments but 1 was given

It's clear that the _sample() function only takes the keyword-only* arguments.


Developers are adding new features in every new release of Python. Asterisks are special and reduce some parts of the code. We can't accept the multiple positional arguments without *, and keyword-only arguments also need * to implement that feature. Other operations of asterisks may be achieved with multiple lines of code. Practice them to understand it fully. Thanks for reading!

If you would like to learn more about Python, take DataCamp's Python Data Science Toolbox (Part 1) course.

Happy Coding :)

Learn more about Python

Certification available

Introduction to Python

BeginnerSkill Level
4 hr
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow

Google Cloud for Data Scientists: Harnessing Cloud Resources for Data Analysis

How can using Google Cloud make data analysis easier? We explore examples of companies that have already experienced all the benefits.
Oleh Maksymovych's photo

Oleh Maksymovych

9 min

A Guide to Docker Certification: Exploring The Docker Certified Associate (DCA) Exam

Unlock your potential in Docker and data science with our comprehensive guide. Explore Docker certifications, learning paths, and practical tips.
Matt Crabtree's photo

Matt Crabtree

8 min

Bash & zsh Shell Terminal Basics Cheat Sheet

Improve your Bash & zsh Shell skills with the handy shortcuts featured in this convenient cheat sheet!
Richie Cotton's photo

Richie Cotton

6 min

Functional Programming vs Object-Oriented Programming in Data Analysis

Explore two of the most commonly used programming paradigms in data science: object-oriented programming and functional programming.
Amberle McKee's photo

Amberle McKee

15 min

A Comprehensive Introduction to Anomaly Detection

A tutorial on mastering the fundamentals of anomaly detection - the concepts, terminology, and code.
Bex Tuychiev's photo

Bex Tuychiev

14 min

Pandas Profiling (ydata-profiling) in Python: A Guide for Beginners

Learn how to use the ydata-profiling library in Python to generate detailed reports for datasets with many features.
Satyam Tripathi's photo

Satyam Tripathi

9 min

See MoreSee More