@@ -68,7 +68,7 @@ Information described here, can be integrated with the following list:
6868>> > 2 ** 3 # Equivalent to `pow(2, 3)`
69698
7070
71- # Math functions from the proper package
71+ # Math functions from the `math` package
7272>> > import math
7373>> > math.ceil(7.2 )
74748
@@ -103,23 +103,24 @@ TODO
103103
104104>> > t[0 ] = 10 # Tuples are immutable: `TypeError: 'tuple' object does not support item assignment`
105105
106- >> > a, b, c = t
106+ >> > a, b, c = t # Unpacking
107107>> > a
1081081
109109>> > b
1101102
111111>> > c
112112' str'
113- >> > a, _, _ = t # Get first element and ignore the other two values
113+ >> > a, _, _ = t # Unpacking: ignore second and third elements
114114>> > a
1151151
116116```
117117
118118## Lists
119119
120- More info about time complexity for lists can be found [ here ] [ python-time-complexity ] .
120+ ` Python ` uses ` Timsort ` algorithm in ` sort ` and ` sorted ` ( < https://en.wikipedia.org/wiki/Timsort > ) .
121121
122122``` python
123+ # Define a list
123124>> > l = [1 , 2 , ' a' ]
124125>> > type (l) # <class 'list'>
125126>> > len (l)
@@ -129,6 +130,7 @@ More info about time complexity for lists can be found [here][python-time-comple
129130>> > l[- 1 ] # Last element of the list (equivalent to `l[len(l) - 1]`)
130131' a'
131132
133+ # Some useful methods
132134>> > l.append(' b' ) # `O(1)`
133135>> > l.pop() # `O(1)` just for the last element
134136' b'
@@ -144,12 +146,35 @@ More info about time complexity for lists can be found [here][python-time-comple
1441460
145147>> > l.index(12 ) # ValueError: 12 is not in list
146148
149+ # More compact way to define a list
147150>> > l = [0 ] * 5
148151>> > l
149152[0 , 0 , 0 , 0 , 0 ]
150153>> > len (l)
1511545
155+ >> > [k for k in range (5 )]
156+ [0 , 1 , 2 , 3 , 4 ]
152157
158+ # Compact way to define 2D arrays
159+ >> > rows, cols = 2 , 3
160+ >> > m = [[0 ] * cols for _ in range (rows)]
161+ >> > len (m) == rows
162+ True
163+ >> > all (len (m[k]) == cols for k in range (rows))
164+ True
165+
166+ # Built-in methods
167+ >> > l = [1 , 2 , 3 , 4 ]
168+ >> > sum (l)
169+ 10
170+ >> > any (v == 4 for v in l)
171+ True
172+ >> > any (v == 5 for v in l)
173+ False
174+ >> > all (v > 0 for v in l)
175+ True
176+
177+ # Sort list in-place (`sort`)
153178>> > l = [10 , 2 , 0 , 1 ]
154179>> > l
155180[10 , 2 , 0 , 1 ]
@@ -160,12 +185,14 @@ More info about time complexity for lists can be found [here][python-time-comple
160185>> > l
161186[10 , 2 , 1 , 0 ]
162187
188+ # Sort a list a return a new one (`sorted`)
163189>> > l = [10 , 2 , 0 , 1 ]
164190>> > sorted (l) # It returns a new list
165191[0 , 1 , 2 , 10 ]
166192>> > l
167193[10 , 2 , 0 , 1 ]
168194
195+ # Sort by a different key
169196>> > students = [
170197... (' Mark' , 21 ),
171198... (' Luke' , 20 ),
@@ -176,13 +203,6 @@ More info about time complexity for lists can be found [here][python-time-comple
176203>> > students.sort(key = lambda s : s[1 ])
177204>> > students
178205[(' Anna' , 18 ), (' Luke' , 20 ), (' Mark' , 21 )]
179-
180- >> > rows, cols = 2 , 3
181- >> > m = [[0 ] * cols for _ in range (rows)]
182- >> > len (m) == rows
183- True
184- >> > all (len (m[k]) == cols for k in range (rows))
185- True
186206```
187207
188208## Strings
215235``` python
216236>> > stack = []
217237
218- >> > stack.append(0 )
238+ >> > stack.append(0 ) # `O(1)`
219239>> > stack.append(1 )
220240>> > stack.append(2 )
221241
227247>> > stack[- 1 ] # Top of the stack
2282482
229249
230- >> > stack.pop()
250+ >> > stack.pop() # `O(1)`
2312512
232252>> > stack.pop()
2332531
250270>> > queue = deque()
251271
252272# Enqueue -> append()
253- >> > queue.append(0 )
273+ >> > queue.append(0 ) # `O(1)`
254274>> > queue.append(1 )
255275>> > queue.append(2 )
256276
2632832
264284
265285# Dequeue -> popleft()
266- >> > queue.popleft()
286+ >> > queue.popleft() # `O(1)`
2672870
268288>> > queue.popleft()
2692891
295315>> > s.add(' a' ) # We can mix types
296316>> > s
297317{1 , 2 , ' a' }
318+ >> > 1 in s # `O(1)`
319+ True
298320
299321>> > s0 = {1 , 2 , ' a' }
300322>> > s0
@@ -356,7 +378,7 @@ dict_values(['hello, world', 11])
356378a hello, world
357379b 11
358380
359- >> > ' a' in d
381+ >> > ' a' in d # `O(1)`
360382True
361383>> > 1 in d
362384False
@@ -395,7 +417,7 @@ You can overcome this problem by applying one of the following strategies:
395417[1 , 2 , 3 ]
396418
397419>> > min_heap = []
398- >> > heapq.heappush(min_heap, 3 )
420+ >> > heapq.heappush(min_heap, 3 ) # `O(log n)`
399421>> > heapq.heappush(min_heap, 2 )
400422>> > heapq.heappush(min_heap, 1 )
401423
@@ -404,7 +426,7 @@ You can overcome this problem by applying one of the following strategies:
404426>> > len (min_heap)
405427>> > min_heap[0 ]
4064281
407- >> > heapq.heappop(min_heap)
429+ >> > heapq.heappop(min_heap) # `O(log n)`
4084301
409431>> > min_heap
410432[2 , 3 ]
@@ -416,11 +438,6 @@ You can overcome this problem by applying one of the following strategies:
416438>> > heapq.heappop(min_heap) # `IndexError: index out of range`
417439```
418440
419- ## Searching
420-
421- ``` python
422- ```
423-
424441### collections.namedtuple
425442
426443``` python
0 commit comments