[Go to site: main page, start]

Asterisks

Python has an * prefix operator and a ** prefix operator that can be used in many different ways.

The below screencasts & articles explain each of the many uses of the * and ** operators in Python.

7 articles · 21 min read
7 screencasts · 21 min watch
01
Accepting any number of arguments to a function
3 min read Screencast available

To make a function that accepts any number of arguments, you can use the * operator and then some variable name when defining your function. Some of Python's built-in functions work this way.

Read
Read
02
Accepting arbitrary keyword arguments
3 min read Screencast available

Ever seen **kwargs in a function definition? There's nothing special about the name "kwargs": it's the ** that's special. You can use Python's ** operator to define a function that accepts arbitrary keyword arguments.

Read
Read
03
Keyword-only function arguments
4 min read Screencast available

If you see a function that has an asterisk (*) on its own with a comma after it, every argument after that point is a keyword-only argument (an argument which can only be specified by its name).

Read
Read
04
Extended iterable unpacking
3 min read Screencast available Premium

Tuple unpacking is great when you know the size of the iterable that you're unpacking, but you can even use it if you don't know the size of your iterable (by capturing remaining values).

Read
Read
05
Unpacking iterables into function arguments
2 min read Screencast available Premium

In Python, you can unpack the items in an iterable into separate positional arguments in a function (sometimes called "variadic arguments"). This works even if you don't know how long the iterable is.

Read
Read
06
Unpacking iterables into iterables
3 min read Screencast available Premium

Have multiple iterables that you'd like to merge together into a new list? If you have just lists, you could concatenate. For other iterables, you can unpack your iterable into a new list instead.

Read
Read
07
Unpacking arbitrary keyword arguments into a function call
3 min read Screencast available Premium

Often seen as **kwargs, Python's double-asterisk syntax can be used for capturing arbitrary keyword arguments in function definitions.

Read
Read
Continue exploring
Profile picture of Trey
Learn something new about Python every week

My name is Trey Hunner. I publish new Python articles and screencasts every week through Python Morsels. If you want to learn something new about Python every week, join Python Morsels!

Join Python Morsels ✨