Tuples, lists, dictionaries

And the data tuples in the list by extracting the offset, the data dictionary is a dictionary of the keys extracted.

 

Tuple: Only query elements, does not support add, modify and delete elements.

= tuple1 ( ' A ' , ' B ' )
 # extract elements from tuple 
Print (tuple1 [0])
 Print (tuple1 [:])

# Output: 
A
('A', 'B')

 

. 1 TUP1 = (12, 34.56 )
 2 tup2 = ( ' ABC ' , ' XYZ ' )
 . 3  
. 4  # Create a new tuple 
. 5 tup3 = TUP1 + tup2
 . 6  Print (tup3)
 . 7  # Output: (12, 34.56, ' ABC ',' XYZ ') 
. 8  
. 9  # deleted tuples 
10 TUP = ( ' Physics ' , ' Chemistry ' , 1997, 2000 )
 . 11  Print (TUP)
 12 is  # output :( 'physics',' chemistry ' , 1997, 2000 )
13 
14 del tup
15 print (tup)
16 #输出:
17 Traceback (most recent call last):
18   File "d:/py case/1.py", line 14, in <module>
19     print (tup)
20 NameError: name 'tup' is not defined

tuple () method

Syntax: tuple ( SEQ ), the parameters: SEQ - be converted into a sequence of tuples, the return value: tuples.
. 1  # tuple () method: sequence into a tuple, tuple () function is not to change the type of the value, but the type of the return value changes, the original value is not changed 
2  # will be converted into a tuple list 
. 3 A = [1,2,3,4 ]
 . 4 A1 = tuple (a)
 . 5  Print (a)
 . 6  Print (A1)
 . 7  
. 8  # tuple for the dictionary of the dictionary will return key composition 
. 9 B = {. 1: 2,3:. 4 }
 10 B1 = tuple (B)   
 . 11  Print (B)
 12 is  Print (B1) 
 13 is  
14  # tuple of tuples returned their 
15 C = (1,2,3,4 )
 16 C1 = tuple (C) 
 . 17  Print(C)
 18 is  Print (C1)  
 . 19  
20 is  output:
 21 [1, 2, 3, 4 ]
 22 (1, 2, 3, 4 )
 23 {1: 2, 3: 4 }
 24 (1, 3 )
 25 ( 1, 2, 3, 4 )
 26 (1, 2, 3, 4)

 

List: positioning element with an offset, the offset from zero, the slice listing rules: Take the left and right does not take

You can only append append an element. del statement is very convenient, both to delete an element, can also delete multiple element once (similar to the principles and slice, take the left and right do not take)

. 1 A = [1,2,4,5,7, ' ABC ' ]
 2  
. 3  # extract a single item from the list 
. 4  Print (A [0])
 . 5 Output: 1
 . 6  
. 7  # extracting a plurality of elements from a list 
. 8  Print ( A [: 2 ])
 . 9 output: [. 1, 2 ]
 10  
. 11  Print (A [. 1: 4 ])
 12 is output: [2, 4, 5 ]
 13 is  
14  # add elements to the list 
15 a.append (. 8 )
 16  Print (A)
 . 17 output: [. 1, 2,. 4,. 5,. 7, ' ABC ' ,. 8]
 18 is  
. 19  # delete a list element 
20 is  del A [:]
 21 is  Print (A)
 22 is output: []

 

 

 Specific usage View link: https://www.runoob.com/python/python-lists.html

 

len () function: draw a tuple, list or dictionary length (number of elements), put in parentheses tuple list or dictionary name

1 students = ['小明','小红','小刚']
2 scores = {'小明':95,'小红':90,'小刚':90}
3 print(len(students))
4 #输出:3
5 print(len(scores))
6 #输出:3

字典:由键值对组成,字典中的键具备唯一性,而值可重复。

scores = {'小明':95,'小红':90,'小刚':90}
#从字典中提取元素
print(scores['小明'])
#输出:95

#给字典增加/删除元素
scores['小青']=95
print(scores)
#输出:{'小明': 95, '小红': 90, '小刚': 90, '小青': 95}

del scores['小明']
print(scores)
#输出:{'小红': 90, '小刚': 90, '小青': 95}

 字典内置函数&方法查看:https://www.runoob.com/python/python-dictionary.html

Guess you like

Origin www.cnblogs.com/String-song/p/11928081.html