Sign in

US Are you Python Expert? You should definitely know this uses of Asterisks then

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.

https://www.residentadvisor.net/events/1430244

https://www.residentadvisor.net/events/1430245

https://www.residentadvisor.net/events/1430247

https://www.residentadvisor.net/events/1430249

https://www.residentadvisor.net/events/1430251

https://www.residentadvisor.net/events/1430252

https://www.residentadvisor.net/events/1430253

https://www.residentadvisor.net/events/1430254

https://www.deviantart.com/crawfordvsbrookfight/commission/HOW-TO-WATCH-Crawford-vs-Brook-Live-Stream-REDDIT-1408381

https://medium.com/@boxing2020/26-year-old-programmer-built-a-1-billion-app-in-2-years-after-following-his-girlfriends-advice-fd65ebc02d3b

https://mediastudies.as.virginia.edu/sites/mediastudies.as.virginia.edu/files/webform/crawford-vs-kell-brook-live-ppv-01.pdf

https://mediastudies.as.virginia.edu/sites/mediastudies.as.virginia.edu/files/webform/crawford-vs-kell-brook-live-ppv-02.pdf

https://mediastudies.as.virginia.edu/sites/mediastudies.as.virginia.edu/files/webform/crawford-vs-kell-brook-live-ppv-03.pdf

https://mediastudies.as.virginia.edu/sites/mediastudies.as.virginia.edu/files/webform/crawford-vs-kell-brook-live-ppv-04.pdf

https://mediastudies.as.virginia.edu/sites/mediastudies.as.virginia.edu/files/webform/crawford-vs-kell-brook-live-ppv-05.pdf

https://mediastudies.as.virginia.edu/sites/mediastudies.as.virginia.edu/files/webform/crawford-vs-kell-brook-live-ppv-06.pdf

https://mediastudies.as.virginia.edu/sites/mediastudies.as.virginia.edu/files/webform/crawford-vs-kell-brook-live-ppv-07.pdf

https://mediastudies.as.virginia.edu/sites/mediastudies.as.virginia.edu/files/webform/crawford-vs-kell-brook-live-ppv-09.pdf

https://mediastudies.as.virginia.edu/sites/mediastudies.as.virginia.edu/files/webform/crawford-vs-kell-brook-live-ppv-08.pdf

https://medium.com/@boxing2020/steve-jobss-definition-of-smart-will-reprogram-your-idea-of-intelligence-166a192f080c

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 .
  • A parameter prefixed by two can capture any number of keyword arguments into a .

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

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:

Combine a list, a tuple and a set into one list

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

A better method is using list comprehensions:

Combine iterables by 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. 🎉 🎉

The simplest way to combine iterables

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 , 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 by asterisks.

Unpack 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 :

More from Irenemdickens

Follow

Oct 31

Best Features of Iphone 12 that you will get in love with

Apple’s first 5G smartphone takes us back to a design aesthetic we didn’t know we missed, marrying it to modern touches introduced with the iPhone X to mostly positive affect. There are 5 big reasons I love this smartphone. Granted, there are some caveats, too.

A gorgeous design

https://www.apec.org/cux/dream_scotland-wales-v-geelng.html

Read more · 10 min read

Oct 25

This Beloved Coffee Chain Just Announced It’s Closing All Locations

The latest business casualty of the coronavirus pandemic is one of best quality coffee chains in the country. On Oct. 21, Brooklyn Roasting Company announced that it was filing for Chapter 11 bankruptcy protection and would be closing all of its brick-and-mortar locations in New York City, The New York Post reports. Read on to learn more, and for another business that’s closing up shop, check out This Popular Gym Is Closing All of Its Locations.

Brooklyn Roasting Company was first founded in 2009 by Jim Munson, a former partner in The Brooklyn Brewery. The company eventually expanded to seven locations, as well as merchandise and a coffee subscription service. Four employees were trained up as “Q Graders,” the coffee industry’s equivalent of being a master sommelier (for reference, there are only 4,000 Q Graders in the entire global coffee industry). …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store