Home  >  Article  >  Backend Development  >  What does python tuple mean?

What does python tuple mean?

藏色散人
藏色散人Original
2019-06-21 11:14:1321056browse

Python tuple (tuple) is an immutable sequence. Python tuples are similar to Python list data, both are linear tables. The only difference is that the data stored in a Python tuple cannot be modified by the program. You can think of a tuple as a list that can only read data but not modify it.

What does python tuple mean?

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

Use parentheses for tuples and square brackets for lists.

Tuple creation is very simple, just add elements in brackets and separate them with commas.

The following example:

Example (Python 2.0)

tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d"

Create an empty tuple

tup1 = ()

When the tuple contains only one element, you need to add a comma

tup1 = (50,)

after the element. Tuples are similar to strings. The subscript index starts from 0 and can be intercepted, combined, etc.

Accessing tuples

Tuples can use subscript indexes to access the values ​​in the tuple, as shown below:

Example (Python 2.0)

#!/usr/bin/python
 
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]

Output results of the above examples:

tup1[0]:  physics
tup2[1:5]:  (2, 3, 4, 5)

Related recommendations: "Python Tutorial"

The above is the detailed content of What does python tuple mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn