Python's metagroup is similar to a list, except that the elements of the metagroup cannot be modified.
The group uses small brackets, and the list uses square brackets.
The creation of a group is simple, just add elements in parentheses and separate them with commas.
Here's an example:
tup1 = ('Google', 'W3CSchool', 1997, 2020) tup2 = (1, 2, 3, 4, 5 ) tup3 = ("a", "b", "c", "d")
Create an empty group
tup1 = ()
When you include only one element in a metagroup, you need to add a comma after the element
tup1 = (50,)
The u-group is similar to a string, and the underlying index starts at 0 and can be intercepted, combined, and so on.
Access to the metagroup
A u-group can use a underseed index to access values in a metagroup, as in the following example:
#!/usr/bin/python3 tup1 = ('Google', 'W3CSchool', 1997, 2020) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1: ", tup1) print ("tup2[1:5]: ", tup2[1:5])
The above example output results:
tup1: Google tup2[1:5]: (2, 3, 4, 5)
Modify the metagroup
Element values in a group are not allowed to be modified, but we can connect to a combination of the groups, as follows:
#!/usr/bin/python3 tup1 = (12, 34.56); tup2 = ('abc', 'xyz') # 以下修改元组元素操作是非法的。 # tup1 = 100 # 创建一个新的元组 tup3 = tup1 + tup2; print (tup3)
The above example output results:
(12, 34.56, 'abc', 'xyz')
Delete the group
Element values in a group are not allowed to be deleted, but we can use the del statement to remove the entire group, as in the following instance:
#!/usr/bin/python3 tup = ('Google', 'W3CSchool', 1997, 2020) print (tup) del tup; print ("删除后的元组 tup : ") print (tup)
When the above instance group is deleted, the output variable has abnormal information, and the output looks like this:
删除后的元组 tup : Traceback (most recent call last): File "test.py", line 8, in <module> print (tup) NameError: name 'tup' is not defined
The group operator
As with strings, the eddies can be used to perform operations between the plus and the numbers. This means that they can be combined and copied, and a new metagroup is generated after the operation.
|len((1, 2, 3))||3||Calculate the number of elements|
|(1, 2, 3) + (4, 5, 6)||(1, 2, 3, 4, 5, 6)||Connection|
|('Hi!') * 4||('Hi!', 'Hi!', 'Hi!', 'Hi!')||Copy|
|3 in (1, 2, 3)||True||Whether the element exists|
|for x in (1, 2, 3): print x,||1 2 3||Iteration|
Yuan index, intercept
Because the metagroup is also a sequence, we can access the elements at the specified location in the group, or we can intercept a segment of the index, as follows:
L = ('Google', 'Taobao', 'W3CSchool')
|L||'W3CSchool!'||Read the third element|
|L[-2]||'Taobao'||To read in reverse;|
|L[1:]||('Taobao', 'W3CSchool!')||Intercept elements, all elements after the second start.|
Here's the running example:
'Google', 'Taobao', 'W3CSchool') > L 'W3CSchool' > L[-2] 'Taobao' > L[1:] ('Taobao', 'W3CSchool')> L = (
Built-in functions for the group
The Python group contains the following built-in functions
|Serial number||Method and description||Instance|
Calculate the number of elements in the group.
>>> tuple1 = ('Google', 'W3CSchool', 'Taobao') >>> len(tuple1) 3 >>>
Returns the maximum value of the element in the metagroup.
>>> tuple2 = ('5', '4', '8') >>> max(tuple2) '8' >>>
Returns the element minimum value in the metagroup.
>>> tuple2 = ('5', '4', '8') >>> min(tuple2) '4' >>>
Convert the list to a metagroup.
>>> list1= ['Google', 'Taobao', 'W3CSchool', 'Baidu'] >>> tuple1=tuple(list1) >>> tuple1 ('Google', 'Taobao', 'W3CSchool', 'Baidu')
than two group elements