Unpacking Variables in Python | how to use *argsv and **kwargs

  Рет қаралды 12,708

Oggi AI - Artificial Intelligence Today

Oggi AI - Artificial Intelligence Today

Күн бұрын

Пікірлер: 41
@crushthedarkforce
@crushthedarkforce 4 жыл бұрын
split() returns substrings, not integers. So if you do a,b,c = '4 5 6'.split(), a will be a string '4', b will be a string '5', and c '6'. Then if you 'add' a and b, they will be concatenated as '45'
@raibahati
@raibahati 3 жыл бұрын
Absolutely, I have noticed too that he made a mistake. Split() is a string method and it returns a list of substrings. So each value will have to be of the string class and not of the int class.
@Broughtvulture
@Broughtvulture 2 жыл бұрын
Thanks for the tutorial, i like how you went through examples while you explained it. Not many people do that
@fe3tpls36
@fe3tpls36 3 жыл бұрын
May god bless your soul i was looking everywhere for a basic explanation and i found your channel.
@williamli55
@williamli55 3 жыл бұрын
I love that mGaetz falls into the same category as Betamax.
@rch308
@rch308 2 жыл бұрын
Matt Gaetz = loser. That's great
@gabehcuodsuoitneterp203
@gabehcuodsuoitneterp203 2 жыл бұрын
Wooow - **Great** teacher! 👍
@mehmetkaya4330
@mehmetkaya4330 2 жыл бұрын
Very well explained! Thanks
@DoYouHaveAName1
@DoYouHaveAName1 2 жыл бұрын
Thank you for your videos! If there's the option, I always choose to see your explanation
@omt2526
@omt2526 2 жыл бұрын
Great work, simple and very helpful. Thank you
@akiratoriyama1320
@akiratoriyama1320 4 жыл бұрын
Great tutorial sir!!
@SaraKolvinsky
@SaraKolvinsky 2 жыл бұрын
@4:58 Thanks for the details about this!
@alokgupta168
@alokgupta168 2 жыл бұрын
a,b,c='4 5 6'.split() # a, b and c will be type str not integer
@oggiai
@oggiai 2 жыл бұрын
Yes I may have a mistake in my video- it doesn’t automatically cast it to an int.
@alokgupta168
@alokgupta168 2 жыл бұрын
@@oggiai yes but overall your video is great
@ianmasie6550
@ianmasie6550 4 жыл бұрын
Great job explaining this! Thank you!
@IanVidaurre
@IanVidaurre 4 жыл бұрын
That was explained wonderfully, thank you!
@jk3089
@jk3089 4 жыл бұрын
Great sharing, Sir!
@abdullahrazzaq3553
@abdullahrazzaq3553 2 жыл бұрын
عاشت ايدك يا سبع كلش افتهمت اني 😍😘❤
@Dequiter
@Dequiter 4 жыл бұрын
Thank you so much!!!
@eniocc
@eniocc 4 жыл бұрын
Thank you Sir.
@amalkrishna5118
@amalkrishna5118 2 жыл бұрын
thank u uncle i was looking for my board exams
@Vikram-wx4hg
@Vikram-wx4hg 2 жыл бұрын
Very useful tutorial on the topic and very sharp. Thanks.
@ireneanibogwu7242
@ireneanibogwu7242 4 жыл бұрын
Wow this was great. Thank you!
@user-or7ji5hv8y
@user-or7ji5hv8y 3 жыл бұрын
Super clear
@jeffreyconboy1626
@jeffreyconboy1626 2 жыл бұрын
do we know why when packing a dict with **kwargs that you cant use the standard dictionary syntax (a:100,b:200). Great video btw thanks
@claffert
@claffert 2 жыл бұрын
When you are using **kwargs for a parameter, you are telling it that what you are giving it is going to be packed into a dictionary. It's not expecting a dictionary, it's expecting keys and values with which it will create a dictionary. So, the reason it doesn't work is that isn't what is being expected. However, you CAN use the dictionary syntax, if you the ** operator to unpack a dictionary into names and values, which then would be repacked in the function. The following is legal syntax: func(**{'a':100, 'b':100})
@محمودمتولي-ق8ذ
@محمودمتولي-ق8ذ 2 жыл бұрын
اشهد ان لا اله الا الله اشهد ان محمد رسول لله
@abodawead9039
@abodawead9039 2 жыл бұрын
you will not be able to do calculate on split return elements , because it returns strings "sub_strings" . do type(a or b or c ) . do int(a or b or c ) first , man focus . good job .
@joserobins
@joserobins 3 жыл бұрын
Great insights and explanation. losers['c'] makes so much sense in that particular category! cheers
@edwardmacnab354
@edwardmacnab354 2 жыл бұрын
Not exactly what I'd call packing and unpacking but hey , It's only Python so no harm done I guess
@oggiai
@oggiai 2 жыл бұрын
That’s what Python calls packing and unpacking. And it’s useful to know
@edwardmacnab354
@edwardmacnab354 2 жыл бұрын
@@oggiai I hate to be the bearer of bad news , but, Python is almost as idiosyncratic and weird as Perl is. I think I'll stick to C/C++ , at least it makes sense consistently !
@AP-qs2zf
@AP-qs2zf 3 жыл бұрын
still don't get the point of **kwargs or why it's useful
@oggiai
@oggiai 3 жыл бұрын
it's just for passing an unknown number of key-value pairs to a function. There are other ways to do that (simply passing a dict), so I agree **kwargs is not extremely useful. But as a programmer you often need to figure out how other peoples' code works, so you should learn these nuances of the language.
@claffert
@claffert 2 жыл бұрын
There are many uses, but one of the most common is simply if you want to have lots of options when calling a function but you don't want to have make a big long list of function parameters for all possible options, especially when most of the time you only need a small number of required parameters. For example, if you have a function that sorts list, you could have additional options such as whether you want it in reverse sorted, or just the first n values, or some other feature. You could make those additional options supported by **kwargs. If you don't need them, they can be left out entirely. If you don't include them in the function call, it doesn't throw an error and just makes a zero length dictionary. When you do want those additional options, just include the ones you want.
@claffert
@claffert 2 жыл бұрын
and I have to add, I strongly disagree about it being "not extremely useful". A lot of the built in functions of Python use this very feature. And understanding it isn't just for understanding other people's code, but for understanding a lot of the official documentation. I for one have written a lot of pieces of code that were a bit dodgy with additional arguments that were only needed in some special cases that I later realized, "I should have just used **kwargs for that".
@flioink
@flioink 2 жыл бұрын
@@claffert Ok, but then your function has to check if those additional are present, no?
@claffert
@claffert 2 жыл бұрын
@@flioink Yes, it does require a bit of additional work when creating the function, but it provides you with more flexibility when calling it. Another common use-case for it is if you are creating a command line programs. A typical command line program often has standard use cases that involve a small number of options, but has large numbers of optional options and parameters that can be used. If you ever use command line programs then you should be very familiar with the pattern, and this is how you can implement it.
@fwily2580
@fwily2580 Жыл бұрын
So why is mGaetz a loser?
Python: Lambda, Map, Filter, Reduce Functions
9:59
Oggi AI - Artificial Intelligence Today
Рет қаралды 384 М.
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
Trick-or-Treating in a Rush. Part 2
00:37
Daniel LaBelle
Рет қаралды 46 МЛН
СКОЛЬКО ПАЛЬЦЕВ ТУТ?
00:16
Masomka
Рет қаралды 3,1 МЛН
How Much Tape To Stop A Lamborghini?
00:15
MrBeast
Рет қаралды 202 МЛН
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Top 18 Most Useful Python Modules
10:50
Tech With Tim
Рет қаралды 933 М.
Optional Arguments in Python With *args and **kwargs
10:44
Real Python
Рет қаралды 125 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 815 М.
Mastering Python - Everything You Need To Know To Become a Python Master
16:02
Python Dictionary Comprehensions
13:03
Oggi AI - Artificial Intelligence Today
Рет қаралды 1,3 М.
Players push long pins through a cardboard box attempting to pop the balloon!
00:31