+ 8

What is fuction of '*' and '**' in python??(list, dict, etc)

Explain Function of *args and **kwargs and where they can be used.

3rd Jun 2018, 11:40 AM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
12 Antworten
+ 8
def foo(a, *b, **c) : print(a) print(b) print(c) foo(1,2,3,num=4,age=15) #output 1 (2, 3) {'num': 4, 'age': 15} *args is used when you need "not predefined length of function arguments". In the example, the first argument will be assigned to "a" as usual. and the rest of the arguments will all be assigned to *args (*b), which is a tuple. **kwargs means keyword arguments, this kind of arguments will be assigned, if the user of this function enter something like xxx=ooo. **kwargs (**c) is a dictionary. Remember that kwargs should always placed after args when using the function.
3rd Jun 2018, 12:05 PM
李立威
+ 5
GODOFPC I just looked at those * and ** and I gave answer. 😅
3rd Jun 2018, 11:50 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 5
1_coder Yes it works Thanks😄
7th Jun 2018, 5:33 AM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
+ 4
Thanks Again😃🙃李立威
3rd Jun 2018, 12:32 PM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
+ 4
Great Explaination 👏
3rd Jun 2018, 12:33 PM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
+ 3
* is used for multiplication and ** is used to represent power. example. 2 * 3 = 6(2 * 3), but 2 ** 3 = 8(2 * 2 * 2)
3rd Jun 2018, 11:46 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 3
Raj Chhatrala Bro I mean in lists *args and **kwargs if we print a list with asterisk before then square brackets will be gone and commas too But I dont know the functuon of **kwargs and their proper use BTW Thnxx for ur attention🙂
3rd Jun 2018, 11:48 AM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
+ 3
Raj Chhatrala Its ok Actually I forgot to mention lists😅
3rd Jun 2018, 11:50 AM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
+ 3
李立威 Thanks 🤩👍 Very nice Explaination 👏 Its clear to me now✌️
3rd Jun 2018, 12:16 PM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
+ 3
Oh, another usage of * before a list or any multiple elements data works like this. tup=(1, 2, 3) print(*tup) #output 1 2 3 First, note that *tup can only be used in another function or method,( i. e. writing *tup itself without wrapped in another function such as print is a syntax error). Given "tup" to be any data structure that may contain multiple elements in Python (e. g. list, tuple, dictionary, set and even generator). f(*tup) will first unwrap all the elements within tup, and then throw them back into the function f. At this point, if the function "f" takes exactly 3 arguments for example, but len(tup) is not 3, it will cause an error. This is a situation when we need "not pre-defined length of arguments" in a function. For example, print(*tup) is equivalent to print(1,2,3) A good way for me to memorize these usage, is that the * (asterisk) sign means "all" in many cases. Reminds something like: from OOO import * The * sign means "all".
3rd Jun 2018, 12:19 PM
李立威
+ 2
A related use of * is for unpacking. a, b, *c = (1, 2, 3, 4) a == 1 b == 2 c == [3, 4] Most commonly we use this to "consume" extra values from a function return when we only want the first one(s). success, *_ = func_that_returns_mult_values()
7th Jun 2018, 12:49 AM
1_coder
+ 1
* ex. 2*3=6 **ex. 2**3=8
28th Jun 2018, 10:43 PM
Tanner