@@ -205,13 +205,22 @@ float('5') + 5
205205# # 10.0
206206
207207list ((2 ,4 ,6 ))
208- # # [2,4,6]
208+ # # [2, 4, 6]
209+
210+ list ({' a' :1 ,' b' :2 })
211+ # # ['a', 'b']
212+
213+ list ({' a' :1 ,' b' :2 }.values())
214+ # # [1, 2]
209215
210216tuple ([1 ,3 ,5 ])
211217# # (1,3,5)
212218
213219set ([1 ,3 ,5 ,1 ,3 ,5 ])
214- # # {1,3,5}
220+ # # {1, 3, 5}
221+
222+ dict ([(' a' ,1 ),(' b' ,2 ),(' c' ,3 )])
223+ # # {'a': 1, 'b': 2, 'c': 3}
215224```
216225
217226-----
@@ -241,20 +250,27 @@ methods for strings:
241250 - ` str.lower() ` lowercase
242251 - ` str.upper() ` uppercase
243252 - ` str.find(x) ` find index of character x
244- - ` str.index(x) ` index of x character (similar to ` .find(x) ` if x is
253+ - ` str.index(x) ` index of character x (similar to ` .find(x) ` if x is
245254 in the string)
246255 - ` str.count(x) ` count how many times x repeated
247256 - ` str.replace(x,y) ` replace character x with y
248- - ` str.split('sep') ` split an string to a list of strings based on the
249- separator ` sep ` (can be empty ` '' ` )
250- - ` sep.join(list) ` join a list of strings to make an string with
251- separator ` sep ` (can be empty ` '' ` ) - opposite of ` .split() `
252- - ` str.center('chr', num) ` see an example in below
257+ - ` str.split(x) ` split an string to a list of strings based on the
258+ separator x (can be empty ` '' ` )
259+ - ` str.join(x) ` join list of strings or string x to make an string by
260+ a separator - opposite of ` .split() `
261+ - ` str.startswith(x) ` True if the string starts with x character
262+ - ` str.endswith(x) ` True if the string ends with x character
263+ - ` str.strip() ` removing whitespace from the beginning and ending
264+ - ` str.center('chr', num) ` see an example in the below
253265
254266For example:
255267
256268``` python
257269name = ' python'
270+
271+ name.startswith(' p' )
272+ # # True
273+
258274name.capitalize()
259275# # 'Python'
260276
@@ -290,16 +306,16 @@ Lists are **mutable**, and their elements are usually **homogeneous**
290306and are accessed by iterating over the list.
291307
292308``` python
293- list_ = [1 ,3 ,5 ,7 ]
294- list_ [0 ] = 100 # Lists are mutable
295- list_
309+ ls = [1 ,3 ,5 ,7 ]
310+ ls [0 ] = 100 # Lists are mutable
311+ ls
296312# # [100, 3, 5, 7]
297313
298314# Lists can hold any type of item
299315example = [1 ,True ,None ,[' word' ,123 ],' test' ,(0 ,1 ),{' name id' : 7 }]
300316
301317# Indexing
302- list_ [1 :3 ]
318+ ls [1 :3 ]
303319# # [3, 5]
304320
305321example[3 ][1 ]
@@ -310,12 +326,14 @@ Here are main lists methods:
310326
311327 - ` list.append(x) ` append x
312328 - ` list.extend(x) ` or ` += ` extend/add x
313- - ` list.insert(i,x) ` insert x in position i
329+ - ` list.insert(i,x) ` insert x to index i
314330 - ` list.remove(x) ` remove x
315- - ` list.pop(i) ` remove item at position i (similar to ` del(list[i]) ` )
331+ - ` list.pop(i) ` pop out and remove item at index i (similar to
332+ ` del(list[i]) ` )
333+ - ` list.pop() ` pop out and remove the last item
316334 - ` list.sort() ` sort
317335 - ` list.reverse() ` reverse the order
318- - ` list.count(x) ` count number of times x repeated
336+ - ` list.count(x) ` count number of times x is repeated
319337 - ` list.index(x) ` find index of item x
320338 - ` list.copy() ` copy list
321339 - ` list.clear() ` clear list
@@ -497,7 +515,7 @@ example['first key']
497515Here are some of dictionaries methods:
498516
499517 - ` dict.update() ` update/add items
500- - ` dict.popitem() ` remove an item
518+ - ` dict.popitem() ` remove the last item
501519 - ` dict.pop(k) ` remove item with key k
502520 - ` dict.keys() ` return keys
503521 - ` dict.values() ` return values
@@ -559,17 +577,18 @@ production rate of two products, id 23 and id 35, in years 2005 and
5595772010:
560578
561579``` python
562- exmp = [{' id' :23 ,' year' :2005 ,' production ' :2305 },{' id' :35 ,' year' :2005 ,' production ' :3505 },{' id' :23 ,' year' :2010 ,' production ' :2310 },{' id' :35 ,' year' :2010 ,' production ' :3510 }]
580+ production = [{' id' :23 ,' year' :2005 ,' rate ' :2305 },{' id' :35 ,' year' :2005 ,' rate ' :3505 },{' id' :23 ,' year' :2010 ,' rate ' :2310 },{' id' :35 ,' year' :2010 ,' rate ' :3510 }]
563581```
564582
565583We can make a dictionary of production rates for each ` id_year `
566584combination such that:
567585
568586``` python
569- dict_ = {}
570- for i in exmp:
571- dict_[' %s _%s ' % (i[' id' ],i[' year' ])] = i[' production' ]
572- dict_
587+ annual_rates = {}
588+ for i in production:
589+ annual_rates[' %s _%s ' % (i[' id' ],i[' year' ])] = i[' rate' ]
590+
591+ annual_rates
573592# # {'23_2005': 2305, '35_2005': 3505, '23_2010': 2310, '35_2010': 3510}
574593```
575594
@@ -579,19 +598,23 @@ of lists such that:
579598
580599``` python
581600import collections
582- dt_ = collections.defaultdict(list )
583- for i in exmp:
584- dt_[i[' id' ]].append(i[' production' ])
585- dict (dt_)
601+
602+ annual_rates = collections.defaultdict(list )
603+ for i in production:
604+ annual_rates[i[' id' ]].append(i[' rate' ])
605+
606+ dict (annual_rates)
586607# # {23: [2305, 2310], 35: [3505, 3510]}
587608```
588609
589610Now let’s find total production over years:
590611
591612``` python
592- for i in dt_:
593- dt_[i] = sum (dt_[i])
594- dict (dt_)
613+ annual_rates_total = {}
614+ for i in annual_rates:
615+ annual_rates_total[i] = sum (annual_rates[i])
616+
617+ annual_rates_total
595618# # {23: 4615, 35: 7015}
596619```
597620
@@ -600,8 +623,8 @@ finite stream of `(key, value)` tuples:
600623
601624``` python
602625L = [(' Italy' , ' Rome' ), (' France' , ' Paris' ), (' US' , ' Washington DC' )]
603- dict (iter (L) )
604- {' Italy' : ' Rome' , ' France' : ' Paris' , ' US' : ' Washington DC' }
626+ dict (L )
627+ # # {'Italy': 'Rome', 'France': 'Paris', 'US': 'Washington DC'}
605628```
606629
607630### Sets
@@ -633,8 +656,31 @@ for element in [[], (), {}]:
633656# # <class 'dict'>
634657```
635658
636- Sets have several methods including set operations such as ` union ` ,
637- ` intersection ` , and ` difference ` .
659+ Sets have several methods including set operations such as:
660+
661+ - ` set.add(x) ` : add a member to the set
662+ - ` set.update(x) ` : add a set/list to a set
663+ - ` set.remove(x) ` : remove a member of the set
664+ - ` set.pop() ` : pop out and remove the first member
665+ - ` set.union(x) ` : union of a set/list to a set
666+ - ` set.intersection(x) ` : intersection of a set/list to a set
667+ - ` set.difference(x) ` : difference of a set/list to a set
668+
669+ <!-- end list -->
670+
671+ ``` python
672+ s = {1 ,2 ,3 }
673+ t = {3 ,4 ,5 }
674+
675+ s.union(t)
676+ # # {1, 2, 3, 4, 5}
677+
678+ s.intersection(t)
679+ # # {3}
680+
681+ s.difference(t)
682+ # # {1,2}
683+ ```
638684
639685Iterating through sets:
640686
0 commit comments