-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Basic
More file actions
396 lines (334 loc) · 7.14 KB
/
Python Basic
File metadata and controls
396 lines (334 loc) · 7.14 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
##short hand if-else
a=1
b=3
print("hello") if a>b else print("Bye")
##DocStrings
def fun():
""" This function does nothing and
this is docstring that gives info about fun work"""
print(fun.__doc__)
##Dealing with file
read mode
f=open("first.txt")
content=f.read()
print(content)
write mode -- overrides the previous content
f=open("first.txt","w")
f.write("Srushti")
append mode --appends to the previous content
f=open("first.txt","a")
f.write("Srushti Hello")
read+write mode
f=open("first.txt","r+")
content=f.read()#reads complete text in file
f.write("Good Morning and Welcome to fresh morning")
# content=f.read(3)#reads upto 3 characters in file
print(content)
content=f.read(3)
print(content)
f.close()
readline
f=open("first.txt")
# print(f.readline())
# print(f.readlines())
c=f.read()
for line in c:
print(line,end='')
tell()-- tells where our pointer is
seek()--takes the pointer back to
f=open("first.txt")
print(f.tell())
print(f.readline())
print(f.tell())
f.seek(10)
print(f.tell())
print(f.readline())
print(f.tell())
f.close()
##with block
with open("first.txt") as f:
a=f.read()
print(a)
f=open("first.txt")
print(f.readline())
##global keyword
l=10
def fun():
l=9
global m
m=7
print(l)
print(m)
fun()
print(l)
print(m)
x=8
def fun():
x=30
def gun():
global x
print("Global ",x)#8
x=9
print("Global changed",x)#9
gun()
print("fun()",x)#30
fun()
print("Changed global",x)# 9
##randint
import random
print(random.randint(0,19))
print(random.random())
l=[1,2,3,4,"hello"]
print(random.choice(l))
##string format
s="helloj"
a=2
a2="this is formatting %s %s"%(s,a)
a3=f"this is also {s} formatting{a}"
a4="this is{} {}"
a4=a2.format(s,a)
print(a4)
print(a2,a3)
##variable arguments
def fun(n,*args):
print(n)
print(type(args))
return args
l=["hello","kello"]
n="yess"
# print(n,fun(*l)) #mystery
# print(fun(*l),n)#error
print(fun(n,*l))
def fun(*args,**args2):
for item in args:
print(item)
for i,j in args2.items():
print(i,j)
l=["hello","world"]
l2={"kilo":"weight","banana":"fruit"}
fun(*l,**l2)
##time module to check time taken by program
import time
i=time.time()
print(i)
j=0
k=0
while j<5:
j=j+1
time.sleep(1)
print("Hello")
print(time.time()-i)
i=time.time()
while k<5:
k=k+1
print("Hello")
print(time.time()-i)
localtime=time.asctime(time.localtime(time.time()))
print(localtime)
##import
import sys
print(sys.path)
from pattern import a
print(a)
import pattern
print(pattern.a)
""" run the pattern program and the output would be And the main is pattern"""
if __name__=='__main__':
print("h")
##map
l=["1","3","4"]
print(l)
l2=list(map(int,l))# map(what to perform,on what)
print(l2)
def square(a):
return a*a
def cube(a):return a*a*a
fun=[square,cube]
for i in range(0,5):
l2=list(map(lambda x:x(i),fun))
print(l2)
##filter
l=[1,2,3,4]
# l2=list(map(lambda x:x<3,l))
l2=list(filter(lambda x:x<3,l))
print(l2)
##reduce
from functools import reduce
l=[1,2,3,65]
l2=reduce(lambda x,y:y+x,l)
print(l2)
#dealing with functions deletion
def fun():
""" fun still works even if we delete it b'coz it is always saved"""
print("It still works")
print(fun.__doc__)
gun=fun
del fun
gun()
##function within function
def f(a):
if(f==0):
return print
else:
# return int
return sum
print(f(0))
print(f(1))
def fun(gun):
gun("This gun is printed")
fun(print)
##decorator
def f(gun):
def exe():
print("Executing")
gun()
print("executed")
return exe
@f
def who():
print("it's a decorator")
# who=f(who) # another way to use it as a decorator is @function name
who()
##OOPS STARTED
class Emp:
n=3
""" this is the doc of Emp class"""
em=Emp()
em2=Emp()
print(em2.n)#n is class variable
em.n=8
print(em2.n)#3
print(em.n)#8
Emp.n=9#change the class variables with class name only
print(em2.n)#9
print(em.n)#still 8
em.name="fan"
em2.name="fan2"
print(em.__dict__)
print(Emp.__dict__)
##class methods ---- used to change class variables for class and all its instances
class s:
n=9
@classmethod
def fun(cls,n):
cls.n=n
f=s()
g=s()
f.fun(89)
print(s.n)
##class methods as alternative constructor
class s:
def __init__(self,name,id):
self.name=name
print("Constructor called")
self.id=id
@classmethod
def convert(cls,string):#alternative constructor
p=string.split("-")
return cls(p[0],p[1])
return cls(*string.split("-"))
@staticmethod
def junk(a):
print(f"{a} is a junk food")
return "No"
print(s.junk("Sandwich"))
# s1=s("sam",88)
# s2=s("tom",34)
# s3=s.convert("tomcat-23")
# print(s1.name)
# print(s3.name)
#Inheritance
##Single inheritance
class Emp:
salary=230876
name="Meena"
time=45
def __init__(self,name,salary,t):
print("Employee constructor")
self.name=name
self.salary=salary
self.time=t
def work(self):
print(f"{self.name} worked for {self.time} hrs")
def salary2(self):
print(f"Salary of {self.name} is {self.salary} ")
e=Emp("Srushti",10000000,354)
e.work()
e.salary2()
class Programmer(Emp):
lang="Python"
def __init__(self,name,salary,t,lang):
# Emp.__init__(self,name,salary,t)
super().__init__(name,salary,t)
self.lang=lang
print("Programmer constructor")
def printAll(self):
print(f"Name : {self.name}\nSalary : {self.salary} \nWork Time : {self.time} \nLanguage : {self.lang}")
p=Programmer("Larry",347974,47,"Java")
p.printAll()
##multiple inheritance
class Emp:
def p(self):
print("f() of Emp")
class Pro:
def p(self):
print("p() of Pro")
class Per(Emp,Pro):
pass
p=Per()
p.p()
multilevel
class Emp:
def p(self):
print("f() of Emp")
class Pro(Emp):
def p(self):
print("p() of Pro")
class Per(Pro):
pass
p=Per()
p.p()
private=__variablename,public=variablename,protected=_variablename
class E:
n=2
_n=9
__n=8
e=E()
print(e.n)
print(e._n)
print(e._E__n)#private variable is called like this
########Very imp
class A:
var1="I am a class variable in class A"
def __init__(self):
self.var1="I am an instance variable in class A"
class B(A):
var1="I am a class variable in class B"
def __init__(self):
self.var1="I am an instance variable in class B"
a=A()
b=B()
print(b.var1)
#check for instance variable in class B--->
#if not then goes for instance variable of A--->
#if not then goes for class variable of class B--->
#if not then goes for class variable of A--->
#if not then gives error
##operator overloading using dunder methods
class Emp:
def __init__(self,salary):
self.salary=salary
def __add__(self,other):
return self.salary+other.salary
def __truediv__(self,other):
return self.salary/other.salary
def __repr__(self):
return f"{self.salary} from repr since str is not present"
#without repr the output was <__main__.Emp object at 0x0000024156FC92B0>
def __str__(self):
return f"{self.salary} from str"
e=Emp(12)
e2=Emp(34)
print(e+e2)
print(e/e2)
print(e)