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