forked from prabhudev740/pythonbatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes.txt
More file actions
955 lines (708 loc) · 23.8 KB
/
Notes.txt
File metadata and controls
955 lines (708 loc) · 23.8 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
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
Python is High level Language and interpreted Language
It is developed by Guido van Rossum in 1991.
Why Python or Advantages of Python
-------------------------------------
1) free and opernsource
2) It is easy t learn: bcz its english like Syntax
3) Its interpreted lang. incase we get any error it will point to that perticular line
4) It will take less lines of code for same funcionality, redunce in devopment time
5) more then 1.5 lakn packages and more than 7 core modules
6) Its portable
7) Dynamic progamming language
Uses of Python
----------------
develop desktop, mobile application
game devopment
web development
networking
Data Science
Automation
AI/ ML/ Dl /Big Data
Shell Scripting
Keywords
-----------
These are reseved words, to perform prerticular task
there are 35 Keywords till Python 3.7
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
Variable
--------------
var = value
identifier = value
Identifiers
-------------
these are the names which are used to identify the variables, fumctions, classes, objects, etc
There are set of rules to define a varible name/ identifier
-----------------------------------------------------------
1) variable names are case sensitive
2) can't use Keywords
3) can't special char except _
4) can't use space at first or middle
5) varible should not start with numbers (we can give number at middle or at the end)
6) it can be of any length but it should be meaningful
variables
------------
Variables are reseved memory locations, where we can some data/ values
To define a variable we have to take help of Identifiers
when we create a varible, it reseve some memory space
all the values will stored Heap memory and adress of varible stored in stack memory
Syntax : var = value
x = 10
name = "abc"
is_valid = True
is_valid = False
x = 20
name = "xyz
Data Types
---------------
These are the types of data stored inside variables
These are of 2 types
--------------------
1) Single Valued Data types
2) Collection Data Types
Data types
/ \
Single Valued DT collection DT
/ \ -----------------
Numeric Boolean string ("Prabhu", 'name', '10')
----------- --------- list ([1, 2, 3])
int (10, -1) True tuples ((1, 2, 3))
float (10.5) False set ({1, 2, 3})
complex (2+3j) dict ({key: value, key2: value2})
type() : it is used to check type of variable
integer
-------
all the positive and negetive whole numbers without decimal points.
represented as "int"
Ex : 10, 0, -100, -5000, 276
Default value -> 0
int() : is used check the default value of integer
float
---------
it is a positive or negetive number with decimal point
represented as "float"
Ex: 10.55, -500.0, -0.67, 0.0
default value -> 0.0
float() : used to check default value of float
complex
-----------
complex = real + imag
it is combinamtion real and imag no.s
format : a + bj // if we write J it will internally converted to 'j'
a => real part => a can int or float
b => imag part => int or float
represented as 'complex'
Ex : 1 + 5j, 5j, 5.0 + 11.4j
Default value : 0j
complex() : to check default value
value of j = Square root of -1
Boolean
--------------
In python Boolean are 2 Keywords \. i.e True , False
represented "bool"
Ex : True, False
Default value : False
bool() : to check default value of Boolean
string
----------
strings are collection data type enclose, ' ', " ", ''' ''', """ """
we cam n n.o char inside string
"A -Z", "a -z", "0-9", Special char ex - ][, _ - +
represented as "str"
default value => ''
str() -> to check default value
memory allocation
----------------------
Whenever controller see a collection data type, it will create memory layer
then it check the lengh of that collection / string , len = n
it crate n block of memoty inside that layer
It will store all values one by one respectively
Then it give index values
Give adress to that objext
var will refer to that objet
Strings are immutable
it has indexing, its ordered data types ,
s = "1234" , len = 4
0 1 2 3
| 1 | 2 | 3 | 4 | ----> 0x55
-4 -3 -2 -1
s -> 0x55
indexing or subadressing
---------------------------
tere are two types of indexing
1) +ve (0 -> len(collection) - 1)
2) -ve (-1 to -len(collection))
Syntx of accessing a value from string
varname[index]
as its immutable we can't replace or assign a value from string
var[idex] = new_val
len() => chexck the len of collection
list
-------
this is a collection of hemog.. of heterg data Types
we can store n no. of values inside a list
These are ordered statypes, so these have indexing
These are mutable datatypes
these are enclose within []
these are comma (,) separated values
represented as 'list'
Sytax : var = [val1, val2, vl3......valn]
a = [1, 2, 3, 4]
default value => []
list() =. t check default
len(list) => to ceck length
memory allocation
--------------------
It will create memory layer in heap memory
it will check len list
It will divide that memory layer into len of blocks
It will store vales one by one respectively
It give index value
Assign some adress to that objects
varible in stack will refer to that address
tuples
------------
Tuple is collection data type of homogeous ans heterogenous.
It is immutable
Its a oredered data type
All the data stored inside (), separated by comma (,)
represented as 'tuple'
It is more secure than list.
Syntax : var = (val1, val2, val3, .... val n)
ex : x = (1, 2, 3)
default value : ()
tuple() => to check default value
len() => to check len
set
------
Set is a collection datatypes, homogeous and heterogenous datatypes
We can store n no of value, but it doesm't allow duplicate valuws
all the values are enclosed within {}
All values will be separated by comma
It is am unorederd data type, so it doesn;t support indexing
All the data in set stored randomly or shuffled manner
represented as 'set'
Syntax : var = {val1, val2, ...val n}
x = {1, 2, 3, 4}
default val => set()
set() => check default val
len() => check len
memory manangmt
--------------
It will create memory layer in heap memory.
It check the length of set, divide that memory into that muh parts
It will store all the randomly
it will some address
in stack memory var will point to the address
Dictionary
-------------
This is a collection DT with Key, Value pair
To assign any value for any key we use colon (:)
all key value pairs will be separated by ,
enclosed within {}
Key can't be duplicated but we can store duplicate values
Dictionary is mutable :- update, add, remove, etc operations can be does
It is unorederd DT
keys are case sensitive
keys must be immutable or single values Data Types, we can't write set and list as dict key
and value can be of both mutable and immutabe
controller can only access to keys
represented as dict
Synatx : var = {key1:val1, key2:val2, ...., key n : val n}
ex : - x = {1 : True, False : "No", 0j : [1, 2, 3], 10.5 : (1, 2, 3), "name' : "Msys", "Name: True}
To acces a value : var[key]
ex : x[1] --> True
x[0j] --> [1, 2, 3]
To update value of key -> var[key] = new val
to to add new key value pair : var[new_key] = new_val
default value --> {}
dict() -> to check default value
len() -> to check no of elements
memory manangmt
==================
when our controller sees a dict. DT it will create a dict block with key and val in Heap memory
first it will store all the leys
then it will store values one by one
then assign some address to that dict block
in stack memory var will point to that address
Slicing
-------------
Its a way of extracting required set of elements from a given collection
we use indexing to perform Slicing
can be performed on ordered DT -> string, list, tuple
arr = [1, 2, 3, 4, 5, 6, 7, 8]
Syntax : var[start_index: end_index +/- 1: updation/step< optional >]
they are of two types
+ve update
-ve update
+ve updation
-------------
When we want to fetch value from left to right we use +ve updation
start_index < end_index
Syntax; var[start_index:end_index + 1:step/updation]
default start_index = 0
default end_index = len(collection)
default updation = +1
we use step or updation skip character from give collection
-ve updation
----------------
we fetch value from right to left
start_index > end_index
Syntax : var[start_index: end_index -1: stpe]
default start_index = > -1
default end_index => -len(collection) -1
step => -1
Default values
---------------
DT func val
----- ----- ------
integer int() 0
float float() 0.0
complex complex() 0j
Boolean bool() False
string str() ""
list list() []
tuple tuple() ()
set set() set()
dict dict() {}
Types Casting
---------------
To converting one DT another DT
Source DT -------> converted DT
-------------- -----------------
float, bool, str(only integer value) ---> int
int, bool, str(only int or float value) ---> float
int, float, bool, str (all numeric DT int, float, complex) --> complex
any DT (default will return False, else True) ---> bool
any DT ---> str
str, tuple, set, dict (list of keys only) ---> list
str, list, set, dict (list of keys only) ---> tuple
str, list, tuple, dict (list of keys only) ---> set
nested data types (with 2 values) ---> dict
ex: ([1, 2], ["a", "b"])
[{1, 2}, [3, 4], (1, 2)]
Operators
-----------------
These are special symbols used to perform specific task.
Syntax : val 1 (Operator) val 2
val 1 and val 2 are called operands
There are 7 types of Operators
==============================
Arithmatic Op.
logical Op.
Relational op.
Assignment Op.
Bitwise Op.
Membership Op.
Identity Op.
Arithmatic Op.
-------------------
These are used for Arithmatic operations
a) addition --> +
b) substraction --> -
c) multiplication --> *
d) true division --> /
e) floor division --> //
g) modulus --> %
h) power --> **
a) addition / concatenation --> +
Synatx : val1 + val2
svdt + svdt ---> addition
collection + collection ---> concatenation
* collections must be of same type (only str, list, tuple)
b) substraction -> -
Synatx : val1 - val2
svdt - svdt
this is applicable to svdt only
c) multiplication -> *
Synatx : val1 * val2
svdt * svdt
collection * int
* we can multiply collection (str, list, tuple only) with only int
d) tru division --> /
Synatx : val1 / val2
return the result in float value
e) floor division --> //
Synatx : val 1 // val 2
return the result in int value
f) modulus --> %
it return reminder
Syntax : val 1 % val2
g) pow --> **
it return pow of val 1 with respect to val 2
Synatx : val 1 ** val 2
Logical Op.
---------------
Logical and
Logical or
logical not
Falsy Values => all the default values (0, 0.0, 0j, False, "", [], (), set(), {})
Truthy Values => all values except default values are Truthy values
Relational Op.
----------------
It will return Boolean value ( True or False)
1) equal to (==)
2) not equal to (!=)
3) less than (<)
4) greater then (>)
5) less than or equal to(<=)
6) greater then or equal to(>=)
1) equal to
----------------
It will check both values are same or not
If both values are equal it will return True, else it will return False
It will check all the values using their ASCII val
val 1 == val 2, 10 == 5 -> False and 5 == 5 -> True
2) not equal to ( != )
----------------------
It will check both values are same or not
If both values are same, it will return False, else it will return True
Syntax : val 1 != val 2,
10 != 20 -> True
"" != " " -> True
5 != 5 -> False
3) less than (<)
-------------------
It will check if the val 1 is less then val 2.
if val 1 is < val 2 , return True, else False
for svdt it will check bpoth the values compare then, return the res. ( except complex value)
for collection, it will compare their length
it will elements 1 by one , if condition satisfied then true, else False
4) greater then ( > )
-----------------------
if the val 1 is > val 2
if val 1 > val 2, it will return True, else False
5) less than or equal to(<=)
---------------------------------
It will check if the val 1 is less then or wqual to val 2 or not
if val 1 is <= val 2 , return True, else False
6) greater then or equal to(>=)
---------------------------------------
if the val 1 is >= val 2
if val 1 >= val 2, it will return True, else False
Assignment Op.
-------------
var = val
1) a = 10
2) a += 5 ---> a = a+ 5
3) a -= 10 ---> a = a- 10
4) a *= 10 ---> a = a* 10
5) a /= 10 ---> a = a / 10
6) a //= 10 ---> a = a// 10
7) a %= 10 ---> a = a % 10
8) a **= 10 --> a = a ** 10
5) Bitwise Op.
---------------------
a) Bitwise and ( & )
b) Bitwise or ( | )
c) Bitwise not ( ~ )
d) Bitwise xor ( ^ )
e) Bitwise right shift ( >> )
e) Bitwise left shift ( << )
a) Bitwise and ( & )
----------------------
Syntax : val1 & val2
it will both val to its binary form
controller will stact computation from right to left
if both the values are 1 it will be 1, else 0
b) Bitwise or ( | )
--------------------
Syntax : val1 | val2
it will convert to binary form
controller will start combinamtion from right to left
if atleast 1 val is 1, it will be 1, else 0
it will return the valu as int
c) Bitwise not ( ~ )
---------------------
Syntax : ~ val
controller will convert the val into binary
controller will start computation frm right to left
if +ve numbers
------------
it will invert the values one by one from right to left untill it found first 0
it will invert the 0 also
the remaing values are taken as it is
it will return the val in int format as -ve
if -ve numbers
-----------------
it will invert the values one by one from right to left untill it found first 1
it will invert the 1 also
the remaing values are taken as it is
it will return the val in int format as +ve
d) xor ( ^ )
--------------
Syntax : val1 ^ val2
it will convert to binary form
controller will start computation from right to left
if aif both the values are equal it gives 0, else 1
it will return the valu as int
e) Bitwise right shift ( >> )
-------------------------------
+ve number
---------
shifts the number to right and filled with 0 in void
-ve number
-------------
shifts the number to right and fill with 1 in void
f) Bitwise left shift ( << )
---------------------------
it will shift the to left
fill 0 in void wheather its +ve number or -ve number
6) Membership Op.
--------------------
for mebership op. we use in and not in
return type 'bool'
in
----
Syntax :
val in collection --> str, list, tuple, set, dict
this Operator is used to check if a value is a member of a collection or not
if it is present in that collection it will return True
else it will return False
in dict we can't access the values, we can access the keys only
not in
----------
val not in collection
if val is not present in collection it return True
if present it return False
7) Identity Op.
------------------
identify op. are of 2 types
is
is not
is
-------
this Operator compares two objects if both are same then it will return true
otherwse False
Synatx: val1 is val2
is not
--------
it will check if both objects are same or diffreent
if both are same objet it will return False
otherwse it will return true
val1 is not val2
identify Operator mainly used to check if a value is None or not
Control Statement
------------------
They are of 2 types
1) Decisional Statement
2) looping Statement
1) Decisional Statement
--------------------------
these are the Statements uhich will control the flow of execution of progam.
These are of 4 types
1) if
2) if ... else...
3) elif
4) nested if
if Statements
---------------------
it is a type of Statement which executes some set of statemnts called true statement block(TSB)
when the constion becames true or condtion statisfied
if the constion becomes false code will not executes and directly go the next line
Syntax :
if condtion:
TSB ( true statement block)
if ... else ....
-----------------------
it is a type of statement , which executes some set of statement if constion is ture, that statement is called TSB
if the constion is False it will execute false statement block
Syntax:
if constion:
TSB - True statement block
else:
False Statement Block (FSB)
elif
-------------
it is a type of statement, which will check n number of conditions
if any condtion becames true, it will execute the TSB(True statement Block) of that condition
and it will exit the statement without checking following condtions
if condition is false controller will go the next condtion and if it is true, it will execute the TSB then it will exit the statement
if all the conditions are Fasle then it will execute the default False Statement
Syntax:
if condtion 1:
TSB 1
elif condtion 2:
TSB 2
.
.
.
elif condition n:
TSB n
else:
FSB
we have to start with if and else is optional here.
nested if
-------------
An if condition inside another if constion is called nested if
we use this to break the huge or big condition to small condition
Syntax:
if outer_condition 1:
if inner_constion 1:
TSB
else:
FSB
else:
if inner_constion 1:
TSB
else:
FSB
looping
--------
looping is used to iterate over collection. (visiting all elemnts 1 by 1)
we can execute a block of code n number of tme using Looping.
There are two types of looping
while
for
while looping
--------------
while loop is used to execute a block of statements repeatedly until a given condition
satisfies. when the condtion becomes false it execute the next line.
initialization
while condtion:
statements
updation - increament / decrement
Note - if we are unbale to make this condition False, we will get infinite loop
i.e. The loop will continue for infinite times.
ex: printt all numbers from 1 to 10 incuding 10.
n = 1
while n <= 10:
print(n)
n = n + 1
break
continue
pass
break statement
----------------
it is used to break the loop and send back to main space
this statemnt is only written inside a loop
it is used to break the loop
continue statement
-----------------------
It will end the current iteration
it will excute the next iteration
pass
-------
pass is uswd as a placeholder for future code
when the pass statement is excuted nothing happens, but it helps to avoid getting empty code error
it defines empty code block
For Loop
------------
when we know the exact number of iteration we will go with for loop
we uuse in Operator to get the values
for val in collection:
statement 1
statement 2
statement n
range()
------------
by using range() we can iterate all the numbers
it will return a range objects
+ve updation
range(start, end + 1, 1)
-ve updation
range(start, end - 1, -1)
Nested loop
------------
Executing one loop inside another loop
Nested For
----------------
Syntax :
for val in collection:
for val2 in collection2:
statement 1
statement 2
outer statement 1
outer statement 2
Nested while
enumerate()
------------
It is a built in function
for a ordered collection it will return index and values
Syntax:
for i, v in enumerate(collection):
print(i, v)
zip()
-------
It is an inbuiltfuction
it will takwe 2 collection with same lengh or it will slice to it to smallest len collection
it will return collection tuples
zip(collection1, collection2)
Fuction
--------
function is a block reusable code, it is only runs when it is called.
it will take some arguments and return some value , this is optional.
What are the types of functions in python?
1) inbuilt function
2) user defined function
1) inbuilt function
-------------------------
these are the prewritten functions by developers to perform some specific tasks.
Ex : len()
sum()
print()
min()
max(), etc
2) user defined function
-------------------------
These are the customized function, written by user to perform specific task as per requirement
to define a user defined function we use "def" keyword.
to execute this function we need to call it
to call a function we have use function name with parenthesis
Ex : function_name()
we cant call a function before execution.
The function always return to the function from where it is called
components of functions
------------------------
def keyword
function name
arguments within parenthesis, separated by comma
statements
return statement
function call
def func_name(args):
statement 1
statement 2
statement 3
return statement
Ex :
def add(n1, n2):
res = n1 + n2
return res
add(1, 2)
Background process
----------------------
Our all codes are executing inside main space.
But the statements in functions are stored/written in function Area
when our controller see the def keyword, it will create a block in FA
it will store the arguments
it will store all the statement in that function block
it will assign some address to that block
noe in main space function will refer to that address
Note: Functions always return None bydefault
user defined functions are of 4 types
----------------------------------------
1) without args and without return value
2) with args and without return
3) without args and with return
4) with args and with return value
1) without args and without return values
-----------------------------------------
Here this function is not taking any arguments and it will not return anything
Syntax :
def func_name():
statement 1
statement 2
statement 3
func_name()
Ex :
def display():
print('message)
display()