-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython-Notes.txt
More file actions
781 lines (564 loc) · 21.5 KB
/
Copy pathpython-Notes.txt
File metadata and controls
781 lines (564 loc) · 21.5 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
Large collection of python notes.
Jupyter Notebook
type its name in command prompt opens jupyter notebook
Ctrl+Alt+N
Runs code in vscode with Code Runner Extension
PyPi repo for open-source third party packages
rubygems in rubygems
php packagist
cpan for properly
npm for node
internal libraries are known as standard libraries
use pip to install
pip install
colorama - allows you to add color to items in command line
requests - allows you make http requests with python
openpyxl - use excel files in python
pylint - checks code and reports possible errors
pylint not recognized in w10
add address to path variable in w10
https://www.youtube.com/watch?v=i4yJi-cpwWk
black - Python code formatter.
https://www.youtube.com/watch?v=esZLCuWs_2Y&feature=youtu.be
Basic Practice:
http://codingbat.com/python
More Mathematical (and Harder) Practice:
https://projecteuler.net/archives
List of Practice Problems:
http://www.codeabbey.com/index/task_list
A SubReddit Devoted to Daily Practice Problems:
https://www.reddit.com/r/dailyprogrammer
A very tricky website with very few hints and touch problems (Not for beginners but still interesting)
http://www.pythonchallenge.com/
PEP-8 Style guide
https://www.python.org/dev/peps/pep-0008/
py -3 -m venv [folderName]
Creates Virtual Environment
-3 is version of python
-m is module you want to install
venv virtual Environment
[folderName is the name of the folder you want to create]
requirements.txt
a list of dependencies application will need to function properly
(similar to package.json)
pip install -r requirements.txt
installs all the dependencies. (similar to npm install command)
ast - abstract syntax tree - similar to a DOM tree but for python code in a file.
This is how the interpreter views python code.
# are comments
'''
These are doc strings
'''
print("something")
prints to console.
type()
gets the data type of whatever you pass into the method
NoneType is none (absence of value)
help()
returns documentation on whats in the parameter
dir()
returns a list of methods that can be used with datatypes/variables w/e
help(set().add)
Mutable - types that can be changed without have to be recreated in memory
Immutable - types that cannot be changed and are pointers to a stored object in memory.
Callables - Are anything that can be called
user-defined functions
generators
classes
instance methods
Singletons - objects
None
NotImplemented
ellipsise
All code implicitly at indentation level 0 gets ran.
variables
variable names should be snake_case
_myVar - Single underscore indicates 'internal use' or 'private'
Objects named this way will not get imported by a statement such as:
from module import *
__myVar - Double underscore aka 'dunder' Used to "mangle" class attributes - use in inheritance chains
__myVar__ - Double dunder are system-defined names that have a special meaning to the interpreter
Dont invite variables, stick to python pre-defined
'__all__'
'__args__'
'__author__'
'__bases__'
'__builtin__'
'__builtins__'
'__cached__'
'__call__'
'__class__'
'__copy__'
'__credits__'
'__date__'
'__decimal_context__'
'__deepcopy__'
'__dict__'
'__doc__'
'__exception__'
'__file__'
'__flags__'
'__ge__'
'__getinitargs__'
'__getstate__'
'__gt__'
'__import__'
'__importer__'
'__init__'
'__ispkg__'
'__iter__'
'__le__'
'__len__'
'__loader__'
'__lt__'
'__main__'
'__module__'
'__mro__'
'__name__'
'__package__'
'__path__'
'__pkgdir__'
'__return__'
'__safe_for_unpickling__'
'__setstate__'
'__slots__'
'__temp__'
'__test__'
'__version__'
Method/function names should use snake-case not camel case
def method_name:
var_name = "snake case"
Strings are immutable
you can't use indexing to change individual elements of a string
String objects do not support item assignment.
Dont support item assignment.
Immutability is when you cannot change the object itself when manipulating it.
You would have to create another object with the changes.
# are comments
a = "string"
a[::-1]
reverses the string
"gnirts"
a[1:]
after first char to end of the world
#tring
# String multiplication
letter = 'z'
letter * 10
# 'zzzzzzzzzz'
#string formatting
.format()
f-strings
n = "Yael"
print('Hello, my name is {}'.format(n))
# Hello, my name is Yael
# formatting floating point numbers
# float formatting follows "{value:width.precision f}"
# width is the whitespace
# https://pyformat.info/
result = 100/777
print(result)
print("The result was {r:1.3f}".format(r=result))
0.1287001287001287
The result was 0.129
# f strings
print(f'Hello, his name is {n}')
.capitalize() - capitalizes first letter
.title() - capitalizes every first letter of the word
Lists (arrays in python)
support indexing and
lists are mutable
supports multidemensional arrays
can have multiple object types
# can concatenate lists
l1 = ['one', 'two', 'three']
l2 = ['four', 'five', 'six']
l1 + l2
['one', 'two', 'three', 'four', 'five', 'six']
# add to end of list. Similar to .push in js
l1.append('addition')
# pop, removes items from the end of the list and returns that object
# by default index location is -1
l1.pop()
popped_item = l1.pop()
popped_item
# sorting is done by .sort() (like js)
# does not return a list, just sorts it in the current lists
# reversing is done by .reverse() (like js)
# does not return a list
Dictionaries
Are objects retrieved by key name
(lists are retrieved by index position)
Are mutable like Lists
Unordered and cannot be sorted
Formatting is similar to json
Can support multidemensional dictionaries
dictionaries are not ordered they are sets.
ordereddict object can support sorting in dictionaries.
# calling items in dict
newDict = {"a": 1, "b": 2}
newDict["a"]
1
Tuples
Similar to lists but are immutable.
Once element inside of a tuple it, cannot be reassigned.
tuple object does not support item assignment
uses parenthesis
You won't use tuples often
You use tuples for when you pass around objects in your program that you don't want to get accidentally changed.
Data integrity
Does not have a lot of methods for tuples
t[0]
calls first value of tuple. Similar to list/(arrays in js)
t.count('val')
counts how many times 'val' appears in tuple.
Sets
unorder collections of unique elements
similar to arrayLists in Java.
does not contain duplicate elements
Union and Intersection
Union - combination of two sets
designated by the union symbol 'U'
.union()
Intersection - values that are similar in both sides of the union
designated by upside down union symbol (upside down u)
.intersection()
Booleans
first letter of True or False has to be capitalize
FileIO
%%write myfile.txt
writes a text file with jupyter notebook
open('[path]')
opens file
(does not work with extension, use python filename.py command)
provide full file path to open a file somewhere else on your system.
open('[path]',mode='val')
mode='r' - read only
mode='w' - write only
mode='a' - append only
mode='r+' - reading and writing
mode='w+' - writing and reading (overwrites exisiting files or creates a new file)
f.ready()
Returns output of the file.
f.seek(0)
resets the read cursor to beginning of the file
f.readlines()
creates a list with each line on the list.
f.close()
closes file so you don't get errors elsewhere in your operating system.
f.write()
writes to the file
cursor is at the end of the document
with
with keyword is like define? but does not define a method?
Comparison operatiors
Same as javascript except with out ===
== is the same as js ===
Logical operatiors
and keyword instead of &&
or keyword instead of ||
not keyword negates the comparison operator
(not 1 == 1) is equal to (1 != 1)
Control flow
if, Elif and Else
if some_condition:
# execute some code
elif some_other_condition:
# execute some code
else:
# do something else
For loops are similar to map function in js and forEach in Java
for var in List:
# do something
Common to use _ for forloop if don't plan on using the variable within the loop.
for _ in myL:
print("Cool!")
can iterate over tuples as well.
Tuple unpacking - duplicate the structure in the list and you do something with them.
Can iterate thru a dictionary as well.
obj = {'k1': 1, 'k2': 2, 'k3': 3}
for val in obj.values():
print(val)
for k in obj.keys():
print(k)
While-loops have else statement like if-else (elif)
while some_boolean_condition:
# do something
else:
# do something else
Kernel > interrupt / restart
If jupyter notebook gets into endless loop
break - breaks out of the current closest enclosing loop
similar to break to javascript
exits the loop
continue - goes to the top of the closest enclosing loop
similar to continue in javascript
skips the iteration and continues the loop
pass - does nothing at all
pass is used for prevent eof (end of file) errors.
Can write a for loop and not add any code to execute (to come back later)
Python has a ternary operator
a = 25
b = 'a < 5' if a < 5 else 'a >= 5'
Special Operatorss
range([start], [stop], [step-size])
used in a for loop prints the range of from start to stop.
Can change the increment value with step size. (can go up by 2, 3 or 4) default is 1.
for num in range(0,11,2):
print(num)
list(range(0,11,2)) # casting numbers to a list.
Range is a generator
Generator is a special type of function that will generate information instead of save it all to memory
enumerate(str)
Return index count along with value when used in for loop in a tuple
list(enumerate(str)) # casting tuples to a list
zip(list1, list2)
combines to lists for iteration
if you run zip a lone, it returns the generated zip's memory address
returns list a list of tuples
will only display as far back as the list that is the shortest
list 1 = 5 items
list 2 = 3 items
zip will print out 3 items.
list(zip(list1, list2)) # can be casted to a list
in [keyword operator]
similar to indexOf in js. Except that it returns a boolean like .contains in javascript
used a lot when working with strings
min(list)
shows minimum value in a list
max(list)
shows maximum value in a list
shuffle(list)
from random [package] import shuffle
randomizes items in a list.
does not return anything
randint(min, max)
returns a random number with min and max.
similar to javascript Math.Random()
input('str')
Accepts input from user
accepts everything as a string.
Can be wrapped for casting.
List Comprehensions
[expression for item in list]
https://www.programiz.com/python-programming/list-comprehension
[ expression for item in list if conditional ]
List comprehension with if statement
[s for s in stores if s["name"] == name]
https://stackoverflow.com/questions/4406389/if-else-in-a-list-comprehension
flattened for loops
celcius = [0,10,20,34.5]
fahrenheit = [((9/5) * temp + 32) for temp in celcius]
append is implied in flattened for loops
if else statements are reverse order
results = [x if x % 2 == 0 else 'ODD' for x in range(0,11)]
can do nested loops
results = [x*y for x in [2,4,6,] for y in [1,10,1000]]
Always try to do readability first.
Methods
def name_of_function():
print("some code")
How to document methods
'''
DOCSTRING: Information about the function
INPUT: no input...
OUTPUT: Hello
'''
def func(a: int, b: int): - Helps the dev know what datatype to use, but does not stop you from using other datatypes
func(2,3) # 6
func("cookies", 2) # cookies cookies
positional arguments - the name given to arguments of a method where the first item will be first variable in method. Second argument is the second variable etc. Based off of position they are in the parameters of the method.
def func(a,b,c=0,d=0,e=0):
*args - arguments
Allows you take a arbitrary number of arguments
Arguments will be treated as a tuple inside of the function
def func(*args):
**kwargs - keyword arguments
arbitrary number of keyword arguments
treats it as a dictionary as a input.
def func(**kwargs):
func(a=1, b=2):
args and kwargs can be used in combination
Useful to be used outside of arguments
func(*args, **kwargs):
func(1,2,3,4,5,6,a=1,b=2,c=3)
Lambda Expressions - Anonymous function - Map and Filter
Way to create anonymous functions that are used only once.
map - map(func, iter) - used to replace the for loop, with less code. Allows you to run a method on each item in the iter.
can cast into list, by wrapping list() around it.
filter - returns a iterable when the items passed in the function are True.
reduce -
no longer built-in function
demoted to functtools module
"Use functools.reduce() if you really need it; however 99% of the time an explicit for loop is more readable."
Guido Van Rossum, Python Creator
lambda - keyword creates anonymous function.
lambda num: num ** 2
can write python in one line, but not standard in styling (but looks cool)
Should only use it when you can easily read it if you were to come back to your code later.
Nested Statements and Scope
create a variable in python, the variable is stored in a namespace.
namespace also has a scope (similar to javascript)
LEGB - Local Enclosing, Global (module), Built-in
Order that python is going to look for variables in
global - keyword used within another scope can change the global variable value
Not recommended.
OOP
__init__ - constructor
__str__ - equal to toString method
__len__ - equal to .length() method
__del__ - deletes the object
method - is a function that inside of a class that will work with the object in some Way
method overloading - is done by setting default value of none.
Inheritance
child classes are called derived classes.
Polymorphism
Won't really use it much in Python
Abstraction
Class thats not expected to be instantiated on, designed to be a base class.
Encapsulation
Defining variables with dunder, will make them private
__privateVar
Create own modules in packages in Python
Modules - is .py script to use in another .py
__name__ - gets assigned a name depending on how you are running the script.
this var gets assigned in the background
if __name__ == "__main__" - if true, it means you are running the .py file directly.
__main__ - name of the file being ran. Implicitly set by python
Errors and Exception handling
try - this will try and run your code
except - will do something if it catches an error
else - else block will run if there are no errors. If there is an error it will not run.
(means theres is no exception)
finally - will run regardless of an error.
wise to add function calls here.
raise - errors are not 'called' in python they are 'raised'
different types of errors and exceptions
syntax errors - errors with syntax
exceptions - error when attempt to execute. Syntax is correct
https://docs.python.org/3/library/exceptions.html#bltin-exceptions
List of python built in exceptions
Unit Testing
pylint - library that looks at your code and reports possible issues
mainly for working with other people or an organization
Styling rules are known as PEP8
https://www.python.org/dev/peps/pep-0008/
python3 disallowed the use of tabs and spaces for indentation
use spaces instead of tabs
pylint not recognized in w10
add address to path variable in w10
https://www.youtube.com/watch?v=i4yJi-cpwWk
unittest - built-in library will allow you to test your own programs and check you are getting desired outputs
You wil create a test class.
Best to go from simple to complex.
Python Decorators
Docorators = java annotations
Decorate a function - on/off switch to add new code (functionality) to a function.
allow you to tack on extra functionality to an already exisiting function
use @ operators placed on top of the original function
annotations like in Java ?
functions are objects that can be passed into other objects
can define function inside of a function
@annotation - the annotation has to be the name of a function that takes a function as a argument
really common in web frameworks like django or flask
decorates the function with some extra code.
Generators
Allows you to write a function that can send a value back and then later resume to pick up where it left off
Generate a sequence of values overtime.
example would be the range function
keeps track of the last number it returned and the step size.
Does not create a giant list in memory.
yield - keyword that returns a value instead of storing it in memory.
allows you to iterate thru loops
create own generators using yield
next - What for-loops use to call the next value
iterator - (iter()) allows you to interate thru objects
string objects support iter, but cannot be interated over with next() function
Have to convert objects that are iterable to iter
generator expressions - are faster than list comprehension
generator comprehension - https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Generators_and_Comprehensions.html
Collections
countainer dicts
counter - dictionary sub class.
Counts the number of times a element shows up in a list.
(list or iterable)
returns a dictionary.
.most_common(#)
shows the most common values in that list.
replacing # with the number will show you the top # elements in that list
sum(c.values()) # total of all counts
c.clear() # reset all counts
list(c) # list unique elements
set(c) # convert to a set
dict(c) # convert to a regular dictionary
c.items() # convert to a list of (elem, cnt) pairs
Counter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairs
c.most_common()[:-n-1:-1] # n least common elements
c += Counter() # remove zero and negative counts
defaultdict - all methods provided by dicionary but takes first argument (default_factory) as default datatype for the dictionary.
faster than using dict.set_default method
Will never raise a KeyError
any key that does not exist gets the value returned by the default factory
orderedDict - remembers the order in which its contents are added
normal dictionary is a mapping, does not retain order
namedtuple - assigns names to numerical index in the tuple
like normal tuple, and assigns object type
namedtuple('name of class', 'param1 param2 param3')
sub class of tuple
Dateime
builtin module to work with date and time.
manages timezone information
datetime.time()
https://docs.python.org/2/library/datetime.html#time-objects
tzinfo - Timezone information
datetime.date.today().timetuple()
returns a tuple of information in that time instance
doing arithmetic with time will return the timedelta
pdb
interactive debugger for python programs
pythons built in debugger
not taught to beginners at all
(Pdb) q
will quit the debugger
https://docs.python.org/3/library/pdb.html
timeit
Times your code execution
timeit.time( # expression as a string, number of times you want to run it)
returns the time in seconds.
significant time difference using map
https://docs.python.org/2/library/timeit.html
StringIO
implements an in-memory file like object.
can be used as input or output to most functions that would expect a standard file object
usually used for scraping from the web.
Can convert strings and read and write to them as if they were files
https://docs.python.org/2/library/stringio.html
cStringIO - faster implementation of stringio
Regular Expressions
https://www.w3schools.com/python/python_regex.asp
https://docs.python.org/3/library/re.html
Similar in syntax to pearl
import re
re.search("")
returns a match object
if no pattern is returned, None is returned.
.start()
shows index of when the match begins
.ends()
shows index of when the match ends.
how to split domain names from a email address?
common interview question
re.split()
re.split([term], [phrase])
splits a phrase and returns a list by the term.
re.findall([term], [phrase])
returns a list of all the matches
meta-characters
r'' - designates the pattern
repetition sets
character sets
exclusion
character ranges
Escape codes
People usually refer to documentation when working with regex
completepython3bootcamp-files / 13-advancedPythonModules / 05-regular expressions