-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass2.py
1410 lines (473 loc) · 8.42 KB
/
Class2.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 5 19:32:19 2023
@author: vikas
"""
'''
In c/ c++
sequence of values
1,3,2,4,5,8,7
int a[10]; create a array
then able to add elelments in that array
for
'''
'''
list
'''
l1 = []
l2 = [2,3,4,1,5,7]
l2.sort()
#Sequences in Python
'''
List
Tupple
Set
Dictionary
# Hetrogeneous or homogeneous , Mutable, Ordered, Indexed
'''
# List
l1 = []
type(l1)
l2 = [1,4,3,7,6,8]
a = 10
print(a)
print(l2)
# Indexing
l2[0]
l2[1]
l2[2]
l2[3]
l2[4]
l2[5]
l2[6]
#Hetrogeneous
l3 = [1, 3.4, True, 'Python']
print(l3)
emp = [901, 'VK', 'CSE', 'M', True]
emp
type(emp)
type(emp[0])
type(emp[1])
type(emp[2])
type(emp[4])
# individual access is possible through indexes
# List have 1000 entries and We want to access all entries individually
r4 = range(100)
r4
l4 = list(r4) #converting a range to list
l4
l4 = list(range(100))
l4
l5 = list(range(10, 21))
l5
l6 = list(range(10, 51, 5))
l6
l7 = list(range(10, 51, 5))
print("value i",l7[0])
print("value i",l7[1])
print("value i",l7[2])
# looping structre
'''
loop
{
startement 1
state 2
state 3
}
'''
l7
for e in l7:
print("value i",e)
for e in emp:
print(e)
#Mutable allow to change any value
l2
l2[1] = 44
l2
l2.append(10)
l2
l2.remove(6)
l2
l2.pop()
l2
l2.pop()
l2.remove(44)
l2
l2.pop(1)
l2 = [2,5,7,5,4,3,8,9,7,5]
l2.count(5)
l2.count(3)
l2.sort()
l2
l2.reverse()
l2
l2.insert(4, 6)
l2
# Set
'''
unique data
ordered data
'''
s1 = {0}
s2 = {3,2,4,1,2,6,4,3}
s2 #Unique and Ordered
# Non-Indexed
s2 = {3,2,4,1,2,6,4,3}
s2
s2[0] #TypeError: 'set' object is not subscriptable Error
for s in s2:
print(s)
# Mutable
s2.add(5)
s2
s2.pop()
s2.remove(5)
s2
s2.update({3,2,6,5,9,8,1})
s2
s2.remove(5)
s2
s2.remove(5) # KeyError: 5
s2
s2.discard(4)
s2
s2.discard(4)
s2
teamA = {'India', 'Australia','Pakistan', 'England'}
teamB = {'Bangladesh', 'New Zealand', 'West Indies', 'India'}
teamA
teamB
teamA.union(teamB)
teamA.intersection(teamB)
E = {1,2,3,4,5,11,12,13,14,15}
B = set(range(11,16))
E
B
A = E.difference(B)
A
A= set(range(1,11))
B =set(range(6,15))
A
B
E = A.union(B)
E
A
B
I = A.difference(B)
I
import numpy as np
TotalEmp = list(range(1,21))
TotalEmp
BenchEmp = np.random.choice(TotalEmp, size=10)
TotalEmp = set(TotalEmp)
TotalEmp
BenchEmp
WorkEmp = TotalEmp.difference(BenchEmp)
WorkEmp
# Dictionary
d1 = {}
# Key : Value Paired
d2 = {'empcode':1, 'empname': 'VK', 'empstatus': True}
d2
#Not Indexed
d2['empcode']
d2['empname']
d2['empstatus']
d2['empsal'] #KeyError: 'empsal'
d2.keys()
d2.values()
d2.items()
#Not Ordered
#Hetrogeneous
d2 = {'empcode':1, 2: 'VK', 'empstatus': True}
d2
car = { 'brand':'Honda', 'model': 'Jazz', 'year' : 2017}
d2.keys()
list(d2.keys())
set(d2.keys())
d2.values()
d2.items()
a = (1,2) # Tuple
b =(2,3)
c= (5,5)
l1 = [a,b,c]
l1
s1 = {a,b,c}
s1
a = [1,2] # Tuple
b =[2,3]
c= [5,5]
l1 = [a,b,c]
l1
car = { 'brand':'Honda', 'model': 'Jazz', 'year' : 2017}
car
car['brand']
#Mutable
car['year'] = 2020
car
car['sold'] = 2021
car
car.pop('year')
car
car.popitem()
car
car.values()
import numpy as np
rno = list(range(1,11))
name = ['Student'+str(i) for i in range(1,11)]
course = list(np.random.choice(['BBA', 'MBA', 'BTech', 'MTech'], size=10))
rno
name
course
student = {'RollNo': rno, 'Name': name, 'Course': course}
student
student.keys()
student['Name']
student['Name']
type(student['Name'])
student['Name'][0]
student['Name'][9]
student
import pandas as pd
df = pd.DataFrame(student)
df.to_csv('Student.csv')
# Tuple
t1 = ()
t2 = (2,3,5,1,7,6,7,8,3)
t2
#Not Ordered, Not Unique Values
#Indexed
t2[0]
t2[7]
#Not Mutable
t2[0] = 20 #TypeError: 'tuple' object does not support item assignment
# Conditional Statements
#if statement
a = 10
b = 20
a<b
if a<b:
print("a is lesser")
a = 20
b = 10
a<b
if a<b:
print("a is lesser")
# if else statement
a = 30
b = 20
if a<b:
print("A is lesser")
else:
print('b is lesser')
# if elif else
'''
Marks Grading System
M > 90 - A
M > 80 and M <= 90 - B
M > 70 and M <= 80 - C
M > 60 and M <= 70 - D
M > 50 and M <= 60 - E
M <= 50 - F
'''
M = 49
if M>90:
print("A")
elif M > 80 and M <= 90:
print("B")
elif M > 70 and M <= 80:
print("C")
elif M > 60 and M <= 70:
print("D")
elif M > 50 and M <= 60:
print("E")
else:
print("Fail")
M=65
if M>90:
print("A")
print("very good grades")
if M > 80 and M <= 90:
print("B")
if M > 70 and M <= 80:
print("C")
if M > 60 and M <= 70:
print("D")
if M > 50 and M <= 60:
print("E")
if M <=50:
print("Fail")
#Looping Stuctures or Iterative Statements
l1 = list(range(0,21, 5))
l1
#for loop
for i in l1:
print(i)
for i in l1:
print("Value is",i)
print("Multple of 2 is",i*2)
print("Power of 2 is",i**2)
print('out')
# Write a table of 2 using for loop
'''
2 x 1 = 2
2 x 2 =4
-
-
-
2 x 10 = 20
'''
for i in range(1,11):
print("2 x",i,"=",2*i)
for i in range(1,11):
print(f"2 x {i} = {2*i}")
for i in range(1,11):
print("2 x {0} = {1}".format(i,2*i))
# Munlti Loops
# Print table of 2 to 4
for j in range(2,5):
for i in range(1,11):
print(f"{j} x {i} = {j*i}")
# While Loop
'''
While will work till condition is Boolean True
While is condition controlled loop
'''
#Print 1-10
cnt=1
while (cnt<=10):
print(cnt)
cnt = cnt + 1
cnt=1
while (True):
print(cnt)
cnt = cnt + 1
# Write a table of 2 using while loop
'''
2 x 1 = 2
2 x 2 =4
-
-
-
2 x 10 = 20
'''
i = 1
while (i<=10):
print("2 x",i,"=",2*i)
i=i+1
j=2
while(j<=4):
i = 1
while (i<=10):
print(f"{j} x {i} = {j*i}")
i=i+1
j=j+1
# break and continue
lst = [3,2,6,5,8,9,1,7]
chk=5
for i in lst:
print(i)
if(chk==i):
print(i,"element Found")
lst = list(range(1,10))
chk=5
for i in lst:
if(chk==i):
print(i,"element Found")
print(i)
print("Completed")
lst = list(range(1,10))
chk=5
for i in lst:
if(chk==i):
print(i,"element Found")
break
print(i)
print("Completed")
# Continue
lst = list(range(1,10))
chk=5
for i in lst:
if(chk==i):
print(i,"element Found")
continue
print(i)
print("Completed")
#password entry system that allows three attempts
pswd = 99
cnt=1
while(True):
en = int(input("Enter Password"))
if pswd == en:
print("Paswword Correct")
break
cnt = cnt + 1
print("Password Incorrect")
if cnt<=3:
print(cnt, " - Re Chance to Enter Password")
continue
print("Exited")
break
# Functions
'''
System Defined
User Defined
Reduce the line of written code
Enhance reutilization of written code
'''
def f1():
print("Hello")
f1()
f1()
f1()
def compute():
x = 10
y = 20
print(x+y)
print(x-y)
print(x*y)
print(x//y)
compute()
compute()
#Function take inputs in the form of Parameters
def compute1(x,y):
print(x+y)
print(x-y)
print(x*y)
print(x//y)
compute1(10,20)
compute1(30,20)
compute1(40,50)
def empdet(eno, ename, dept, email):
print(eno, ename, dept, email)
empdet(111, 'Vikas', 'CSE', '[email protected]')
empdet(111, 'Vikas', 'CSE')
help(print)
#Default Values as paramerters or arguments
def empdet(eno, ename, dept='NA', email='NotAvailable'):
print(eno, ename, dept, email)
empdet(111, 'Vikas', 'CSE', '[email protected]')
empdet(111, 'Vikas', 'CSE')
#Return or Ouput from a function
def maximum(lst):
m = 0
for i in lst:
if i>m:
m=i
print(m, "is greatest")
l1 = [4,3,6,9,2,1]
print(maximum(l1))
x = maximum(l1)
print(x)
print(maximum(l1))
def maximum(lst):
m = 0
for i in lst:
if i>m:
m=i
print(m, "is greatest")
return(m)
l1 = [4,3,6,9,2,1]
print(maximum(l1))
x = maximum(l1)
print(x)