forked from ahmedshaaban1/Python_course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_function.py
256 lines (213 loc) · 6.38 KB
/
7_function.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author : Ahmed Shaaban
Date : Sep 13, 2021
Purpose: demonstrating functions
"""
print("converting celsius to fahrenheit and Kelvin")
tem_cel = 30
tem_fahr = (tem_cel*9./5.)+32
tem_kel = tem_cel + 273.15
print(str(tem_cel)+" degree cel is "+str(tem_fahr)+" degree fahrenheit")
print(str(tem_cel)+" degree cel is "+str(tem_kel)+" degree Kelvin")
print("converting celsius to fahrenheit and Kelvin")
tem_cel = 10
tem_fahr = (tem_cel*9./5.)+32
tem_kel = tem_cel + 273.15
print(str(tem_cel)+" degree cel is "+str(tem_fahr)+" degree fahrenheit")
print(str(tem_cel)+" degree cel is "+str(tem_kel)+" Kelvin")
print("converting celsius to fahrenheit and Kelvin")
tem_cel = 25
tem_fahr = (tem_cel*9./5.)+32
tem_kel = tem_cel + 273.15
print(str(tem_cel)+" degree cel is "+str(tem_fahr)+" degree fahrenheit")
print(str(tem_cel)+" degree cel is "+str(tem_kel)+" Kelvin")
#%% function to say hello
def Hello_all():
print("Welcom to Functions! ")
#%% calling the function
Hello_all()
#%% temperature conversion function
def temp_conversion(tem_cel):
print("converting celsius to fahrenheit and Kelvin")
tem_fahr = (tem_cel*9./5.)+32
tem_kel = tem_cel + 273.15
print(str(tem_cel)+" degree cel is "+str(tem_fahr)+" degree fahrenheit")
print(str(tem_cel)+" degree cel is "+str(tem_kel)+" Kelvin")
#%%
temp_conversion(30)
#%% function with defintion
help(abs)
#%% docstring
def temp_conversion(tem_cel):
'''
temp_conversion function is used to convert
temperature from degree celsius to degree Fahreheit
Parameters
----------
tem_cel : TYPE
temperature in celsius.
Returns
-------
temperature in Kelvin and Fahreheit.
'''
print("converting celsius to fahrenheit and Kelvin")
tem_fahr = (tem_cel*9./5.)+32
tem_kel = tem_cel + 273.15
print(str(tem_cel)+" degree cel is "+str(tem_fahr)+" degree fahrenheit")
print(str(tem_cel)+" degree cel is "+str(tem_kel)+" Kelvin")
#%%
help(temp_conversion)
#%%
print(temp_conversion.__doc__)
#%% coverting a list of temperatures ...
for x in [10,20,30]:
temp_conversion(x)
#%% return statement
def temp_conversion(tem_cel):
'''
temp_conversion function is used to convert
temperature from degree celsius to degree Fahreheit
Parameters
----------
tem_cel : TYPE
temperature in celsius.
Returns
-------
temperature in Kelvin and Fahreheit.
'''
print("converting celsius to fahrenheit and Kelvin")
tem_fahr = (tem_cel*9./5.)+32
tem_kel = tem_cel + 273.15
print(str(tem_cel)+" degree cel is "+str(tem_fahr)+" degree fahrenheit")
print(str(tem_cel)+" degree cel is "+str(tem_kel)+" Kelvin")
return tem_fahr,tem_kel
#%%
x=temp_conversion(30)
print(x)
print(type(x))
#%%
x[0]=100
print(id(x))
#%%
temp_conversion(20)
temp_conversion(tem_cel=20)
#%%
temp_conversion()
#%% default input value
def temp_conversion(tem_cel=30):
'''
temp_conversion function is used to convert
temperature from degree celsius to degree Fahreheit
Parameters
----------
tem_cel : TYPE
temperature in celsius.
Returns
-------
temperature in Kelvin and Fahreheit.
'''
print("converting celsius to fahrenheit and Kelvin")
tem_fahr = (tem_cel*9./5.)+32
tem_kel = tem_cel + 273.15
print(str(tem_cel)+" degree cel is "+str(tem_fahr)+" degree fahrenheit")
print(str(tem_cel)+" degree cel is "+str(tem_kel)+" Kelvin")
return tem_fahr,tem_kel
#%%
temp_conversion()
print(temp_conversion.__defaults__)
temp_conversion(20)
#%% raise error if the tem_cel > 100
def temp_conversion(tem_cel=30):
'''
temp_conversion function is used to convert
temperature from degree celsius to degree Fahreheit
Parameters
----------
tem_cel : TYPE
temperature in celsius.
Returns
-------
temperature in Kelvin and Fahreheit.
'''
import sys
if (tem_cel > 100):
print("existing as tem > 100")
sys.exit()
print("converting celsius to fahrenheit and Kelvin")
tem_fahr = (tem_cel*9./5.)+32
tem_kel = tem_cel + 273.15
print(str(tem_cel)+" degree cel is "+str(tem_fahr)+" degree fahrenheit")
print(str(tem_cel)+" degree cel is "+str(tem_kel)+" Kelvin")
return tem_fahr,tem_kel
#%%
temp_conversion(100)
#%%
def test():
print("test")
return -1
#%% local and global variables:
#%% local variable used after assignment
a = None # ensure that a is not defined... this is better than del a
def f():
a="ali"
print("inside function "+a+" "+str(id(a)))
f()
print(f.__code__.co_varnames)
#%% variable inside function could be defined outside function
a = None # ensure that a is not defined... this is better than del a
def f():
print("inside function "+a+" "+str(id(a)))
a="ahmed"
print("outside function "+a+" "+str(id(a)))
f()
print(f.__code__.co_varnames)
#%%
a = "ahmed" # ensure that a is not defined... this is better than del a
def f():
a="ali"
print("inside function "+a+" "+str(id(a)))
print("outside function "+a+" "+str(id(a)))
f()
print("outside function "+a+" "+str(id(a)))
print(f.__code__.co_varnames)
#%% the trickest one
a = None # ensure that a is not defined... this is better than del a
def f():
print("inside function "+a+" "+str(id(a)))
# if variable is assigned to specific value, then it must be used only after assignment even it has been defined outside the scope of the function
a="ali" # comment this line to see the difference
a="ahmed"
f()
#%%
a = None # ensure that a is not defined... this is better than del a
def f():
global a
print("inside function "+a+" "+str(id(a)))
a="ali" # comment this line to see the difference
a="ahmed"
f()
# see this post on stackoverflow https://stackoverflow.com/questions/8943933/variables-declared-outside-function
#%% unknown number of values
def mysum(first, *values):
mysum=first+sum(values)
return mysum
print(mysum(1,2,3))
print(mysum(2,4,5,6,78))
#%% unkonwn number of arguments
def f(temp,**kwargs):
pres =kwargs.get('pres')
rh =kwargs.get('rh')
print("temperature is "+str(temp))
if pres is not None:
print("pressue is "+str(pres))
if rh is not None:
print("rh is "+str(rh))
f(30)
print("-----------------")
f(30,pres=1004)
print("-----------------")
f(30,rh=100)
print("-----------------")
f(30,pres=1002,rh=100)