forked from Akuli/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_telusko.py
More file actions
252 lines (152 loc) · 4.07 KB
/
functions_telusko.py
File metadata and controls
252 lines (152 loc) · 4.07 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
#### Functions ####
def greet():
print("Hellow Baby")
def add(x,y,z):
print(x+y+z)
def multiplication(a,b):
print(a*b)
greet()
add(2,67,98)
multiplication(4,6)
###
def add(x,y):
z=x+y
print(z)
def multy(a,b):
c=a*b
print(c)
add(6,8)
multy(4,5)
###
def add_sub(x,y):
c=x+y
d=x-y
return c,d
result1,result2 = add_sub(4,9)
print(result1,result2)
###
### Arguments are 4 types###
#1) Position
#2) Keyword
#3) Default
#4) Veriable leanght
#1) position arguments
def person(name,age):
print(name)
print(age)
person("Raja", 16)
###
#2) Keyword arguments
def person(name,age):
print(name)
print(age)
person(age=16,name="Raja")
###
#3) Default arguments
def person(name,age=18): #if some social network websites requires min age is 18 and while enter age if user not provide it will through error. that is the case we ill use this
print(name)
print(age)
person("Raja") # if we give age then given age will over ride
# if we give age then given age will over ride
# example:
def person(name,age =18): #if some social network websites requires min age is 18 and while enter age if user not provide it will through error. that is the case we ill use this
print(name)
print(age)
person("Raja", 30)
###
#3) Variable lenght arguments
def sum(a,b):
c=a+b
print(c)
sum(6,8)
## if arguments are having multiple values
def sum(a, *b): # here *b argument alows multiple values
c=a+b
print(c)
sum(6,8,12,45,23,33) # here 6 is allocated to "a" and remining all values are allocated to "b"
# from above excution we will get error due to "a" type is 'int' and "b" type is 'tuple'
#to solve above
def sum(a, *b): # here *b argument alows multiple values
print(a) #value is 6
print(b) #value is (8, 12, 45, 23, 33)
sum(6,8,12,45,23,33) #to just verify the arguments allocation printing the a,*b
### to do actual excution for above problem
def sum(a, *b): # here *b argument alows multiple values
c=a
print(c) # if we dont need all steps excution we can commit this line
for i in b:
c=c+i
print(c) # if we dont need all steps excution we can commit this line
print(c)
sum(6,8,12,45,23,33)
##### personal example to solve above similer problem with 3 argument values
def problem(a, *b):
c=a
for i in b:
c=c+i
print(c)
problem(4,12,15,2,8,9)
##### Key word veriable lenght arguments #######
def person_details(name, *details):
print(name)
print(details)
person_details('Raja',16,'Hyderabad', 9542542542)
##
def person_details(name, **details):
print(name)
print(details)
person_details('Raja',age=16,city='Hyderabad', mobile=9542542542) ## once we provide the keyword here and run the program will get "person_details() got an unexpected keyword argument 'age' error then need to give "**" for function level arguments"
## Results:
Raja
{'age': 16, 'city': 'Hyderabad', 'mobile': 9542542542}
####
def person_details(name, **details):
print(name)
for i,j in details.items():
print(i,j)
person_details('Raja',age=16,city='Hyderabad', mobile=9542542542)
##RESULTS
Raja
age 16
city Hyderabad
mobile 9542542542
######## GLOBEL VS LOCAL VERIABLES ######
# Inside the function is the LOCAL veriable
# Out side the function is called GLOBEL veriable
#using globel veriable in side the local
x = 10
def local():
print('inside', x)
local()
print('outside',x)
#results:
>>> local()
inside 10
>>> print('outside',x)
outside 10
#using globel veriable & in side/local veriable seperately
x = 10
def local():
x=500
print('inside', x)
local()
print('out side', x)
#Results like below
>>> local()
inside 500
>>> print('out side', x)
out side 10
>>>
# If we want to change the Globel veriable value as same local veriable
x = 10
def local():
global x # need to give this if we want to change the Globel veriable value as same local veriable
x=500
print('inside', x)
local()
print('out side', x)
##Results shows like below:
>>> local()
inside 500
>>> print('out side', x)
out side 500