avatarYang Zhou

Summary

This article discusses five uses of asterisks in Python, including multiplication or exponentiation, receiving an unlimited number of arguments, restricting to keyword-only arguments, iterables unpacking, and extended iterable unpacking.

Abstract

The article "5 Uses of Asterisks in Python" explains the various uses of the asterisk symbol in Python programming. The asterisk is commonly known as the multiplication operator, but it has other uses in Python. The article begins by explaining the use of asterisks as infix operators for multiplication and exponentiation. It then discusses how asterisks can be used to receive an unlimited number of arguments in a function, making it more flexible. The article also explains how asterisks can be used to restrict a function to receive only keyword arguments. The fourth use of asterisks is for iterables unpacking, which can make code more elegant and clear. Finally, the article discusses extended iterable unpacking, which allows for the specification of a "catch-all" name that will be assigned a list of all items not assigned to a regular name. The article provides examples and explanations for each use of asterisks in Python.

Bullet points

  • The asterisk is commonly known as the multiplication operator in Python.
  • Asterisks can be used as infix operators for multiplication and exponentiation.
  • Asterisks can be used to receive an unlimited number of arguments in a function.
  • Asterisks can be used to restrict a function to receive only keyword arguments.
  • Asterisks can be used for iterables unpacking, making code more elegant and clear.
  • Extended iterable unpacking allows for the specification of a "catch-all" name that will be assigned a list of all items not assigned to a regular name.
  • The article provides examples and explanations for each use of asterisks in Python.

5 Uses of Asterisks in Python

Starry Night by Vincent van Gogh

The asterisk, which is known as the multiplication operator, is a commonly used symbol in all programs. It could be enough for us to use it just as a multiplication operator. However, if you are serious about to become a Python expert. It’s time to know how useful and powerful the asterisk is in Python.

This post will explain 5 usage scenarios of asterisks with most understandable examples, from elementary to profound.

Use 1: Multiplication or Exponentiation Operator

The simplest use is to exploit asterisks as infix operators:

  • Single * for the multiplication operation.
  • Double ** for the exponentiation operation.
>>> 2*3
>>> 6
>>> 2**3
>>> 8

Use 2: To Receive an Unlimited Number of Arguments

A function is not necessarily to receive a fixed number of arguments if you need more flexibility or even not sure how many arguments will be passed. Here is the asterisks’ showtime.

As the above example shown, when defining a function, we can define a parameter prefixed with one or two asterisks to capture unlimited numbers of arguments.

  • A parameter prefixed by one * can capture any number of positional arguments into a tuple.
  • A parameter prefixed by two * can capture any number of keyword arguments into a dict.

By convention, we define a function like the following if the number of its arguments cannot be determined:

def func(*args, **kwargs):
    pass

Use 3: Restrict to Keyword-Only Arguments

A really cool usage of asterisks is to make a function can only receive keyword arguments.

An example says more than a thousand words:

As shown in the above example, just one * can restrict all following arguments must be passed as keyword arguments.

Actually, if we just would like to restrict a few arguments to be keyword-only and remain some positional arguments. We can just put the positional arguments before the asterisk.

Use 4: Iterables Unpacking

We can use asterisks to unpack iterables, which will make our programs clear and elegant.

For example, if we gonna combine different iterables, such as one list, one tuple and one set, into a new list, which is the best way?

Obviously, we can use for-loops to iterate all items and add them to a new list one by one:

This way can accomplish our mission, but the code looks so long and not very “Pythonic”.

A better method is using list comprehensions:

We reduced three for-loops to one line list comprehension. It’s Pythonic already but not necessarily the simplest!

It’s time to see how beautiful the asterisks are. 🎉 🎉

As stated above, we can use the asterisk as prefix of iterables to unpack their items.

By the way, if we exploit one single * as a prefix of a dict , its keys will be unpacked. If we exploit double asterisks ** as a prefix, its values will be unpacked. However, we must use their keys to receive the unpacked values. Because of this inconvenience, it’s not common to extract items of a dict by asterisks.

Use 5: Extended Iterable Unpacking

This unpacking syntax was introduced by PEP 3132 to make our code more elegant.

This PEP proposes a change to iterable unpacking syntax, allowing to specify a “catch-all” name which will be assigned a list of all items not assigned to a “regular” name.

A simple example:

Conclusion

The asterisk is one of the most commonly used operators in programs. Besides using as a multiplication operator, there are some elegant and powerful uses of it in Python, which will help our code become more “Pythonic”.

Thanks for reading! More relative Python tutorials :

Programming
Python
Python Programming
Technology
Python3
Recommended from ReadMedium