/
important_notes.txt
59 lines (52 loc) · 3.14 KB
/
important_notes.txt
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
Summary:
This will be an ongoing growing notes picked from the internet along with my experince,
serving the purpose of writing better Python code.
Notes about:
1 - Time Complexity from good to worst(top-to-down). >>> line 11
2 - len(list) is O(1). >>> line 18
3 - List VS Tuple efficiency. >>> line 22
4 - Duplicated keys:values pairs in dictionary. >>> line 37
5 - Dictinary .items(), .keys(), and .values() returns a view object. >>> line 52
NOTE no.1 Time Complexity from good to worst(top-to-down)
* O(1) Constant time complexity.
* O(log n) Logarithmic time complexity.
* O(n) Linear time complexity.
* O(n log n) Linearithmic time complexity.
* O(n ** 2) Quadratic time complexity.
NOTE no.2 len(list) is O(1)
* List built on the array data structure
* Array length = (Array_head memory allocation) - (Array_tail memory allocation)
NOTE no.3 List VS Tuple efficiency.
* Tuple is slightly faster to access than list.
* Tuples are immutable data types which are stored in a single block of memory
so it doesn’t require extra space to store new objects.
* But list are mutable data types and are allocated in two blocks where the
fixed one with all object information and a variable sized block for the data.
* There is slight difference in indexing speed of list and tuple because tuples
uses fewer pointers when indexing comparing to list. Because of fewer pointers,
access mechanism is generally faster in tuples than lists.
* Because that Tuple is an immutable data type, unpacking must be equal to the
variables, otherwise ERROR will occur.
>>>> t = (1, 2, 3, 4)
>>>> s1, s2, s3, s4 = t
While it's OK with list.
NOTE no.4 Duplicated keys and values in dictionary.
* Create Dictionary with duplicated keys, will neglect first pairs and keep the last.
>>>> {2:'a', 3:'b', 2: 'c'} returns {2: 'c', 3: 'b'}
* Python dictionaries built-on the hash tables data structure.
* Hash Table use the Set abstract data structure to register its keys to the array.
* So we can't have repeated key but it's fine for the repeated values.
>>>> {'a': 10, 'b': 20, 'c': 30} returns {'a': 10, 'b': 20, 'c': 30}
* Since Set has no order, you can't have same result with method like
.popitem() with that being said, the next note has been mentioned on
RealPython website "In Python versions less than 3.6, popitem() would return an arbitrary
(random) key-value pair since Python dictionaries were unordered before version 3.6"
Link: https://realpython.com/python-dicts/#built-in-dictionary-methods.
NOTE no.5 Dictionary .items(), .keys(), and .values() returns a view object.
* The .items(), .keys(), and .values() methods actually return something
called a view object. A dictionary view object is more or less like a window
on the keys and values. For practical purposes, you can think of these
methods as returning lists of the dictionary’s keys and values.
* With the previous note being said, view object is not actually a Python list
means we can't index it.
* Window is just a way of peek at the Key:Value pairs of the dictionary.