Catalog
One 、 list
1) The concept of list
2) List creation
3) Access to the list
4) Add elements to the list
5) List delete elements
6) List modification elements
7) List * and + operation
Two 、 Tuples
1) The concept of tuples
2) Tuple creation
3) Tuple access
4) Addition, deletion and modification of tuples
5) Of a tuple * and + operation
3、 ... and 、 Dictionaries
1) The concept of a dictionary
2) Dictionary creation
3) Access to dictionaries
4) Dictionary add key
5) Dictionary delete key
6) Dictionary modifier
The list is Python Built in Ordered variable sequence , All elements of the list are placed in a pair of brackets “[ ]” in , And use commas to separate .
When list elements are added or deleted , List objects Automatically expand or shrink memory , Make sure there's no gap between the elements .
One The data types in the list can be different , Can be integers at the same time 、 The set of real Numbers 、 String and other basic types , Even lists 、 Tuples 、 Dictionaries 、 Collection and other custom types of objects .
List1 = [1, 2, 3, 4, 5]
List2 = ["a", "b", "c", "d", "e"]
List3 = [1.1, 2.2, 3.3, 4.4, 5.5]
List4 = [1, "a", 1.1]
example 1.1
List1 = [1, 2, 3, 4, 5] for i in range(len(List1)): print(List1[i], end='\t')
Output
example 1.2
List1 = [1, 2, 3, 4, 5] for item in List1: print(item, end='\t')
Output
example 1.3
List1 = [1, 2, 3, 4, 5] List1.append(6) List1.append(7) for item in List1: print(item, end='\t')
Output
example 1.4
List1 = [1, 2, 3, 4, 5] List1.insert(0, "a") # The index location is 0 Where to add "a" List1.insert(len(List1) - 1, "b") # The index location is 6 - 1 = 5 Add "b" for item in List1: print(item, end='\t')
Output
example 1.5
List1 = [1, 2, 3, 4, 5] List1.pop(0) # Delete first element List1.pop(len(List1) - 1) # Delete the last element for item in List1: print(item, end='\t')
Output
example 1.6
List1 = [1, 2, 3, 4, 5] List1.remove(2) # Remove elements 2 List1.remove(4) # Remove elements 4 for item in List1: print(item, end='\t')
Output
example 1.7
List1 = [1, 2, 3, 4, 5] List1[0] = "a" # The first element is changed to a List1[len(List1) - 1] = "b" # The last element is changed to b for item in List1: print(item, end='\t')
Output
example 1.8
List1 = [1] List2 = [2, 3, 4, 5] List1 = List1 * 5 for item in List1: print(item, end='\t') print() List3 = List1 + List2 for item in List3: print(item, end='\t')
Output
Tuples are similar to lists , But it belongs to Immutable sequence , Once the tuple is created , Its elements cannot be modified in any way .
Tuples are defined in the same way as lists , But when defining All elements are placed in a pair of parentheses “()” in , Not square brackets .
tuple1 = (1,)
tuple2 = ("a", "b", "c", "d", "e")
tuple3 = (1.1, 2.2, 3.3, 4.4, 5.5)
tuple4 = (1, "a", 1.1)
example 2.1
tuple1 = (1) tuple2 = ("a") tuple3 = (1,) print(type(tuple1), type(tuple2), type(tuple3), sep=" . ") # With . For division Printout
Output
example 2.2
tuple1 = (1, 2, 3, 4, 5) for i in range(len(tuple1)): print(tuple1[i], end='\t')
Output
example 2.3
tuple1 = (1, 2, 3, 4, 5) for item in tuple1: print(item, end='\t')
Output
Tuples belong to immutable sequences , It can't be modified !!!
example 2.4
tuple1 = (1,) tuple2 = ("a", "b", "c", "d", "e") tuple1 = tuple1 * 3 for item in tuple1: print(item, end='\t') print() tuple3 = tuple1 + tuple2 for item in tuple3: print(item, end='\t')
Output
The dictionary is Out of order variable sequence , By key 、 The value of , The key must be unique , But values don't have to be .
When defining a dictionary , Of each element Keys and values are separated by colons , The elements are separated by commas , All the elements are in a pair of braces “{}” in .
In the dictionary The key can be any immutable data , For example, integer. 、 The set of real Numbers 、 The plural 、 character string 、 Tuples, etc. .
dict1 = {"name": "admin", "pwd": "123456"}
dict2 = {1: "a", 2: "b"}
dict3 = {1.1: 1, 1.2: 2}
example 3.1
dict1 = {"name": "admin", "pwd": "123456", "hobby": ["play", "singing", "dancing"]} print(dict1["name"]) print(dict1["hobby"])
Output
example 3.2
dict1 = {"name": "admin", "pwd": "123456"} print(dict1["hobby"])
Output
example 3.3
dict1 = {"name": "admin", "pwd": "123456"} res = dict1.get("name", " There's no such key ") print(res) # With this key , Print the corresponding value normally res = dict1.get("hobby", " There's no such key ") print(res) # There's no such key , Print There's no such key
Output
example 3.4
dict1 = {"name": "admin", "pwd": "123456"} print(dict1) dict1["hobby"] = ["play", "singing"] print(dict1)
Output
example 3.5
dict1 = {"name": "admin", "pwd": "123456"} print(dict1) del dict1["pwd"] print(dict1)
Output
example 3.6
dict1 = {"name": "admin", "pwd": "123456"} print(dict1) dict1["pwd"] = "654321" print(dict1)
Output
The end ……
Originality is not easy. , Reprint please indicate the source .
If it's helpful to you, you can press one key three times , It will be updated continuously ( Hee hee ).