What does python tuple mean?

藏色散人
Release: 2019-06-25 14:53:37
Original
21068 people have browsed it

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"
Copy after login

Create an empty tuple

tup1 = ()
Copy after login

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

tup1 = (50,)
Copy after login

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]
Copy after login

Output results of the above examples:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!