home PYTHONJAVA
 

Python3 tuple

Python's tuples are similar to lists, except that the elements of the tuple cannot be modified.

Tuples use parentheses and lists use square brackets.

Tuple creation is simple, just add elements to the parentheses and separate them with commas.

实例(Python 3.0+)

>>>tup1 = ('Google', 'Welookups', 1997, 2000); >>> tup2 = (1, 2, 3, 4, 5 ); >>> tup3 = "a", "b", "c", "d"; # No need for parentheses >>> type(tup3) <class 'tuple'>

Create an empty tuple

When a tuple contains only one element, you need to add a comma after the element, otherwise the parentheses will be used as operators:

Instance (Python 3.0+)

>>>tup1 = (50 ) >>> type( Tup1) # without commas, type For integers <class ' int'> >>> tup1 = (50,) >>> type( Tup1) # plus comma, type Is a tuple <class ' tuple'>

The tuple is similar to a string. The index of the subscript starts from 0 and can be intercepted, combined, and so on.

When a tuple contains only one element, you need to add a comma after the element, otherwise the parentheses will be used as operators:

Instance (Python 3.0+)

>>>tup1 = (50 ) >>> type( Tup1) # without commas, type For integers <class ' int'> >>> tup1 = (50,) >>> type( Tup1) # plus comma, type Is a tuple <class ' tuple'>

The tuple is similar to a string. The index of the subscript starts from 0 and can be intercepted, combined, and so on.

......

The output of the above example:

tup1[0]: Google
Tup2[1:5]: (2, 3, 4, 5)

Modify the tuple

The element values in the tuple are not allowed to be modified, but we can combine the tuples, as in the following example:

Instance (Python 3.0+)

#!/usr/bin/python3 tup1 = (< Span class="hl-number">12, 34.56); tup2 = (< Span class="hl-quotes">'abc', 'xyz' ) # The following modifications to tuple element operations are illegal. # tup1[0] = 100 # Create a new tuple tup3 = tup1< Span class="hl-code"> + tup2; print (tup3)

The output of the above example:

(12, 34.56, 'abc',  'xyz')

Delete tuple

The element values in the tuple are not allowed to be deleted, but we can use the del statement to delete the entire tuple, as in the following example:

Instance(Python 3.0+)

#!/usr/bin/python3 tup = ('Google', 'Welookups', 1997, 2000) print (tup) del tup; print ("Deleted tuple tup : ") print (tup)

After the above instance tuple is deleted, the output variable will have exception information, the output is as follows:

Deleted tuple tup :
Traceback (most recent call last):
File "test.py", line 8, in <module>
print (tup)
NameError: name 'tup' is not defined

Tuple operator

As with strings, tuples can be operated with + and * signs. This means that they can be combined and copied, and a new tuple is generated after the operation.

Python ExpressionResults Description
len((1, 2, 3))3 Calculate the number of elements
(1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6)Connection< /td>
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!')Copy
3 in (1, 2, 3)TrueElements exist
for x in (1, 2, 3): print (x,)1 2 3iteration

tuple index, interception

Because the tuple is also a sequence, we can access the elements of the specified position in the tuple, or we can intercept an element in the index as follows:

Tuple:

L = ('Google', 'Taobao', 'Welookups')
Python ExpressionResults Description
L[2]'Welookups'Read the third element
L[-2]'Taobao'Reverse reading; reading the penultimate element
L[1:]('Taobao', 'Welookups') Intercept the elements, all elements from the second start.

Run the example as follows:

>>> L = ('Google', 'Taobao', 'Welookups')
>>> L[2]
'Welookups'
>>> L[-2]
'Taobao'
>>> L[1:]
('Taobao', 'Welookups')





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.