python from scratch - 07 lists, tuples and dictionaries

List (list): is an ordered set, the difference between it and a tuple is that a list can add and delete elements at any time, while a tuple cannot.

name_list = ['Peter', 'John', 'Mick','Philip']
print(name_list)
name_list.append('zhangSan')
print(name_list)
name_list.insert(1, 'Bill')
print(name_list)
C:\testPro\venv\Scripts\python.exe C:/testPro/testPac/testpyfile.py
['Peter', 'John', 'Mick', 'Philip']
['Peter', 'John', 'Mick', 'Philip', 'zhangSan']
['Peter', 'Bill', 'John', 'Mick', 'Philip', 'zhangSan']

Tuple: () is used for tuple, and [] is used for list. The variables in tuple cannot be assigned twice, they are read-only, and can be understood as a set of constants. So Tuple[3] = 'ttt' will report an error

tuple = ('runoob', 786, 2.23, 'john', 70.2)
tinytuple = (123, 'john')

print(tuple) # print the full tuple
print(tuple[0]) # print the first element of the tuple
print(tuple[1:3]) # print the second to third elements
print(tuple[2:]) # prints all elements from the third to the end of the list
print(tinytuple * 2) # print the tuple twice
print(tuple + tinytuple) # print the combined tuple
('runoob', 786, 2.23, 'john', 70.2)
runoob
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('runoob', 786, 2.23, 'john', 70.2, 123, 'john')

Dictionaries, no different from other languages:

dict = {}
dict['a'] = 1000
dict['b'] = 2000
print(dict)
student_age_dict = {"John":23, "Bill":22, "Micky":23}
print(student_age_dict)
print(student_age_dict.keys())
print(student_age_dict.values())
{'b': 2000, 'a': 1000}
{'Bill': 22, 'Micky': 23, 'John': 23}
dict_keys(['Bill', 'Micky', 'John'])
dict_values([22, 23, 23])

The access to list and Tuple generally uses index, but if the access is out of range, IndexError will be thrown. In order to avoid out-of-range access, generally use len(aList) to read its length first.

aList = [45, 45, 32, 37, 56]
i = 0
while i < len(aList):
    print(aList[i])
    i += 1
Accessing with a negative integer index, such as aList[-1], accesses the last value of aList, aList[-2], accesses the second-to-last value, and so on

Supplement 1:

      a) Regarding the List, it can be deleted with the pop method

      b) Regarding List and Tuple, you can use sub-List or Tuple as its elements, such as

aList = [45, 45, 32, 37, 56, [99, 98, 97]]
print(aList[-1][0])
C:\testPro\venv\Scripts\python.exe C:/testPro/testPac/testpyfile.py
99


Supplement 2: 

     a) You can use the get method to get the value corresponding to the key, or you can use the [key] subscript to get it

   b) delete key-value pair with pop

     c) Dictionary is used where high-speed query is required. Although some memory is wasted, its search and insertion speed will not slow down with the increase of Key.

student_age_dict = {"John":23, "Bill":22, "Micky":23}
print(student_age_dict.get('John'))

if "John" in student_age_dict:
    print(student_age_dict["John"])

student_age_dict["Nero"] = 21
print(student_age_dict)
student_age_dict.pop('Nero')
print(student_age_dict)
23
23
{'Nero': 21, 'John': 23, 'Micky': 23, 'Bill': 22}
{'John': 23, 'Micky': 23, 'Bill': 22}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325749465&siteId=291194637