-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython-Basics.py
More file actions
340 lines (246 loc) · 7.39 KB
/
Copy pathPython-Basics.py
File metadata and controls
340 lines (246 loc) · 7.39 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
'''
Basics of python Executable code
'''
from functools import reduce
boolean = True
Integer = 2
Float = 2.0
String = 'This is String'
print(boolean, Integer, Float, String )
# Typecasting
print('Float To Integer :' ,int(2.1))
print('Float to Integer: ',float(2))
# type() is used to print the class/data-type of variable
print(type(5))
# int('A') will not be typecasted
# Expression and variables
answer = 43 +31 //2 -1
print("\n",answer)
'''
Here,
answer: variable
=, +, //, - : Operators
43, 31, 2, 1 : Operands
'''
# Strings
alpha = 'abcd'
#alpha[0] ='b' # will not work
alpha = alpha + 'e' # will work
print(alpha)
alpha.replace('d','k')
# Escape Sequence is denoted by \.
print("\nFind 'a': ",alpha.find('a'))
print("\nFind 'ab': ",alpha.find('ab'))
print("\nUppercase: ",alpha.upper())
print("\nReplace: ",alpha)
# Lists
list_1 = [1,3,2,'ab',4]
list_1.extend([2,3,1,4,5])
print(list_1)
del(list_1[0])
print(list_1,"\n")
# Tuples
tuple_1 = (1,2,3)
# Dictionary
dict_1 = {'a':1,'b':2}
for value,key in dict_1.items():
print('Key: ',key,'-> value: ',value)
print('\n')
# Loops
# For Loop
for i in range(1,10):
print(i,end='- ')
print("\n")
# While Loop
i = 0
while(i<=10):
print(i,end='- ')
i += 1
print('\n')
# Conditional Statements
a = 7
if (a > 5):
print(True)
else:
print(False)
# Functions
def print_bye(name):
print(name,' bye - bye !')
print_bye('Krishna')
# Scope
def add_1(y):
x=x+1
print(x)
x=2
# z = add_1(x) //returns error
# Objects and Classes
class Employee:
increment = 1.5 # Class variable
number_of_employee = 0
def __init__(self, first, last, sal): # Constructor for class
self.fname = first # instance variable
self.lname = last
self.salary = sal
def increase(self):
self.salary = self.salary * self.increment
@classmethod # a function that uses only class variable
def change_increment(cls, amount):
cls.increment = amount
print('\nOOPS IN PYTHON\n')
Emp_1 = Employee('Narayan','Krishna',40000)
print('Name: ',Emp_1.fname+' '+Emp_1.lname,'\nSalary of Emp_1: ',Emp_1.salary,'')
Employee.change_increment(2)
Emp_1.increase()
print('New salary after increment: ',Emp_1.salary)
# Class Method as an alternative Constructor
class Employee_2:
increment = 1.5 # Class variable
number_of_employee = 0
def __init__(self, first, last, sal): # Constructor for class
self.fname = first # instance variable
self.lname = last
self.salary = sal
def increase(self):
self.salary = self.salary * self.increment
@classmethod
def from_string(cls, emp_string):
fname, lname, salary = emp_string.split('-')
return cls(fname, lname, salary)
Emp_2 = Employee_2.from_string('Romit-Kumar-38000')
print('\nExample of class method as an alternative constructor:-\n','Salary of Emp_2: ',Emp_2.salary)
# Static methods in class (when there is no use of class methods or instance methods)
class Box:
length = '5m'
def __init__ (self,shape):
self.shape = shape
@staticmethod
def is_shipped_on(day):
if day == 'sun':
return False
else:
return True
box_1 = Box('square')
print("\nExample of static method in class:-\n",'Is Shipped on Sunday:',box_1.is_shipped_on('sun'))
# Inheritance in Python Class
class Programmer(Employee):
def __init__(self,fname,lname,sal,prolang,exp):
super().__init__(fname,lname,sal)
self.prolang = prolang
self.exp = exp
def increase(self): # Method overwriting
self.salary = int(self.salary * (self.increment+0.2))
Emp_3 = Programmer('Fullmetal','Alchemist',45000,'Python','0yrs')
print('\nExample Of Inheritance:-\n','Emp_3 salary: ',Emp_3.salary)
Emp_3.increase()
print('\nNew salary: ',Emp_3.salary)
# Magic / Dunder Method in Python -> Represented by '__methodName__' and is used for method overriding.
class Employee_3:
def __init__(self, first, last, sal): # Constructor for class
self.fname = first # instance variable
self.lname = last
self.salary = sal
# Magic method
def __add__(self,other): # Changing the functionality of addition
return self.salary + other.salary
def __repr__(self): # what to show when we print object
return 'Employee_3({}, {}, {})'.format(self.fname, self.lname, self.salary)
# def __str__(self): #overwrites the repr method
# return 'The name is {} {}'.format(self.fname,self.lname)
Emp_4 = Employee_3('Dante','Kumar',40000)
Emp_5 = Employee_3('Gordon','Freeman',1000000)
print('\nExample of Dunder Methods:-\n','Emp_4 + Emp_5: ',Emp_4 + Emp_5,"--- repr(Emp_4): ",repr(Emp_4))
# *args and **kwargs
def function_1(*args):
return 'The names are {}, {} and {}'.format(args[0],args[1],args[2])
print('\nExample of args: ',function_1('Prince',"Babbage","Charly"))
def function_2(**kwargs):
for key, value in kwargs.items():
print(key,' -> ',value)
marksheet = {'Rohan': 92, 'Mohan': 85}
print('\nExample of kwargs: ',"Marks of: ")
function_2(**marksheet)
# Exception Handling
# Exception - only interrupts certain part of program. Error - interrupt whole program.
try:
open('a.txt')
except Exception as e:
print("\nError : ",e)
# Handling Multiple exceptions
try:
file = open('a.txt','r')
except EOFError as e:
print(e)
except IOError as e:
print('This is I/O Error')
finally:
print('This will always run')
# Generators and iterators
def gen(n):
for i in range(n):
yield i
obj_1 = gen(10)
print('\nGenerators Example :- \n',obj_1)
print(next(obj_1))
print(next(obj_1))
print('\nFor Loop:-')
for i in range(10):
print(i,end=',')
print('\nComprehsions Examples :- ')
# List, Set, Dictionary and Generator Comprehension
list_1 = [1,2,3,4,5,6,7,8,9]
print('List Example:-\n',[ item for item in list_1 if item % 2 == 0])
# Dictionary
keys = ['a','b', 'c']
values = ['1','2','3']
print('Dictionary Example:-\n',{i:j for (i,j) in zip(keys,values)})
# Set
squared = { x**2 for x in list_1 }
print('Set Example:-\n',squared)
# Generator
print('Generator Example:-\n')
gen = (i for i in range(56) if i% 3 == 0)
for item in gen:
print(item, end =' ')
# Map
h1 = [1,2,3,4,5]
def square(n):
return n*n
sq = list(map(square, h1))
print('\nExample Of Mapping: ',sq)
# Filter
def greater_than_2(num):
if num > 2:
return True
else:
return False
greater_than_2v = filter(greater_than_2, h1)
print('\nExample Of Filter: ',list(greater_than_2v))
# Reduce
def mul_num (a,b):
return a*b
mul_list = reduce(mul_num, h1)
# Join
list_of = [ 'chalk', 'pen', 'paper']
print('\nExample of Join: ',' and '.join(list_of))
# Bisect Module
import bisect
list_num = [1,2,3,4,8]
print(bisect.bisect(list_num,5)) # tells where to insert number
print(bisect.insort(list_num,5)) # insert number
print('\nExample of Bisect: ',list_num)
# Lambda Function
add = lambda x,y: x+y
print('\nExample of Lamda Function: ',add(5,10))
# Enumerate in Python
print('\nEnumerate in Python :-')
rand_alpha = ['ab','cd','ed']
for i,item in enumerate(rand_alpha):
print(i,' -> ',item)
# Formatting in Python
print('\nFormatting of string :-')
name = 'Algorithm To Live By'
categ = 'non-fiction'
template = 'This is {} book and category of this book is {}'.format(name,categ)
print(template)
template = 'This is {1} book and category of this book is {0}'.format(name,categ)
print(template)