-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs and kwargs.py
More file actions
73 lines (56 loc) · 1.75 KB
/
args and kwargs.py
File metadata and controls
73 lines (56 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
61
62
63
64
65
66
67
68
69
70
71
72
73
# # *args are you used to make parameter values limit infinite
# Normal function
def function_name_print(a, b, c, d):
print(a, b, c, d)
# function_name_print("Nitesh", "Mohan", "Sohan", "Harry")
# Using *args
# def funargs(*args):
# # print(type(args))
# for items in args:
# print(items)
name = ["Nitesh", "Mohan", "Sohan", "Harry"]
# funargs(*name)
# args convert data type into tuple
# we can also send normal value to function with *args
# we need to give normal arguments first and then *args ------ Convention
# def funargs(pre_name, *args): #if you def funargs(*args, per_name): ---- error
# # print(type(args))
# print(pre_name)
# for items in args:
# print(items)
# name = ["Nitesh", "Mohan", "Sohan", "Harry", "Coder"]
first_name = "Google"
# funargs(first_name, *name)
# # **kwargs
# **kwargs are used with dictionaries
# def funkwargs(**kwargs):
# print(type(kwargs))
# for key, value in kwargs.items():
# print(f"{key} is a {value}")
# kw = {
# "Karan":"Class teacher",
# "Nitesh":"Monitor",
# "Rohan":"Front bencher",
# "Sohan":"Back bencher"
# }
# funkwargs(**kw)
# we can also use normal parameters with **kwargs also with *args
# we must give normal parameters before *args and **kwargs
# sequence of using normal parameters, *args and **kwargs
# 1. Normal parameters
# 2. *args
# 3. **kwargs
def funkwargs(pre_name, *args, **kwargs):
print(pre_name)
for items in args:
print(items)
print("\nIntroducing **Kwargs")
for key, value in kwargs.items():
print(f"{key} is a {value}")
kw = {
"Karan":"Class teacher",
"Nitesh":"Monitor",
"Rohan":"Front bencher",
"Sohan":"Back bencher"
}
funkwargs(first_name, *name, **kw)