Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python - Tuples and Dictionaries


May 30, 2021 Article blog


Table of contents


Tuple of tuples

  • A special list of tuples, marked with ( ) that cannot be changed once established (neither the data items in them can be modified nor the data items can be modified and deleted).
  • And when there is only one element, you must add a comma after the element, otherwise it is defaulted to the operator ()

Basic instructions

Tuple ('args,'kwargs') converts other elements to tuple objects
Count (value) the number of elements of the statistical value
Index value (value, start-None, stop-None) index value

Built-in methods

len (tuple) calculates the number of tuple elements
max (tuple) returns the maximum value of the elements in the tuple
min (tuple) returns the minimum value of the element in the tuple

The tuple method is simple, and the created tuple cannot be changed (this does not mean that the variable cannot be changed, it can point to the new tuple value or otherwise, that is, the value within the tuple cannot be changed).

Dictionary dict

Key value pairs indicate data, similar to java Map, and are marked with a .

The types of keys here can be str (string), int (integer), float (float), bool (Boolean type), None (empty), unrecognizable using other types

e.g.: a'a': '1', 'b': '2', 'c': '3''

traverse:

For key in a: Get key traversal data
print(key+':'+a[key])
For key in a.keys(): . . . get the key and traverse the data
print(key+':'+a[key])
For value in a.values(): The data is traversed after the value is obtained
print(value)
For key, value in a.items(): The keys and values traverse the data
print(key+':'+value)

Traversing dictionary items:

for kv in a.items():
print(kv)

Basic instructions

clear() empty the dictionary
copy() copy the dictionary
get (key, default)) gets the value corresponding to the key value key, and returns default if it does not exist.
items() gets an iterator consisting of keys and values
Keys() Gets the iterator of the key
Values() gets the iterator of the value
pop (key) deletes the key:value member
update (adict) updates a member from another dictionary (created when it does not exist, overridden if it exists)
Update (E-None, sf) is updated from dict/iterable E and F.
If E exists and has a .keys() method, do the following for k in E: D (k) - E (k)
If E exists and the .keys() method is missing, do the following: for k, v:D (k) in E , v in both cases,
They are all closely followed: for k:D (k) in F .
Fromkeys (iter, value) establishes a dictionary with a given key in a list or tuple, with the default value being value
Popitem () removes any key:value item from the dictionary and returns it
Setdefault ('args,'kwargs') returns its corresponding value if there is a key value in the dictionary;
Str (dict) output dictionary, expressed as a printable string

Built-in methods

len (dict) calculates the number of dictionary elements, which is the total number of keys
type (variable) returns the type of variable entered and the dictionary type if the variable is a dictionary

Additional instructions

Dictionaries are more commonly used, generally commonly used json data after the conversion of objects are basically dictionary types, the use is also more extensive. H owever, the dictionary does not have a slice operation (and is not suitable for a slice operation).

List derivation is available because the implementation approach is primarily iterative and performance optimization, with the for loop type.

Recommended lessons: Python3 Getting Started, Python3 Advanced.