-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_notes
More file actions
202 lines (113 loc) · 5.13 KB
/
python_notes
File metadata and controls
202 lines (113 loc) · 5.13 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
java/c++ try throw catch
python try raise except
wtf :: python does not support inline assignment if(num=arg): not supported in python , but supported in c
every module is an object and they have a special property __name__
__name__ is the filename of the module , BUT IF THE PROGRAM IS CALLED AT FIRST WITHOUT IMPORTS ,
then __name__ is __main__
date types ::
Booleans
Numbers , integer -float-fraction-complex number
Strings
Bytes and byte arrays
Lists
Tuples
Sets
Dictionaries -- KEYS SHOULD BE IMMUTABLE
Booleans can be treated as numbers
True =1 , False = 0
"" ZERO VALUES ARE FALSE, NON ZERO ARE TRUE ""
True+True = 2
type()
and
isinstance()
int() will truncate, not round
/ => always floating division
// , for positive truncate for negative rounding down
fractions module
Lists :::
lists ['a','b','c']
slicing of list ,
lists can be added by .
adding two lists ,
append method append adds only 1 element to teh list , if a list is added , a list items stays as 1 element
insert method
extend mehtod all memebers are added
list.count('item')
list.index('item')
'item' in list
to remove from a list
del list['index']
list.remove('value')
list.pop()
list.pop(4)
list.shift()
lists can also be used as boolen , any list with at least an element is evaluated as True
''' PYTHON 3 , DAY 3 , PAGE NO:- 70 chapter :- TUPLES
In lists, square bracket is used , whereas in teh case of tuple , parenthesis are used ()
tuples are immutable
Tuples are same as Lists BUT THEY CANNOT BE CHANGED ,
TUPLES HAVE NO APPEND,POP,REMOVE,ADD METHODS LIKE THAT OF LISTS ,,
Tuples can be used for multiple assignments , if multiple values are returned from a function , then they can will
be saved as a tuple or individual parameters can be given to save them..
/// gotchaa , teh LHS variables should be equal to the number of values returned or should always be 1
SETS ARE ANOTHER DATA TYPES , their ordering is not determinable , union/intersection can be performed.
sets are implemented as class
sets can be created from list/tuple with set(<name>) method.
{} doesnt create empty set but creates an empty dictionary.
items can be added to the sets with add or update method,
add takes a single item to add to the set ,
update , takes a single SET to add its elements to the set.
items can be removed by the methods discard , remove , pop
REMEMBER , sets have no specific ordering
in set operations , symmetric_difference() , union() , intersection() are symmetric i.e.
a.symmetric_difference(b) equals b.symmetric_difference(a) and so on
DICTIONARY IS ANOHTER DATA TYPE
similar to set, but uses a key-value pair , list is UNORDERED SET OF KEY/VALUE PAIR , {}
### OS MODULE ###
os module takes care of operating system things.
os.path.join() used to construct os path names .. no need to worry of slashes
os.path.split() , splits a path into dir and file
os.path.splitext() , splits a filename into filename and extension, tuple returned
### GLOB MODULE ###
glob modules, to use wildcard characters for file/directory things
os.stat ,, shows metadata about the file ,
tiem.localtime , to convert unix time to local time
os.stat('file').st_size ,,, // returns the file size in bytes
os.path.realpath('fiel') // gives the absolute path for a file
os.path.relpath('file1') // prints the RELATIVE path
UTF - 32 || UTF - 16 || UTF - 8
*** ALL STRINGS ARE A SEQUENCE OF UNICODE CHARACTERS ***
sys.modules is a dictionary of all the imported modules , keys are the strings of module names and values are the module object
In an Item-Key , if the starting is number then it is treated as a number everything else is treated as a STRING
Bytes are bytes , characters are characters , strings are strings..
to declare a byte ,
byte1=b'abcd\xaa'
immutable byte cna be converted to mutable bytearray using bytearray() method
byte objects have decode method to convert to string,
string objects have encode method to return a byte
ROMAN NUMBERS :-
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
VARIABLE PASSING IN PYTHON, HOW ARE THEY PASSED , BY VALUE OR BY REFERENCE OR CALL-BY-OBJECT
REGULAR EXPRESSIONS AND CLOSURE/GENERATORS
re.sub('[abc]','o','Mark') is used to replace the 'a' in Mark by 'o' ,, this replaces all instance of a or b or c
so, maark => moork
PRINT LOCALS() , PRINT GLOBALS()
LIST OF PATTERNS \\\\ PYTHON
CLOSURES == USING VALUES OF OUTSIDE PARAMETERS INSIDE A DYNAMIC FUNCTION
**********
String is itself an iterator , giving out character on each iteration
and join() method takes an iterator as an argument
**********
Generators save space/memory , when using temporary lists just for a loop or sth, do use generators
itertools.permutations()
itertools.combinations()
itertools.groupby(seq,func) <= it works only if the sequence is already sorted
translate() method , takes a string and then a translation table
translation table consist of 1 byte:1byte to replace
eval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVILeval() is EVIL