-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python3 notes
1214 lines (1037 loc) · 48.6 KB
/
Python3 notes
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
udemy course link -https://www.udemy.com/course/complete-python-developer-zero-to-mastery/
#COMPLETE PYTHON DEVELOPER 2020
#AUTHOR:ANDRIE NEAGOIE
#Notes prepared by: TUSHAR JAIN
………………………………………………………………………………………………………………………………………………………………….
SECTION 1 :introduction
...............................................................................................................................................................
SECTION 2: Python intoduction
lec4: compiler vs interpreter
lec5: python interpreter
download it from python.org
usually cpython is downloaded which is python interpreter written in c language
other types of compiler/interpreter are also availble on other sites like
#pypy(written in python)
lec6: can use online compiler for python:
*repl.it (signup required)
*glot.in (starts coding without any signup required)
lec8: first python program:
name=input('what is your name?')
print('helllloooo '+ name)
note: we can use " in place of '
written code----->python interpreter------> python vmachine----->machine language
lec9: python 2 vs python 3
....................................................................................................................................................................
SECTION 3:Python basics I
lec14: DATA TYPES
#fundamental data types
int
float
complex
bool
str
list
tuple
set
dict
#classes--->custom types
#specialised data types
implemented using python extensions(modules)
#none
lec17: NUMBERS(int+float)
print('')--->syntax to print
note-print() add the curser to new line after printing because default argument is end=’\n’
inside print() we can also do print(‘hello’,end=’ ‘ ) to prevent the cursor from going to
next line.
a=input('')---->syntax to input
type--->keyword to get type of data,eg-print(type(6))-----> will print <class 'int'>
#---> for adding comments
'**'----> power of operator --- print(2 ** 3) gives 8
'//'----> divide then rounded upto integer value --- print(5 // 4) gives 1
'%'-----> modulo operator --- gives remainder
lec18: MATH FUNCTIONS
print(round(3.1)) gives 3
print(round(3.9)) gives 4
print(abs(-20) gives 20
note- i)you can comment multiple line by selecting lines and then press control+slash
ii)dont need to memorise all function you just need to know where to google it and . how to implement it
lec20: OPERATOR PRECEDENCE
follow "BODMAS"
lec22: OPTIONAL:bin() and complex
i)complex is another data type that stores imaginary or complex numbers
ii)print(bin(5)) returns binary of 5- 0b101,note-0b represent that the number is in binary
iii)print(int('0b101',2)---->this tells the interpreter to print integer value of '0b101' or'101' . which is of base 2.
lec23 :VARIABLES
its our 1st term
syntax- iq=190 ,note doesnt need to declare like in c or c++
rules:-
i) snake_case(spaces not available)
Ii)start with lowercase or underscore
iii)letters,numbers,underscore
iv)case sensitive
v)dont overwrite keywords
note-underscore in starting signifies private variable(will be discuss latter on)
by convention variable in capitals represent constant and its value is not meant to be . change though it can change
'__'before variable(called dunder variables)
shorthand practice---> a,b,c=1,2,3
lec25: AUGMENTED AND ASSIGNMENT OPERATOR
value=5
value +=2 #this is called augmented assignment
print(value)
output--->7
lec26: STRING
print(type('hi'))
long_string='''
WOW
0 0 OUTPUT-
--- WOW
''' 0 0
print(long_string) ---
first='tushar ' tushar jain
last='jain'
full=first+last #string concatenation
print(full)
lec29: ESCAPE SEQUENCES
\t ---> to add tab spacing
\n ---> to add new line
"it\'s a \"kind\" of sunny day"---> note \(backslash) tells the interpreter whatever comes . after this is a string
so to print ''(inverted comma) or ""(double inverted) in string use a backslash before it.
lec30: FORMATED STRING
name='johnny'
age=55
#normal string
print('hi ' + name + '.you are ' + str(age) + ' years old)
#formated string
print(f'hi {name}.you are {age} years old')
note- ‘f’ before the string tells interpreter that this is a formated string
this syntax is applicable in python3
#formated string(old syntax for python 2 also applicable in python3)
print('hi {0}. you are {1} years old'.format(name,age))
note- we change the order of printing of variables by altering the numbers in curlybraces
lec31: STRING INDEX//called slicing of string
selfish='01234567' output
#[start]
print(selfish[2]) 2
#[start:stop]
print(selfish[2:7]) 23456
#[start:stop:stepover]
print(selfish[1:8:2]) 1357
print(selfish[::-1]) 76543210
lec32: IMMUTABILITY
Strings in python can’t change
eg:selfish='012345'
selfish[0]='8' #this is illegal operation
selfish=100 #this is allowed as whole new value is assigning with erasing the whole . . previous shell
lec33: BUILTIN FUNCTION+METHODS
str.upper() changes everything to capital letters
str.capitalize() changes first letter in capital
str.lower() change everything to lowercase
str.find('str1') finds first occurence of str1 and return index at which its found
str.replace(old_str,new_str) replaces str1 with str2 in string str
len(str) returns length of the string
lec39: LIST SLICING
#list is just like arrays
li=[1,2,3,4,5]
li2=['a','b','c']
li3=[1,2,'a',True]
#list are mutable unlike string
#list slicing
cart=['notebook,'glasses,'toys','grapes']
print(cart[0::2]) ----------> output-['notebook','toys']
new_cart=cart #new_cart and cart are same list
new_cart=cart[:] #new_cart and cart are two different carts
lec40: MATRIX
matrix=[
[1,2,3],
[4,5,6],
[7,8,9],
]
lec41: LIST METHODS
basket=[1,2,3,4,5]
#adding output
basket.append(100) #adds the element in last
print(basket) [1,2,3,4,5,100]
basket.insert(4,9) #insert 9 at index 4
print(basket) [1,2,3,4,9,5,100]
basket=[1,2,3,4,5]
basket.extend([100,101]) #extends the list
print(basket) [1,2,3,4,5,100,101]
#removing
basket=[1,2,3,4,5]
basket.pop() #removes last element
print(basket) [1,2,3,4]
basket.pop(0) #removes element at index 0
print(basket) [2,3,4]
basket.remove(4) #removes the value 4 from the list
print(basket) [2,3]
basket.clear()
print(basket) []
note- new_list=basket.pop() #is allowed
#this is not allowed with other methods
eg-new_list=basket.clear() #is not allowed it will print NONE
lec42: LIST METHOD2
basket=['a','b','c','d','e']
#searching
print(basket.index('d')) #prints index number of 'd' that is 3
print(basket.index('d',0,2)) #starts searching at index 0 and stops at 2 and
prints index of 'd' if found in that range
that is 0 to 2 otherwise returns error
print('x' in basket) #asks is 'x' is in basket?,if yes output is TRUE otherwise FALSE
print(basket.count('d') #prints how many times 'd' is present in basket
lec43: LIST METHODS3
basket=['a','b','c','e','d']
#sort keyword
print(basket.sort()) #this is illegal syntax as .sort() doesn't create new copy of list
basket.sort()
print(basket) output-['a','b','c','d','e']
#sorted keyword output
print(basket.sorted()) ['a','b','c','d','e'] #creates new copy of list
print(basket) ['a','b','c','e','d'] #basket and basket.sorted() are different list
#two methods of copying
new_basket=basket[:]
new-basket=basket.copy()
new_basket=basket #dost not copy basket in new_basket but shares address of basket to . new_basket means both are same list
#reverse
basket.reverse() #reverses the whole list
print(basket)
lec44: COMMON LIST PATTERNS
#reversing
print(basket[::-1]) #using list slicing
basket.reverse()
#range(start,stop) output
print(range(1,10)) range(1,10)
print(list(range(1,10))) [0,1,2,3,4,5,6,7,8,9]
print(list(range(10))) [0,1,2,3,4,5,6,7,8,9]
#.join()
sentence='!'
new_sentence=sentence.join('hi','i','am','JOJO')
print(new_sentence) #output-hi!i!am!JOJO
#new_sentence='!'.join('hi','i','am','JOJO') creates same output
lec45: LIST UNPACKING
a,b,c,*other,d=[1,2,3,4,5,6,7]
#a=1;
#b=2;
#c=3;
#other=[4,5,6];
#d=7
lec47: DICTIONARIES
#keyword for dictionary is "dict"
#definition-dictionary is unordered key value pair
#means- all keys are not stored in contagious manner
dictionary={
'a':1, #a,b are key that are string
'b':[2,4,6]
}
print(dictionary['b'][2]) #output-6
#we can have list inside a dictionary and vice versa
lec48: DEVELOPER FUNDAMENTALS3
a developer should know which data structure or data type to use when
and what are pros and cons of what
dictionary vs list
lec49: DICTIONARY KEYS
a key needs to be immutable means vallue of key meant no to be change
a list cant be a dictionary key as it is mutable
a key has to be unique
if interpreter encounters same key it overwrites the data
lec50: DICTIONARY METHODS
user={
'basket':[1,2,3],
'greet':'hello'
}
#.get method
print(user.get('age')) #prints NONE if age is not present in dictionary
print(user.get('age',55)) #print age if age is present otherwise first assign 55 to age . and then print
#another syntax for creating new dictionary
user2=dict(name='johnjohn')
print(user2) #output-{'name':'johnjohn'}
#this syntaax is not generally used
lec51: DICTIONARY METHODS2
print('basket' in user) #prints TRUE if basket is present in the dictionary user . . otherwise print FALSE
print('basket' in user.keys()) #checks in the keys of the dictionary user
print('basket' in user.values())
print(user.clear()) #prints NONE
user.clear print(user) #prints {}
#copying
user2=user.copy()
#removes
print(user.pop('age')) #prints value of age that is going to be removed as pop() . . . returns that
user.pop('age')
print(user) #prints removed dictionary
#popitem()
user.popitem()
print(user) #randomly deletes one item from the dictionary
because items are not contagious in dictionary
# update
print(user.update({age:55}))
print(user)
lec52: TUPLES
are immutable list
my_tupple=(1,2,3,4,5)
my_tuple[2]='w' #this produces error as tuple are immuatble
lec53: TUPLES2
my_tuple=(1,2,3,4,5)
#count
print(my_tuple.count(5)) #prints no. of time 5 is present
#index
print(my_tuple.index(5)) #prints first index of 5
lec54: SETS
#unordered collection of unique objects
my_set={1,2,3,4,5,5}
print(my_set) #output-{1,2,3,4,5} because set hold unique object
my_set.add(100)
my_set.add(2)
print(my_set) #output-{1,2,3,4,5,100}
question-remove duplicates from the list
my_list=[1,2,3,4,5,5]
answer-print(set(my_list))
#set does not support indexing
print(my_set[0]) #this produces error
lec55: SETS2
my_set={1,2,3,4,5}
new_set={4,5,6,7,8,9,10}
#.difference()
print(my_set.difference(your_set)) #my_set minus your_set
#discard
print(my_set.discard(5)) #output-NONE
print(my_set) #output-{1,2,3,4}
#.difference_update() output
my_set={1,2,3,4,5}
new_set={4,5,6,7,8,9,10}
print(my_set.difference(your_set) {1,2,3}
print(my_set) {1,2,3,4,5}
print(my_set.difference_update(your_set) NONE
print(my_set) {1,2,3}
#.intersection
print(my_set.intersection(your_set)) {4,5}
print(my_set & your_set) #same result
#.isdisjoint()
print(my_set.isdisjoint(your_set)) FALSE
#.union()
print(my_set.union(your_set) {1,2,3,4,5,6,7,8,9,10}
print(my_set | your_set) #also the same
#issubset()
print(my_set.issubset(your_set))
#issuperset()
print(your_set.issupeset(your_set))
........................................................................................................................................................................
SECTION 4: PYTHON BASICS2
lec57: CONDITIONAL LOGIC
if condition:
statement 1 #instead of curly brackets or any
statement 2 brackets indention is used
elif condition: #indention is not for decoration in python
staement 1 but its for the interpretation of the interpreter
else:
stament1
statement
#we can join two statements like:
if condition1 and condition2
lec59: TRUTHY VS FALSEY
lec60: TERNARY OPERATOR
#conditional operation
#its a new feature of python3
# condition_if_true if condition else condition_if_false
is_friend=True
can_message="message allowed" if is_friend else"not allowed"
lec61: SHORT CIRCUITING
lec62: LOGICAL OPERATOR
#and
#or
#><
#==
#!=
print(not(True)) #output-False #not is also a keyword
lec64: is vs ==
== checks whwther values are equal
is checks whwther the have same address or not
a=[1,2,3]
b=[1,2,3,]
print(a==b) output-True
print( a is b) output-False
lec65: LOOP
for variable_name in 'string':
eg- for item in[1,2,3]
print(item)
output-
1
2
3
lec66: ITERABLES
iterable- object or a collection that can be iterate over(list,dictionary,tuple etc)
iterated --> one by one check each item in collection
user={
'name':'Golem',
'age': 5006,
'can_swim'=False
}
#method1
for item in user:
print(item)
OUTPUT-
name
age
can_swim
#method2
for item in user.items():
print(item)
OUTPUT-
('name','Golem')
('age',5006)
('can_swim',False)
#method3
for item in user.values():
print(item)
OUTPUT-
Golem
5006
False
#method4
for item in user.keys():
print(item)
OUTPUT-
name
age
can_swim
#method5
for key,value in user.items():
print(ket,value)
OUTPUT-
name Golem
age 5006
can_swim False
lec68: range()
for number in range(1,5)
print(number)
OUTPUT-
1
2
3
4
note- range() also has third parameter
range(start,stop,stepover)
by default start=0,stepover=1
lec69: ENUMERATE()
for i,char in enumerate("hello")
print(i,char)
OUTPUT
0 h
1 e
2 l
3 l
4 o
lec70: WHILE LOOP
while condition: output
i=0 0
while i < 5: 1
print(i) 2
i=i+1 3
4
note-python also has break keyword
note- else can be used with while
lec72: break,continue,pass
pass-does nothing just pass control to next line
use of pass-
for item in my_list
#thinking what to code
pass #we fill the space so that interpreter doesn't return any error
lec76: FUNCTIONS
#use 'def' keyword for defining any function
def say_hello(name,age): #function definition #name,age are parameters
print(f"hello {name} ({age})")
say_hello('tushar','18') #function calling #tushar,18 are arguments
OUTPUT-
hello tushar(18)
note-instead of brackets python uses indentation
lec78: DEFAULT PARAMETERS AND KEYWORD ARGUMENTS
#default parameters
def say_hello(name='tushar',age='18'):
print(f'hello {name} {age}')
#positional arguments
say_hello('andrie','100')
#keyword arguments
say_hello(age='100',name='andrie') #position/order does not matter here
lec79: RETURN
lec82: DOCSTRING
def test(a):
''' info:this function prints param a'''
print(a)
#this string inside ''' ''' is called docstring
#whenever we type test when a popupbox appears it shows this line
like any other built in function does to show what does the function do
#keyword help()
help(test) #output-'''info:this function prints param a'''
#dunder method __doc__
print(test.__doc__) #output- info:this function prints param a
lec84: *ARGS AND **KWARGS
def super_func(*args,**kwargs): #arg ang kwarg are just variable name
total=0 #*arg take the parameter as tupple
for items in kwargs.value(): #args=(1,2,3,4,5)
total+=items #**kwargs take the parameter as dictionary
return sum(args) +total #kwargs={num1:5
num2:10}
print(super_func(1,2,3,4,5,num1=5,num2=10))
#output-30
#Rule: param,*args,deafult parameter,**kwargs
lec87: SCOPE RULES
#1 - start with local
#2- parent local ?
#3 - global
#4 - built in python function
#keyword to access global keyword without creating new one-global
#another keyword – local
…………………………………………………………………………………………………………………………………………………………………………………………….
SECTION5: DEVELOPER ENVIRONMENT
Lec:93 PYTHON INSTALLATION
#Install it from python.org
#While installing click on the allow path option
#Use terminal and type python
#For windows use ‘command prompt’ but ‘power shell’ is recommended as linux and . mac commands are also allowed in power shell
#if your pc doesn’t have power shell use ‘gitbash’
Lec96: PYTHON DEVELOPER TOOL
1.Terminal- used for quick testing
2.code editor –light weighted as compared to IDEs
3.IDEs- has tons of extra stuff
4.Jupytr Notebook
Lec97: CODE EDITOR-sublime test
Best as recommended by the author
To upgrade python interpreter to python3 go to toolcommand palletsearch . . . . . . python3 and click(shortcut to open command palletctrl+shift+P
Explore more features on your own
Lec99: optional:TERMINAL COMMANDS
#To show various elements in present directory
Mac/Linux/PowerShell- ls
Windows – dir
#to show present directory
Mac/Linux/power shell- pwd
Windows – cd
# to enter into directory
cd Desktop
#to go back previous
cd ..
#clearing screen
Clear
Windows-cls
#to enter into root directory
cd /
ls
# user directory
cd ~
ls
note-for more look into book mark section
lec102: VISUAL STUDIO
it’s a code editor
lec103: PYCHARM
its IDE
it has pre-installed everything
lec104: PEP8
python enhancement proposal for clean code
need to be installed in code editors but comes up with IDEs
lec106: JUPYTER NOTEBOOK
anaconda distributor
…………………………………………………………………………………………………………………………………………………………………………………..
SECTION6: ADVANCED PYTHON :
OBJECT ORIENTED PROGRAMMING
Lec109: what is OOP?
#Everything in python is a object
Lec110: OOP
Keyword- class
Convetion- use camel convention that is name the variable as BigWorld instead of . . . bigworld or big_world
Syntax-
Class BigObject: #class
#code
Obj=BigObject()
Lec111: CREATING OUR OWN OBJECT
Class PlayerCharacter:
def __init__(self,name): #__init__ is a dunder method or a constructor method
self.name = name #self is used to reference object of PlayerCharacter
that has not yet created
def run(self)
print(‘run’)
player1 = PlayerCharacter(‘cindy’) #when we create this instance constructor is
print(player1.name) called automatically and seld refered to player1
#output-cindy
We can create new attributes as-
Player1.attack=50
Print(player1.attack) #output-50
Lec112: ATTRIBUTES AND METHOD
Note- help(list) or help(classname) prints blueprint of that data type
Class NameOfClass :
Class_attribute = ‘value’
def __init__(self,param1,param2):
self.param1 = param1
self.param2 = param2
def method(self)
#code
@classmethod
def cls_method(cls,param1,param2):
#code
@staticmethod
def stc_method(param1,param2):
#code
Lec118: ENCAPSULATION
Lec119: ABSTRACTION
Lec120: PRIVATE VS PUBLIC VARIABLE
Python does not have private variables
But we can name variable as _variable by using underscore before the name
This tells other coder from python community as this variable meant to be private
And should not be overwrite
#dunder method(double underscore) – we should not make our own dunder method
(convention) dunder method are named so to tell the coder to prevent it from
Overwriting as python does not have private scope
Lec121: INHERITENCE
Class user():
Def sig_in(self):
Print(‘logged in’)
# to make inherited class
Class wizard(user): #here class wizard inherit the properties of class user
Pass
Wizars1=wizard()
Print(wizard.sign_in())
Lec122: see the video
Lec123: POLYMORPHISM
Lec125: SUPER()
Class user():
Def __init__(self,email):
Self.email = email
Def sign_in(self):
Print(‘logged in’)
Class Wizard(user):
Def __init__(self,name,power):
Self.name = name
Self.power = power
Def attack(self)
Print(f’attacking with power of {self.power}’)
Wizard1=Wizard(‘merlin’,100)
Print(Wizard1.email) #this give error though user class is inherited to wizard class
But object of wizard class cant access __init__ function of . . user class .
#to solve this :
#method 1:define wizard like this:
Class Wizard(user):
Def __init__(self,name,power,email)
User.__init__(self,email)
Self.name = name
Self.power = power
Def attack(self)
Print(f’attacking with power of {self.power}’)
Wizard1=Wizard(‘merlin’,100,’[email protected]’)
Print(Wizard1.email) #output- [email protected]
#method : use of super():cleaner code
Class Wizard(user):
Def __init__(self,name,power,email)
Super().__init__(email)
Self.name = name
Self.power = power
Def attack(self)
Print(f’attacking with power of {self.power}’)
Wizard1=Wizard(‘merlin’,100,’[email protected]’)
Print(Wizard1.email) #output- [email protected]
Lec126: OBJECT INTROSPECTION:
#dir function
Print(dir(wizard1))
#this prints all the methods and dunder methods accessible to object wizard1
Help us to identify to which methods we have access to
Lec127: DUNDER METHODS or magic methods
Everything in python is inherited from base class ‘object’
Class toy():
Def _init__(self,color,age):
Self.color = color
Self.age = age
Action_figure = toy(‘red’,0)
Print(action_figure.__str__)
Print(str(action_figure) #both produces same output as dunder method __str__ is . . . used to implement the function of str()
#we can modify dunder methods but we shouldn’t
#modify __str__
def __str__(self):
return f’{self.color}
print(str(action_figure)) output-red
print(str(‘action_figure)) action_figure
#modifying doesn’t change actual function of str but it change its function only when it
Is used with the object of toy.few more eg. To go:
def __del__(self): #generaly delete some item inside object
print(‘deleted’)
def __call__(self):
return(‘yes??’)
def __getitem__(self,i): #for this you need to modify __init__ in above code as:
return self.my_dict[i] self.my_dict={
‘name’: ‘yoyo’,
del action_figure ‘has_pets’: False
print(action_figure()) }
print(action_figure[‘name’])
#output-
deleted
yes??
yoyo
Lec129: MULTIPLE INHERITENCE
Syntax- class variable(user,wizard,…)
Class user():
Def __init__(self,email):
Self.email = email
Def sign_in(self):
Print(‘logged in’)
Class Wizard(user):
Def __init__(self,name,power):
Self.name = name
Self.power = power
Def attack(self)
Print(f’attacking with power of {self.power}’)
Class archer():
def __init__(self,name,arrows):
self.name = name
self.arrows = arrows
def check_arrows(self):
print(f’{self.arrows} remaining ‘)
def run(self):
print(‘ran really fast’)
class hybrid(wizard,archer):
pass
hb1 = hybrid(‘borgie’,50)
print(hb1.run())
print(hb1.check_arrows()) #this produces error:object hb1 has no attribute named
check_arrows().because wizard is inherited first so
interpreter assumes 50 as power not arrows
#to solve this
Class hybrid(wizard, archer):
def __init__(self,name,power,arrows)
archer.__init__(self,name,arrows)
wizard.__init__(self,name,power)
hb1 = hybrid(‘borgie’, 50, 100)
print(hb1.check_arrows()) #now this works
Lec130: MRO-Method Resolution Order
MRO tells the interpreter the order to check attribute in inherited class in that order
We can check that order by
inheritance hierarchy:-
A
B C
D
interpreter first checks for the attribute in D then B(if D is inherited as D(B,C)) then C then A then universal base class ‘object
note- we can also check the mro order by writing
print(D.__mro__)
……………………………………………………………………………………………………………………………………………..........................
SECTION7: ADVANCED PYTHON:FUNCTIONAL PROGRAMMING
lec132: FUNCTIONAL PROGRAMMING
separate data and function unlike OOP
but both methods have same goal
pillar-1)pure function
lec133: PURE fUNCTION
input-functionoutput
if function is pure
input and are same type# no matter how many times we run the function it poduces . . . . same output
it doesn’t have any side effect #if it interact with outside world
eg-
def multiply_by2(li):
new_list=[]
for item in li:
new_list.append(item*2)
return new_list
lec134: MAP()
syntax-map(func,*iterable)
def multiply_by2(item):
return item*2
print(list(map(multiply_by2, [1,2,3])))
lec135: FILTER()
my_list=[1,2,3,4]
def only_odd(item)
return item % 2 !=0
print(list(filter(only_odd,my_list))) #output-[1,3]
lec136: ZIP()
my_list=[1,2,3]
your_list=[10,20,30]
print(list(zip(my_list,your_list))) #output-[(1,10),(2,20),(3,30)]
#we can have multiple arguments to zip more elements together
lec137: REDUCE()
#to use we need to import some files
from functools import reduce
my_list=[1,2,3]
def accumulator(acc,item):
return acc + item
print(list(reduce(accumulator,my_list,0))) #output-6
lec139: LAMBDA EXPRESSION
#lambda expression is used to replace function which is used only once in a program
and doesn’t need to be stored in the memory.
syntax- lambda param : action(param)
#we can replace the following code :
def multiply_by2(item):
return item*2
print(list(map(multiply_by2, [1,2,3])))
#by this:
print(list(map(lambda item: item*2, my_list))) #here any name is not assigned to func
def accumulator(acc,item):
return acc + item print(reduce(lambda acc,item: acc+item ,my_list))
print(list(reduce(accumulator,my_list,0)))
lec140: EXERCISE
question- sort a=[(0,2),(4,3),(10,-1),(9,9)] on the basis of 2nd element i.e.(2,3,-1,9)
hint-a.sort() sorts w.r.t 1st elements i.e.(0,4,10,9)
sort function is .sort(key=NONE,reverse=False)
solution-
a.sort(key=lambda x: x[1])
print(a)
lec141: LIST COMPREHENSIONS
list comprehensions
set comprehensions
dictionary comprehensions
#we can replace this:
my_list=[]
for char in ‘hello’:
my_list.append(char)
#by this :
my_list=[char for char in ‘hello’]
#code to print even
my_list=[num for num in range(0,100) if num % 2 ==0]
print(my_list)
lec142: SET AND DICTIONARY COMPREHENSION
#set comprehension is same as list just replace [] with {}
#dictionary comprehension
simple_dict = {
‘a’ : 1,
‘b’ : 2
}
my_dict = {key:value**2 for key,value in simple_dict.items()}
print(my_dict)
…………………………………………………………………………………………………………………………………………………………………………….
SECTION8: ADVANCED PYTHON:DECORATORS
lec146: HIGHER ORDER FUNCTION,HOC
function that accepts function as parameter or returns function
lec147: DECORATOR
it simplify a function(HOC) that wraps another function and enhances its functionality
#decorator
def my_decorator(func):
def wrap_func():
print(‘******’)
func()
print(‘******’)
retun wrap_func
@my_decorator
def hello():
print(‘hello’)
#output-
******
hello
******
lec149: why do we NEED DECORATOR?
#define a decorator that checks performance
from time import time #to module(going to study about this ahead)
def performance(fn):
def wrapper(*args,**kwargs) #to make function flexible and to accepts any
t1=time() any number of arguments
result=func(*args,**kwargs)
t2=time()
print(f’took {t2-t1} sec’)
return result
return wrapper
@performance #now this can be used over any function to know its performanc
def long_time():
for i in range(100000000)
i*5
#output-
took 19.0450119972229 sec
……………………………………………………………………………………………………………………………………………………………………………..
SECTION9: ADVANCED PYTHON:ERROR HANDLING
lec151: ERRORS IN PYTHON
lec152: ERROR HANDLING
age=int(input(‘what is your age?’))
age+10
#on entering string or other than int it produces valuerror
#error handling
try:
age=int(input(‘what is your age?’)) #this avoids error instead of showing error msg
age+10 except block is run
except:
print(‘please enter a number:’)
#we can also specify the error
while True:
try:
age= int(input(‘what is your age?’))
print(age+10)
except ValueError as err:
print(f‘please enter a number {err}’)
except ZeroDivisionError:
print(‘please enter higher num than zero’)
else:
print(‘thank you’)
break
#we can also combine errors as:
except (ValueError,TypeError) as err:
print(err)