The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial

Patrick Loeber · Intermediate ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

The video tutorial covers the various use cases of the asterisk (*) operator in Python, including multiplication and power operations, creation of lists/tuples/strings with repeated elements, variable length arguments, keyword-only parameters, argument unpacking, and merging containers.

Full Transcript

hi everybody and welcome to your new python tutorial so in this tutorial we will talk about the different use cases of the asterisk or star sign in Python so it can be used for multiple different cases like multiplication and power operations the creation of lists or tuples with repeated elements for arcs quarks and keyword only parameters for unpacking lists tuples or dictionaries into function arguments for unpacking containers and for merging containers into a list or merging two dictionaries so we will have a look at all of these use cases first of all of course there is the simple multiplication operation so let's say result equals 5 times 7 and then if I print the result then this will print the multiplication of these two or if I use two stars or two asterisks let's say two and then two stars and then a four this will be a power operation so this is 2 to the power of 4 equals 16 this is one use case then it can be used to create lists tuples or strings with repeated elements so let's say I want to have a list called C rows equals and then I write one element so I say a one item here so C row and then I write times 10 so this will create a list with 10 elements and each element has 0 so this is my list I can also put in multiple initial items here so if I write it like this then this will repeat 0 and 1 10 times I can also use a 2 people here and it also works with strings so if I say let's say a B here then this will create a new string with ten times a B so next is to use the star or asterisk for the arts and quartz and keyword only argument so if you don't know what this means please watch my last video about function arguments so probably you've seen a function that looks like this so that define a function called foo and then it has some arguments and then also some arcs and with one star and with Clark's with two stars so and then let's print a and now arcs is a tuple so I can go over this tuple for arc in arcs and then print arc and Clark's here is a dictionary so I can say for key in quarks and then print the key and also the dictionary value of this key and now I can call this function with the a and B arguments so let's say one and two and then for this arcs I can use as many positional arguments as I like so I can say three four five and then I can also use as many keyword arguments as I want so I can say for example 6 equals 6 and 7 equals 7 so this will print my function here forgot the beer and then if I only use one star here and then another parameter here then all parameters after the star are keyword only parameter so if I want to print see I cannot call it call the function like this so because here the last item must be a keyword argument so I have to write C equals 3 and then it will work so this is another use case of the star operator to enforce keyword only arguments then we can also use the asterisk for argument unpacking so let's say I have a list my list equals and it has three elements so 0 1 2 then I can call this function and unpack this list here with one star and then my list so this will work and the only thing that is important here is this is that the number of arguments must match the number of parameters here the number of elements in the list must match the number of parameters here so if I have another one then this will raise an error and this will also work with a tuple and if I have a dictionary so let's say my dict and then this must have the parameter names as key so a and then a value 1 B and the value and see and the value then I can unpack this dictionary with two stars and then my ticked so this will work and also the number of elements must match the number of parameters here and also the key the keys the name of the keys must match the name of the parameters so if I have a different key here then this won't work then the asterisk can be used for unpacking containers so it can unpack the elements of a lists tuple or sets into single and multiple remaining elements so let's say I have a list called numbers and this is let's say 1 2 3 4 5 6 then I can unpack them let's say I write star and then beginning and then a last value and this is equal numbers so let's print beginning and let's print last so this will unpack all the elements except the last one into a list and then it will unpack the last item into a single number and yeah be careful here this will always unpack your elements into a list so if I have a tuple here then unpacking works but is it will still be a list here so if I run this then it looks like this I can also unpack the or put the star sign for the last item so the we'll unpack the first number into the first element into one number and all the remaining elements into a list that is now called last or I can use this in the middle so I can say beginning and then star middle and then last so and then I can print the middle here so now middle is my list with the elements between so if I run this it will print this and for example I can also unpack more numbers into single elements so I can say second-last and then here print second-last so this is how we can unpack multiple items into a list and we can also use the star operator to merge it rebels into a list so for example if I have one tuple with elements one two and three and then I have another list so my list equals four five six and then I can say second or let's say new list equals and then I say I in brackets I put my first iterable here so I can say star and then my tuple and then I can put in the second iterable here so my list so if I print the new list and this will be a new merged list and I can also use a set here so if I use a set here my set then this will also work so this merging works for lists tuples and sets into a list or I can merge two dictionaries so if I have one dictionary call it dict a equals and then some elements here so let's say a + 1 + b + 2 and then I have a second dictionary so let's say dict B this has the key C and D with the values 3 & 4 and then I can create another dictionary so let's say my dick equals and then inside these square brackets I use 2 stars and then the first dictionary and then comma and then again two stars - unpacked the second dictionary so this will merge multiple dictionaries into one dictionary now if I print this then I can see that I have one dictionary now and yeah I think that's all the important use cases of the asterisk sign and I hope you enjoyed this tutorial and see you in the next tutorial where we will talk about shallow and deep copying in Python

Original Description

In this Python Advanced Tutorial, I will talk about the asterisk (*) or star operator in Python. I will talk about the different use cases: - Multiplication and power operations - Creation of list / tuple / string with repeated elements - Variable length arguments (*args and **kwargs) - Keyword only arguments - Unpacking container data types into function arguments - Unpacking container into single and multiple elements - Merging lists / dictionaries ~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~ ✅ Write cleaner code with Sourcery: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer * 📚 Get my FREE NumPy Handbook: https://www.python-engineer.com/numpybook 📓 Notebooks available on Patreon: https://www.patreon.com/patrickloeber ⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN A written Tutorial can be found here: https://www.python-engineer.com/courses/advancedpython/19-asterisk/ You can find me here: Website: https://www.python-engineer.com Twitter: https://twitter.com/patloeber GitHub: https://github.com/patrickloeber #Python ---------------------------------------------------------------------------------------------------------- * This is a sponsored link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Patrick Loeber · Patrick Loeber · 19 of 60

1 Lists in Python - Advanced Python 01 - Programming Tutorial
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
2 Tuples in Python - Advanced Python 02 - Programming Tutorial
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
3 Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
4 Sets in Python - Advanced Python 04 - Programming Tutorial
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
5 Strings in Python - Advanced Python 05 - Programming Tutorial
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
6 Collections in Python - Advanced Python 06 - Programming Tutorial
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
7 Itertools in Python - Advanced Python 07 - Programming Tutorial
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
8 Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
9 Exceptions in Python - Advanced Python 09 - Programming Tutorial
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
10 Logging in Python - Advanced Python 10 - Programming Tutorial
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
11 JSON in Python - Advanced Python 11 - Programming Tutorial
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
12 Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
13 Decorators in Python - Advanced Python 13 - Programming Tutorial
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
14 Generators in Python - Advanced Python 14 - Programming Tutorial
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
15 Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
16 Threading in Python - Advanced Python 16 - Programming Tutorial
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
17 Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
18 Function arguments in detail - Advanced Python 18 - Programming Tutorial
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
20 Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
21 Context Managers in Python - Advanced Python 21 - Programming Tutorial
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
22 KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
23 Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
24 Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
25 Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
26 Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
27 Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
28 SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
29 Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
30 Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
31 Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
32 PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
33 K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
34 Anaconda Tutorial - Installation and Basic Commands
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
35 PyTorch Tutorial 01 - Installation
PyTorch Tutorial 01 - Installation
Patrick Loeber
36 PyTorch Tutorial 02 - Tensor Basics
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
37 PyTorch Tutorial 03 - Gradient Calculation With Autograd
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
38 PyTorch Tutorial 04 - Backpropagation - Theory With Example
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
39 PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
40 PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
41 PyTorch Tutorial 07 - Linear Regression
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
42 PyTorch Tutorial 08 - Logistic Regression
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
43 PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
44 PyTorch Tutorial 10 - Dataset Transforms
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
45 Download Images With Python Automatically - Python Web Scraping Tutorial
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
46 PyTorch Tutorial 11 - Softmax and Cross Entropy
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
47 Select Movies with Python - Web Scraping Tutorial
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
48 PyTorch Tutorial 12 - Activation Functions
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
49 List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
50 PyTorch Tutorial 13 - Feed-Forward Neural Network
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
51 How To Add A Progress Bar In Python With Just One Line - Python Tutorial
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
52 PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
53 The Walrus Operator - New in Python 3.8 - Python Tutorial
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
54 PyTorch Tutorial 15 - Transfer Learning
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
55 YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
56 YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
57 YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
58 YouTube Data API Tutorial with Python - Analyze the Data - Part 4
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
59 AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
60 Ultimate FREE Study Guide for Machine Learning and Deep Learning
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber

This tutorial covers the asterisk operator in Python, including its use cases for multiplication, list creation, variable length arguments, and container merging. By the end of this tutorial, you will be able to apply the asterisk operator to various Python programming tasks.

Key Takeaways
  1. Use the asterisk operator for multiplication and power operations
  2. Create lists/tuples/strings with repeated elements using the asterisk operator
  3. Implement variable length arguments (*args and **kwargs) in Python functions
  4. Use keyword-only parameters in Python functions
  5. Unpack arguments using the asterisk operator
  6. Merge containers (lists, tuples, sets, dictionaries) using the asterisk operator
💡 The asterisk operator is a versatile operator in Python that can be used for various operations, including multiplication, list creation, and container merging.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →