Home >Backend Development >Python Tutorial >Introduction to Python tuples, lists, and dicts
Tuple:
Tuples are often represented by parentheses, that is: (), the element plus a comma is the identifier of the tuple.
1 #定义一个元组 2 3 #tuple = 'a', 4 5 tuple = ('a','b','c','d','e','f','g') 6 7 #常规来说,定义了一个元组之后就无法再添加或修改元组的元素,但对元组切片可以添加会修改元组的元素。 8 9 print tuple[1:5]10 11 tuple = tuple[:2]+('h')+temp[2:]12 13 print(tuple)14 15 #使用for循环进行遍历元组16 17 for each in tuple:18 19 print each20 21 #通过range()函数和for循环获取元组内元素的序号22 23 for index in range(len(tuple)):24 25 print tuple[index]
List:
Lists are often represented by square brackets, that is: [];
Create a list , just use square brackets to enclose different data items separated by commas.
For example:
1 list1 = ['a','b','c',1,3,5]2 list2 = [1,2,3,4,5,6]3 list3 = ["abc","bcd","cde"]
Traversing the list: (len(each): represents the length of each iteration variable , each: represents the variable of each iteration)
1 list1 = ['a','b','c',1,3,5]2 for each in list13 print(each,len(each))
Commonly used functions in lists:
cmp(list1,list2): Compare the elements of two lists
len(list): Return the number of list elements
max(list): Returns the maximum value of a list element
min(list): Returns the minimum value of a list element
list(tuple): Converts a tuple to a list
9 methods commonly used in lists:
List.append(obj): Add a new object at the end of the list
List.count(obj): Count a certain The number of times an element appears in the list
List.extend(list): Add another sequence containing multiple values at the end of the list, which has the effect of extending the list
List.insert(index ,obj): Insert the object before the index element in the list
List.pop(obj=list[-1]): Remove an element from the list by default (the last element by default), and return The value of the element
list.remove(obj): remove a value from the list
list.reverse(): reverse the elements in the list
list.sort(function()): Sort the list
Dictionary (dict)
The dictionary is enclosed by curly braces {} For its data, the curly braces contain the key (key) and its corresponding value (value). A pair of keys and values becomes an item. The key and value are separated by a colon: between items. Separate them with commas. An empty dictionary is a dictionary that does not contain any items. It can also be understood that an empty dictionary means that the curly braces do not contain any content and are expressed directly using curly braces {}.
Create a dictionary:
dict = {'name':'john','age':20,'sex':male}
Remarks: The key is a Immutable Data type
Accessing the dictionary:
Since the dictionary is unordered, the dictionary cannot be accessed through indexing; through variable name [key name] to access.
Dictionary added items:
Variable name: [newly added key name] = value corresponding to the newly added key
Value of dictionary modified items:
Variable name: [key name to be modified] = new value
Dictionary deletion item or value:
del method: delete the value corresponding to the key, del variable name [key name];
Delete the dictionary, del variable name.
clearMethod: Clear the dictionary contents.
Variable name.clear()
pop method: delete the value corresponding to the key, but it will output the corresponding value before deleting it
The above is the detailed content of Introduction to Python tuples, lists, and dicts. For more information, please follow other related articles on the PHP Chinese website!