@@ -84,10 +84,31 @@ Information described here, can be integrated with the following list:
8484inf
8585```
8686
87- ### Bit manipulation
87+ ## ` range ` and ` enumerate `
8888
8989``` python
90- TODO
90+ # `range`
91+ >> > list (range (3 )) # Equivalent to `range(0, 3)`
92+ [0 , 1 , 2 ]
93+ >> > list (range (1 , 10 , 2 ))
94+ [1 , 3 , 5 , 7 , 9 ]
95+ >> > for i in range (3 ): print (i)
96+ ...
97+ 0
98+ 1
99+ 2
100+
101+ # `enumerate`
102+ >> > for i, v in enumerate (range (3 )): print (i, v)
103+ ...
104+ 0 0
105+ 1 1
106+ 2 2
107+ >> > for i, v in enumerate (range (3 ), start = 10 ): print (i, v)
108+ ...
109+ 10 0
110+ 11 1
111+ 12 2
91112```
92113
93114## Tuples
@@ -130,6 +151,12 @@ TODO
130151>> > l[- 1 ] # Last element of the list (equivalent to `l[len(l) - 1]`)
131152' a'
132153
154+ # Slicing
155+ >> > l[:] # `l[start:end]` where `end` is exclusive
156+ [1 , 2 , ' a' ]
157+ >> > l[0 :len (l)] # `start` is 0 and `end` is `len(l)` if omitted
158+ [1 , 2 , ' a' ]
159+
133160# Some useful methods
134161>> > l.append(' b' ) # `O(1)`
135162>> > l.pop() # `O(1)` just for the last element
@@ -164,7 +191,11 @@ True
164191True
165192
166193# Built-in methods
167- >> > l = [1 , 2 , 3 , 4 ]
194+ >> > l = [2 , 1 , 4 , 3 ]
195+ >> > min (l)
196+ 1
197+ >> > max (l)
198+ 4
168199>> > sum (l)
16920010
170201>> > any (v == 4 for v in l)
@@ -214,13 +245,15 @@ True
21424513
215246
216247>> > s[0 ] = ' h' # Strings are immutable. So you will get: `TypeError: 'str' object does not support item assignment`
248+ >> > s += ' Another string' # A new string will be created, so concatenation is quite slow
217249
218- >> > ls = list (s)
219- >> > ls
220- [' H' , ' e' , ' l' , ' l' , ' o' , ' ,' , ' ' , ' w' , ' o' , ' r' , ' l' , ' d' , ' !' ]
221- >> > ls[0 ] = ' h'
222- >> > ' ' .join(ls)
223- ' hello, world!'
250+ >> > s = ' Hello'
251+ >> > l = list (s)
252+ >> > l
253+ [' H' , ' e' , ' l' , ' l' , ' o' ]
254+ >> > l[0 ] = ' h'
255+ >> > ' ' .join(l)
256+ ' hello'
224257
225258>> > ' Hello' in s
226259True
233266## Stacks
234267
235268``` python
236- >> > stack = []
269+ >> > stack = [] # We can use a list to simulate a stack
237270
238271>> > stack.append(0 ) # `O(1)`
239272>> > stack.append(1 )
309342>> > s.add(2 )
310343>> > s
311344{1 , 2 }
345+ >> > len (s)
346+ 2
312347>> > s.add(1 ) # Duplicate elements are not allowed
313348>> > s
314349{1 , 2 }
@@ -317,6 +352,10 @@ True
317352{1 , 2 , ' a' }
318353>> > 1 in s # `O(1)`
319354True
355+ >> > s.remove(1 )
356+ >> > s
357+ {2 , ' a' }
358+ >> > s.remove(1 ) # KeyError: 1
320359
321360>> > s0 = {1 , 2 , ' a' }
322361>> > s0
@@ -325,16 +364,6 @@ True
325364>> > s1
326365{1 , 2 , ' a' }
327366
328- >> > len (s)
329- 3
330- >> > ' a' in s
331- True
332-
333- >> > s.remove(1 )
334- >> > s
335- {2 , ' a' }
336- >> > s.remove(1 ) # KeyError: 1
337-
338367>> > s0 = {1 , 2 }
339368>> > s1 = {1 , 3 }
340369>> > s0 | s1
@@ -360,14 +389,12 @@ True
360389## Hash Tables
361390
362391``` python
363- >> > d = {' a' : ' hello, world' , b : 11 }
392+ >> > d = {' a' : ' hello, world' , ' b ' : 11 } # Equivalent to `dict(a='hello, world', b=11)`
364393>> > type (d)
365394< class ' dict' >
366395>> > d
367396{' a' : ' hello, world' , ' b' : 11 }
368397
369- >> > d = dict (a = ' hello, world' , b = 11 )
370-
371398>> > d.keys()
372399dict_keys([' a' , ' b' ])
373400>> > d.values()
0 commit comments