This article shares the introduction of Python Tuples (tuple), lists, and dictionaries (dict ).
Tuples are commonly represented by parentheses (). The elements are identified by commas.
1 # define a tuple 2 3 # tuple = 'a', 4 5 tuple = ('A', 'B', 'C', 'D', 'e ', 'F', 'g') 6 7 # In general, after defining a tuples, you cannot add or modify the elements of the tuples, however, you can add elements that modify the tuples to the tuples. 8 9 print tuple [] 10 11 tuple = tuple [: 2] + ('h') + temp [2:] 12 13 print (tuple) 14 15 # Use a for loop to traverse tuples 16 17 for each in tuple: 18 19 print each20 21 # Use range () function and for loop get element serial number 22 23 for index in range (len (tuple): 24 25 print tuple [index]
List commonly used square brackets, that is, [];
To create a list, you only need to enclose different data items separated by commas (,) in square brackets.
For example:
1 list1 = ['a','b','c',1,3,5]2 list2 = [1,2,3,4,5,6]3 list3 = ["abc","bcd","cde"]
Traversal list: (len (each): indicates the length of each iteration variable, and each: indicates the variable of each iteration)
1 list1 = ['a','b','c',1,3,5]2 for each in list13 print(each,len(each))
Common functions in the list:
Cmp (list1, list2): compares two list elements.
Len (list): returns the number of list elements.
Max (list): returns the maximum value of the list element.
Min (list): returns the minimum value of the list element.
List (tuple): convert tuples to a list
The following nine methods are commonly used in the list:
List. append (obj): add a new object to the end of the list.
List. count (obj): count 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 can be used to expand the list.
List. insert (index, obj): insert an object before the index element in the list.
List. pop (obj = list [-1]): By default, one element in the list is removed (the last element by default) and the value of this element is returned.
List. remove (obj): removes a value from the list.
List. reverse (): sorts the elements in the list in reverse order.
List. sort (function (): sorts the list.
The dictionary contains data from curly braces {}. the curly braces contain keys and values. a pair of keys and values is an item, keys and values are separated by colons. items and items are separated by commas. an empty dictionary is a dictionary that does not contain any items, it can also be understood as an empty dictionary, which means that no content is contained in the curly braces and is directly represented by curly braces.
Create a dictionary:
Dict = {'name': 'John', 'age': 20, 'Sex': male}
Note: The key is an unchangeable data type.
Access dictionary:
Because the dictionary is unordered, you cannot access the dictionary by indexing. you can access the dictionary by using the variable name [key name.
Dictionary add item:
Variable name: [new key name] = value corresponding to the new key
Value of the dictionary modification item:
Variable name: [key name to be modified] = new value
Delete a dictionary item or value:
Del method: delete the value corresponding to the key. del variable name [key name];
Delete the dictionary and del variable name.
Clear method: clear the dictionary content.
Variable name. clear ()
Pop method: delete the value corresponding to the key, but it will output the corresponding value and then delete it.
The above are the details introduced by the Python Tuples (tuple), list, and dict. For more information, see other related articles in the first PHP community!