-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
373 lines (325 loc) · 6.28 KB
/
function.py
File metadata and controls
373 lines (325 loc) · 6.28 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# def xyz():
# print("Hello")
# xyz()
#add two values
#without reurn
# def add(x,y):
# print(x+y)
# p=9
# q=8
# res=add(p,q)
# print(res)
# print(res)
##with return type
# def add(x,y):
# z=x+y
# return z
# p=9
# q=8
# res=add(p,q)
# def add(x,y):
# z=x+y
# return z
# p=9
# q=8
# res=add(p,q)
# print(res)
# print(print("Hello"))
# def add(x,y):
# return(x+y)
# p=9
# q=8
# print(add(p,q))
#with argument with return
# def sub(x,y):
# return(x-y)
# x=6
# y=2
# print(sub(x,y))
#with argument without return
# def mul(x,y):
# print(x*y)
# p=int(input("Value:" ))
# q=int(input("value: "))
# mul(p,q)
#without argument with return
# def div():
# return(p//q)
# p=8
# q=2
# print(div())
#without argument without return
# def add():
# print(p+q)
# p=9
# q=5
# add()
#factorial using function
# def factorial(a):
# ans=1
# for i in range(1,a+1):
# ans*=i
# return ans
# num = int(input("Enter a number : "))
# answer=factorial(num)
# print(answer)
#print(n natural numbers)
# def print_nat():
# li=[]
# for i in range(11):
# li.append(i)
# return li
# numbers=print_nat()
# print(numbers)
#fibonacci series
# n=int(input("Enter value :"))
# fi=0
# se=1
# print(fi,se,end=' ')
# for i in range(n-2):
# nx=fi+se
# fi=se
# se=nx
# print(nx,end=" ")
# num=5
# fi=0
# se=1
# for i in range(num):
# print(fi,end=" ")
# fi,se =se,fi+se
#prime
# n=int(input("Enter a number : "))
# count=0
# for i in range(2,n):
# if n%i==0:
# count+=1
# if count==0:
# print("Prime")
# else:
# print("Not prime")
# n=int(input("Enter a number : "))
# count=0
# for i in range(1,n+1):
# if n%i==0:
# count+=1
# if count==2:
# print("Prime")
# else:
# print("Not prime")
# n=int(input("Enter a number : "))
# ans=True
# for i in range(2,n):
# if n%i==0:
# print("Not a prime")
# ans=False
# break
# if ans==True:
# print("prime")
#positional argument
# def add(x,y,z):
# return x+y+z
# p,q,r=23,65,43
# res=add(89,89,56)
# res=add(int(input("Enter a number :")),int(input("Enter a number :")),int(input("Enter a number :")))
# print(res)
# #default positional argument
# def add(x=0,y=0,z=0):
# return x+y+z
# # res=add()
# # res=add(10)
# # res=add(10,20)
# res=add(10,20,30)
# print(res)
#variable length positional argument
# def add(*args):
# print(args)
# print(type(args))
# add(10,20,19,20,30,23,32,33)
# def add(*n):
# sum=0
# for i in n:
# sum=sum+i
# return sum
# sum_n=add(10,20,19,20,30,23,32,33)
# print(sum_n)
# def add(*n):
# sum=0
# for i in n:
# for j in i:
# sum=sum+j
# return sum
# sum_n=add(eval(input("Enter any value :")))
# print(sum_n)
# def add(*n):
# print(n)
# print(type(n))
# add(*eval(input("Enter any value : ")))
#key-word argument
# def add(x,y,z):
# print(z)
# print(x)
# print(y)
# p=10
# q=20
# r=30
# add(z=p,y=q,x=r)
#default key-word argument
# def add(x=0,y=0,z=0):
# print(z)
# print(x)
# print(y)
# print(x+y+z)
# p=10
# q=20
# r=30
# # add(z=p,y=q,x=r)
# add()
# add(p,q)
#variable length key-word argument
# def add(**kwargs):
# print(kwargs)
# print(type(kwargs))
# add(x=10,y=20,z=30,p=5,q=4)
# def add(**kwargs):
# print(kwargs)
# print(type(kwargs))
# add(**eval(input("Enter any dict : ")))
# calling of function is called unpacking while declaration of a function is called packing.
# def add(**kwargs):
# #for i in kwargs.keys():
# #print(i)
# #for i in kwargs.values():
# #print(i)
# for i,j in kwargs.items():
# print('key = ',i,'value = ',j)
# add(x=10,y=20,z=30,p=5,q=4)
# def add(x,y=0,*z,p,**q):
# print(x)
# print(y)
# print(z)
# print(p)
# print(q)
# add(10,20,30,40,50,p=5,r=4,s=3,t=9)
# def add(x,*z,y=0,p,**q):
# print(x)
# print(y)
# print(z)
# print(p)
# print(q)
# add(10,20,30,40,50,p=5,r=4,s=3,t=9)
# def add(x,p,*z,y=0,**q):
# print(x)
# print(y)
# print(z)
# print(p)
# print(q)
# add(10,p=20,r=4,s=3,t=9)
# def natural_no(n):
# for i in range(1,n+1):
# print(i)
# n=int(input("Enter any number : "))
# natural_no(n)
# def natural_no(n):
# sum=0
# for i in range(1,n+1):
# print(i)
# sum=sum+i
# print(sum)
# n=int(input("Enter any number : "))
# natural_no(n)
# def even_no(n):
# for i in range(1,n+1):
# print(2*i)
# n=int(input("Enter any number : "))
# even_no(n)
# def prime_num(n):
# count=0
# for i in range(1,n+1):
# if( n%i==0):
# count+=1
# if count==2 :
# print("prime")
# else:
# print("not prime")
# n=int(input("Enter a number :"))
# prime_num(n)
# n=int(input("Enter"))
# def prime_num(n):
# count=0
# for i in range(1,n+1):
# if( n%i==0):
# count+=1
# if count>=2 :
# return 'not_prime'
# print ('prime')
# n=int(input("Enter a number :"))
# print('prime')
# prime_num(n)
#local variable(access within a block of code)
# def display():
# x=10 #local variable
# print(x)
# display()
# print(x)
#local variable to global variable
# def display():
# global x #here x is globalised
# x=10
# print(x)
# display()
# print(x)
# def display():
# global x #here x is globalised
# x=10
# print(x)
# print(x) #x cannot be called whenever function is not called & also not globalized
# display() # x is not called global because here x is not accessable from block of code
# print(x)
#global variable
# x=10
# def show():
# print(x)
# print(x)
# show()
# print(x)
# x=10
# def show():
# x=20
# print(x)
# print(x) #x=10 global
# show()
# print(x)
# x=10
# def show():
# print(x) #variable is same
# x=20
# print(x)
# show()
# x=10
# def show():
# x=20
# print(globals()['x'])
# show()
#non local
# def show():
# x=10
# def display():
# print(x)
# display()
# show()
def show():
x=10
def display():
nonlocal x #used to take value from local variable
x=x+5
print(x)
display()
show()
def show():
x=10
def display():
nonlocal x #used to take value from local variable
x=x+5
print(x)
display()
show()