-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson4.py
More file actions
359 lines (322 loc) · 7.16 KB
/
lesson4.py
File metadata and controls
359 lines (322 loc) · 7.16 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
""" PYTHON KEY WORDS"""
"""
1. True - Represents the boolean true; if the given condition is true, then it returns "True".
Non-zero values are treated as true
2. False - Represents the boolean false; if the given condition is false, then it returns "False".
Zero value is treated as False
3. None - Denotes the null value or void. An empty list or Zero can't be treated as NONE.
4. and - is a ogical operator in Python.
Used to check multiple conditions.
Returns true if both conditions are true
5. or - is a logical operator in Python.
Returns true if one condition is true
6. not - is a logical operator in Python.
Inverts the truth table
"""
""" 7. assert """
"""
assert - Used as a debugging tool in Python.
It checks the correctness of the code.
It raises an AssertionError if found any error in the code and also prints the message with an error
"""
print(" -- assert -- ")
a = 10
b = 0
print("a is dividing by zero")
# assert b!=0
# print(a/b) #throws an error
""" 8. def """
"""
def - This key word is used to declare the function in Python
"""
print("")
print(" --- def ---")
def my_func(a,b):
c = a+b
print(c)
my_func(30,20)
""" 9. class """
"""
class - Used to represent the class in Python
The class is the blueprint of the objects
It is the collection of the variable and methods.
"""
print("")
print(" --- class --- ")
class Myclass:
def add(a,b):
a = 40
b = 60
c= a+b
print(c)
add(a,b)
""" 10. continue """
"""
continue - Used to stop the execution of the current iteration.
"""
print("")
print(" --- continue --- ")
a = 0
while a < 5:
a += 1
if a == 3:
continue
print(a)
""" 11. break """
"""
break - Used to terminate the loop execution and control transfer to the end of the loop
"""
print("")
print(" -- break -- ")
for i in range(5):
if(i==3):
break
print(i)
print("End of execution")
""" 12. If """
"""
if - Used to represent the conditional statement.
The execution of a particular block is decided by idf statement
"""
print("")
print(" --- if --- ")
i = 20
if(1<15):
print("I am less than 20")
""" 13. else """
"""
else - Else statement is used with the if statement.
When if statement returns false, the else block is executed
"""
print("")
print(" -- else --")
n = 14
if(n%2==0):
print("Even")
else:
print("Odd")
""" 14. elif """
"""
elif - Used to check the multiple conditions.
It's short for else-if
If previous condition is false, then check until the true condition is found.
"""
print("")
print(" --- elif --- ")
marks = 30
if(marks>=90):
print("Excellent")
elif(marks<90 and marks>=75):
print("very Good")
elif(marks<75 and marks>=60):
print("Good")
else:
print("Average")
""" 15. del """
"""
del - used to delete the reference of the object
"""
print("")
print(" --- del --- ")
x=2
y=5
print(x,y)
del x
print(y)
# x nolonger exists
#print(x) # throws an error that x is not defined
""" 16. try, except """
"""
try, except - try-except is used to handle exceptions.
The exceptions are run-time errors
"""
print("")
print(" --- try,except --- ")
m=0
try:
b=2/m
except Exception as e:
print(e)
""" 17. raise """
"""
raise - is used to throw the exception forcefully
"""
print("")
print(" --- raise --- ")
a=5
# if(a>2):
# raise Exception('a should not exceed 2')
""" 18. finally """
"""
finally - used to create a block of code thar will always be executed no matter the else block raises an error or not.
"""
print("")
print(" --- finally --- ")
a=0
b=5
try:
c=b/a
print(c)
except Exception as e:
print(e)
finally:
print("finally was executed")
""" 19. for """
"""
for- used for iterations.
used to iterate over sequences (list, tuple, dictionary, string)
"""
print("")
print(" --- for ---")
positions = [1,2,3,4,5]
for i in positions:
print(i)
""" 20. while """
"""
while - Used for iterations
while loop is executed until the condition returns false
"""
print("")
print(" --- while --- ")
a=0
while(a<5):
print(a)
a =a+1
""" 21. import """
"""
import - used to import modules in the current Python script
"""
print("")
print(" --- import --- ")
import math
print(math.sqrt(25))
""" 22. from """
"""
from - used to import the specific function or attributes in the current Python script
"""
print("")
print(" --- from --- ")
from math import sqrt
print(sqrt(36))
""" 23. as """
"""
as - used to create name alias.
It provides the user-define name while importing a module
"""
import calendar as cal
print(cal.month_name[12])
""" 24. pass """
"""
pass - is used to execute nothing or create a placeholder for future code
If we declare an empty class or function, it will throw an error,
so we use the pass key word to declare an empty class or function
"""
print("")
print(" --- pass --- ")
class my_class:
pass
def my_function():
pass
""" 25. return """
"""
return - used to return the result value or none to called function
"""
print("")
print(" --- return -- ")
def sum(a,b):
c = a + b
return c
print("The sum is:",sum(25,15))
""" 26. is """
"""
is - used to check if the two variables refer to the same object.
returns true if they refer to the same object otherwise false
"""
print("")
print(" --- is --- ")
x=5
y=5
a=[]
b=[]
print(x is y)
print(a is b)
""" 27. global """
"""
global - used to create a global variable inside the function. Any function can access the global variable
"""
print("")
print(" --- global --- ")
def summation():
global a
a = 20
b = 30
c = a+b
print(c)
summation()
def display():
print(a)
display()
""" 28. nonlocal """
"""
nonlocal - It's similar to the global and used to work with a variable inside
the nested function ( function inside a function)
"""
print("")
print(" --- nonlocal --- ")
def outside_function():
a = 20
def inside_function():
nonlocal a
a = 30
print("Inner function a : ",a)
inside_function()
print("Outer function a : ",a)
outside_function()
""" 29. lambda """
"""
lambda - used to create the anonymous function in Python.
It is an inline function without a name
"""
print("")
print(" --- lambda ---")
a = lambda x:x**3
for i in range(1,6):
print(a(i))
""" 30. yield """
"""
yield - used with the Python generator.
It stops the function's execution and returns the value to the caller
"""
print("")
print(" --- yield --- ")
def fun_generator():
yield 1
yield 2
yield 3
# Driver code to check above generator function
for value in fun_generator():
print(value)
""" 31. with """
"""
with - used in the exception handling.
Makes code cleaner and more readable
Advantage of using it is that we don't need to call close().
"""
print("")
print(" --- with --- ")
with open('file_path', 'w') as file:
file.write('Hello World!')
""" 32. None """
"""
none - used to define the null value.
It's remembered that None does not indicate 0, false, or any empty data-types.
It is an object of its data-type
"""
print("")
print(" --- None --- ")
def return_none():
a = 10
b = 20
c = a + b
x = return_none()
print(x)