Python basic training Question 2-tuples, dictionaries, and python Basic Training
1. determine the value in the tuples
>>> a = ( 1, 2, 3, 4, 10 )>>> 10 in aTrue>>> '10' in a False
2. Modify the values in the tuples. Because the tuples cannot be directly modified, You can first convert them into a list. After the list is modified, assign them to a new tuple object.
>>> a = ( 10, 20, 30, 40 )>>> l = list( a )>>> l[0] = 100>>> t = tuple( l )>>> t(100, 20, 30, 40)>>> id( a )139920488716168>>> id( t )139920488447152>>> type( a )<type 'tuple'>>>> type( t )<type 'tuple'>>>>
3. Add a value to the set, delete a value, and calculate the intersection and Union
>>> aset(['a', 'c', 'b', 'e', 'd', 'g', 'f'])>>> a.add( 'ghostwu' )>>> aset(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'ghostwu'])>>> a.remove( 'g' )>>> aset(['a', 'c', 'b', 'e', 'd', 'f', 'ghostwu'])>>> b = set( "abcdlmn" )>>> a & bset(['a', 'c', 'b', 'd'])>>> a | bset(['a', 'c', 'b', 'e', 'd', 'f', 'm', 'l', 'n', 'ghostwu'])>>>
4. Use a dictionary to implement a Student Score System, and then perform operations such as adding, modifying, deleting, and sorting.
>>> student = { 'ghostwu' : { 'name' : 'ghostwu', 'age' : 20, 'score' : { 'math' : 78, 'english' : 66, 'python' : 75 } } }>>> student{'ghostwu': {'age': 20, 'score': {'python': 75, 'math': 78, 'english': 66}, 'name': 'ghostwu'}}>>> student['tom'] = { 'name' : 'tom', 'age' : 21, 'score' : { 'math' : 60, 'english' : 80, 'python' : 90 } }>>> student{'ghostwu': {'age': 20, 'score': {'python': 75, 'math': 78, 'english': 66}, 'name': 'ghostwu'}, 'tom': {'age': 21, 'score': {'python': 90, 'math': 60, 'english': 80}, 'name': 'tom'}}>>> student['ghostwu']['score']['php'] = 90>>> student['tom']['score']['php'] = 50>>> student['ghostwu']['score']['math'] = 30>>> del student['ghostwu']['age']>>> score1 = student['ghostwu']['score'].values()>>> score1[75, 90, 30, 66]>>> score1.sort()>>> score1[30, 66, 75, 90]>>> student.pop( 'address', 'shenzhen' )'shenzhen'>>> student{'ghostwu': {'score': {'python': 75, 'php': 90, 'math': 30, 'english': 66}, 'name': 'ghostwu'}, 'tom': {'age': 21, 'score': {'python': 90, 'php': 50, 'math': 60, 'english': 80}, 'name': 'tom'}}>>>