Tuple definition
A tuple is another data type, similar to a list.
The tuple is identified with a "()". The inner elements are separated by commas. However, an element cannot be assigned two times, which is equivalent to a read-only list.
Example:
tuple = (' ABCD ', 786, 2.23, ' John ', 70.2)
list = [' ABCD ', 786, 2.23, ' John ', 70.2]
Tuple[2] = 1000 # tuples are illegal to apply
List[2] = 1000 # is a legitimate application in the list
Print tuple * 2 # output tuple two times
Dictionary definitions
The Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects.
The difference between a list and a dictionary is that the elements in the dictionary are accessed by keys, not by offsets.
The dictionary is identified with "{}". A dictionary consists of an index (key) and a value corresponding to it.
{key1:value1, key2:value2, ..., Keyn:valuen}
Dictionaries are also referred to as associative arrays or hash tables.
Two things to note in a dictionary:
1, the keys in the dictionary must be unique, but the value is not necessary. When created, if the same key is assigned two times, the old value is replaced;
2, the key must be immutable, can be used as a number, string or tuple, but can not use the list
Create a dictionary
(1) Factory method
Adict = Dict () or
Adict = Dict (([' X ', 1],[' Y ', 2]))
(2) Keyword parameters
adict= dict (name= ' Allen ', age= ' 40′)
(3) Built-in method
L1 = [D.fromkeys] (L1)
Dict.fromkeys (L1, ' over ') {1: "Over", 2: ' Over ', 3: ' Over '}
B={}.fromkeys ((' x ', ' Y '), 1)
Update dictionary Operations
Adict[new_key] = value form adds an item
Adict[old_key] = new_value Update a data item (element) or key-value pair
del Adict[key] Delete keys key
Del adict Delete entire dictionary
Dictionary Common Methods
1, Adict.keys () returns a list containing all keys of the dictionary;
2, Adict.values () returns a list containing all the value of the dictionary;
3, adict.clear () Delete all the items or elements in the dictionary;
4, Adict.get (key, default = None) returns the value corresponding to the key in the dictionary, and returns the value of default if the key does not exist in the dictionary (default defaults to none);
5, Adict.pop (Key[,default]) is similar to the Get method. If there is a key in the dictionary, delete and return the key corresponding to the Vuale; if the key does not exist and the default value is not given, the keyerror exception is thrown;
6, Adict.update (bdict) Add dictionary bdict key value pairs to the dictionary adict, none Add, there is overwrite
Example:
Adict={1: ' A ', 2: ' B ', 3: ' C '}
Bdict={1: ' AA ', 5: ' CCC ', 8: ' 66 '}
Adict.update (bdict)
Print Adict
For key in Adict.keys ():
print key
For value in Adict.values ():
print value
For Key,value in Adict.items ():
print str(key)+‘:‘+str(value)
Python basic tuple (tuple), dictionary (Dictionary) detailed