Python type of tuples of data collection, Dictionary

Tuple (tuple)
tuple create very simple, only need to add elements in parentheses and separated by commas can be. And a list of different is that element of the tuple can not be changed. As shown in the following code:

tup1 = ()
tup2 = (1)
tup3 = (1,)
tup4 = ('test', 1, False)
tup5 = (1, 2, 3, 4, 5)
tup6 = "a", "b", "c", "d"

Print (type (TUP1))
Print (type (tup2))
Print (type (tup3))
Print (type (tup4))
Print (type (tup5))
Print (type (tup6))
above will output the following:

<class 'tuple'>
<class 'int'>
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
by what we found only tup2 output type is int, the rest are tuple. Why is this? If the parentheses is only one element, add after "," in order to represent tuples. Although not used tup6 (), but also a special writing tuples.

Operation tuple
of tuples (tuple) and a list of similar, except that the elements of the tuple can not be modified. We can be related to the operation of tuples. As shown in the following code:

a = (1, 'test' , ' Hello',. 1 + 2, False, [2,. 3, 'CHINA'])
Print ( "Access tuple element 1 by indexing:", a [0] )
Print ( "access tuple of two elements by indexing:", A [. 1])
Print ( "access tuple third element by indexing:", A [2])
Print ( "accessed by indexing tuple of four elements: ", A [. 3])
Print (" access metadata by indexing group 5 elements: ", A [. 4])
Print (" access tuple sixth element by indexing: " , A [. 5])
Print ( "access mode using a negative number, sixth tuple elements:", a [-1])
Print ( "access mode using a negative number, tuple fifth element is:", a [ -2])

print ( "second tuple element to the second element 6, in steps of 2:", A [. 1:. 5: 2])
print ( "penultimate to the last number:", a [-2:] )
Print ( "tuple sixth element is a list, access a nested list of elements:", a [5] [ 0])
above will output the following:

Accessing the first element of the tuple by indexing: 1
Access tuple second indexing elements by: test
accessing the third element of the tuple by indexing: Hello
Access tuple the fourth element by indexing: 3
by index access element group 5 elements: False
access tuple sixth element by indexing: [2, 3, 'CHINA']
with negative access mode, the tuple sixth element is: [2, 3 ' CHINA ']
with negative access method, the first tuple element is 5: False
tuple element to the second element 6, in steps of 2: (' test ', 3 )
the penultimate to the last number: ( false, [2, 3, ' CHINA'])
tuple sixth element is a list, access a nested list of elements: 2

Collection (set)
set (set) by one or more of various shapes composed of overall size, things or objects referred to as element configuration set or member. The basic function is to test membership and remove duplicate elements. Braces {} may be used or a set () function creates a set of note: Create an empty set must be set () instead of {}, {} as is used to create an empty dictionary.

Creating format

set1 = {value1,value2,...}
或者
set(value)

We can be related to the operation of tuples. As shown in the following code:

name = {'testDog', 'Jim', 'Mary', 'Jim', 'Jack', 'Rose'}

# Output set print (name), repetitive elements are automatically removed

Members of the test

IF 'testDog' in name:
Print ( 'testDog in the collection')
the else:
Print ( "not set testDog ')

a set operation set

a = set('testDogDog')
b = set('test')
c = {}

Print (A)
Print (b)
Print (type (C))
Print (A - b) # A and b difference set
print (a | b) # a and b and set
print (a & b) # a and b intersection
print (a ^ b) # of elements a and b are not simultaneously present in
the above code will output the following:

{'Mary', 'Rose', 'testDog', 'Jack', 'Jim'}
testDog 在集合中
{'t', 'e', 'g', 's', 'o', 'D'}
{'t', 's', 'e'}
<class 'dict'>
{'D', 'o', 'g'}
{'t', 'e', 'g', 's', 'o', 'D'}
{'t', 's', 'e'}
{'o', 'g', 'D'}

Note: Since sets are unordered, so order your output results may not match with the author, not an error, if you define an empty {}, will be the type of dictionary.

Dictionary (dict)
list is an ordered collection of objects, dictionaries are unordered collections of objects. A dictionary mapping type, a dictionary "{}" identifier, which is a random key (key): Value (value) pairs. Key (key) must be immutable. In the same dictionary, the key (key) must be unique. As shown in the following code:

a = {"code": "10000", "result": 1, "status": 1, "text": "操作成功"}
print("取code的值", a["code"])

print ( "Output all keys", a.keys ())
print ( "all output values", a.values ())
above will output the following:

10000 code value takes
the output of all keys dict_keys ([ 'code', ' result', 'status', 'text'])
the output values of all dict_values ([ '10000', 1 , 1, ' successful operation'])

Summary: The data types are numbers, strings, lists, tuples, sets, dictionary. We will identify the types of data through common points and differences mind maps, easy to remember. As shown below:

Data type .png

Welcome attention to micro-channel public number: Wang software testing. Software testing exchange group: 809 111 560

Guess you like

Origin blog.51cto.com/14421641/2415251