-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09thFunctionsinPython.py
More file actions
158 lines (81 loc) · 3.49 KB
/
09thFunctionsinPython.py
File metadata and controls
158 lines (81 loc) · 3.49 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# ! Functions
def areaofrectangle(a, b):
return a * b
print('Area is:', areaofrectangle(5, 2))
def areaoftriangle(a, b):
return 0.5 * a * b
print('Area of Triangle is:', areaoftriangle(5, 2))
def name(a):
return f'First Name is {a.split()[0]}\t and\t Last Name is {a.split()[-1]}'
a = 'Hamza Shabbir Sahapurwala'
print(f'The output is :',name(a))
def d():
print('Hello')
d()
print(d()) # * For functions which have no return statements ans they are still called, they return None
d1 = d # * We are assigning function to another name
d()
del d # * We delete the old name
d1() # * This only works with new function name
def a(a,b,c):
return a,b,c
print(a(1,2,3)) # * Returns a tuple
# There are 4 scenarios of Functions:
# With Argument, With return
# With Argument, Without return
# Without Argument, Without return
# Without Argument, With return
# ! Positional and Keyword Arguments
def b(c, n = 'Lol', a = 7): # * c is a positional argument while n and a are keyword arguments
print(f'The name is {n} and my age is {a} and I got to go to {c}.')
b('Hhahaha',4) # * c = Hhahaha, n = 4 & a = 7(Cause a has default value)
b('Noice',a = 3, n = 'Bwahahahah') # * c = 'Noice'
b('Noice','AHAHAHHAHA',6) # * c = 'Noice', n = 'AHAHAHHAHA', a = 6
b(n = 'Yolo', c = 'Noice') # * We can change the position of c if we treat it like a keyword argument
# b(a = 7, n = 'SUI', 'Noice') # ! This won't work as for both in function header and in caller, the positional(non-default) argument/parameter is ALWAYS first
# ! Positional Arguments can't be inbetween or after the keyword(default) arguments/parameters
def aa(x,y):
print(x - y)
x = 3; y = 4 # * ; acts as a separator so that x can have 3 as value and y has value 4
# aa(x,y)
a = eval(input('List:'))
print(type(a))
def bbb(*args):
# * args returns a tuple
print(args)
print('With *:',*args) # * Returns the input but can't do anything with that
# * args by itself is a tuple but *args is just a starred expression
# for i in *args: # * This doesn't work
# print(i)
for i in args:
print(i)
bbb('A','B','C','D')
def myFun(arg1, **kwargs):
# * kwargs is now a dictionary with the 'before =' as the keys and '=after' as the values
for key, value in kwargs.items():
print("%s == %s" % (key, value))
# Driver code
myFun("Hi", first='Geeks', mid='for', last='Geeks')
def myFun(*args, **kwargs):
print("args: ", args)
# print('**kwargs: ',**kwargs) # * This doesn't work as all the first="Geeks", mid="for", last="Geeks" thing is just copy pasted and print is a function
print("kwargs: ", kwargs)
# * Now we can use both *args ,**kwargs
# * to pass arguments to this function :
myFun('geeks', 'for', 'geeks', first="Geeks", mid="for", last="Geeks")
def myFun(arg1, arg2, arg3):
print("arg1:", arg1) # * for kwargs: arg1 = for
print("arg2:", arg2) # * for kwargs: arg2 = Geeks
print("arg3:", arg3) # * for kwargs: arg3 = Geeks
# * Now we can use *args or **kwargs to
# * pass arguments to this function :
args = ("Geeks", "for", "Geeks")
myFun(*args)
kwargs = {"arg3": "Geeks", "arg1": "for", "arg2": "Geeks"}
myFun(**kwargs) # * the positional parameter in the function should have the same name
# * as the keys in the dictionary named kwargs so that some output is achieved
def f1(a,b,*ar):
print(a,b,*ar) # * a = 12, b = 22, *ar = 2 6
# * prints 12 22 2 6
f1(12,22,2,6)
# * * and ** make is easier to take tuple and dictionary input