Python3 tuple

Python3 tuple

Python tuples are similar to lists. They can also store different types of values. The difference is that the elements of the tuple cannot be modified, and the size of the tuple cannot be changed, which means that modifications and deletions cannot be performed.
Use parentheses for tuples and square brackets for lists.
Tuple creation is very simple, just add elements in parentheses and separate them with commas.
Code example:

tup1=("hello","world",123,12.3,456)
tup2=(10,22,33,45,5,62,15)
tup3=("ls","ll","cd","mv")
tup4=()  #This is creating an empty tuple

print(tup1)print(tup2)print(tup3)print(tup4)

operation result:

(‘hello’, ‘world’, 123, 12.3, 456)
 (10, 22, 33, 45, 5, 62, 15)
 (‘ls’, ‘ll’, ‘cd’, ‘mv’)
 ()

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

tup1=(10)  #This is a wrong way of declaration, parentheses will be regarded as operators
tup2=(10,) #Correct single-element tuple declaration

print(type(tup1))print(type(tup2))

operation result:

<class 'int'>
 <class 'tuple'>

Access tuple

Like lists, tuples also use subscripts to access the values in tuples, code examples:

tup1=("hello","world",123,12.3,456,233)print(tup1[1])print(tup1[1:5])

operation result:

world
 (‘world’, 123, 12.3, 456)

Modify tuple

The element value in the tuple is not allowed to be modified, but we can connect and combine the tuple. Code example:

tup1 =(10,22,33,45,5,62,15)
tup2 =("ls","ll","cd","mv")

tup3 = tup1 + tup2  #Need to create a new tuple to store the combined data
print(tup3)

operation result:

(10, 22, 33, 45, 5, 62, 15, ‘ls’, ‘ll’, ‘cd’, ‘mv’)

If you modify the elements in the tuple, an error will be reported
**Error example: **

tup1=(10,22,33,45,5,62,15)
tup1[0]=100  #This modification of tuple elements is illegal.

print(tup1)

As a result, the following exception will be thrown:

Traceback (most recent call last):
   File “E:/PythonProject/TestTup4.py”, line 2, in  
     tup1[0]=100
 TypeError: ‘tuple’ object does not support item assignment

Delete tuple

The element value in the tuple is not allowed to be deleted, but we can use the del statement to delete the entire tuple, code example:

tup1=(10,22,33,45,5,62,15)

del tup1

**Error example: **

tup1=(10,22,33,45,5,62,15)

del tup1[0]

As a result, the following exception will be thrown:

Traceback (most recent call last):
   File “E:/PythonProject/TestTup4.py”, line 3, in  
     del tup1[0]
 TypeError: ‘tuple’ object doesn’t support item deletion

Tuple operator

Like character strings, you can use + and * to perform operations between tuples. This means that they can be combined and copied, and a new tuple will be generated after the operation
Tuple index, interception
Because a tuple is also a sequence, we can access the element at a specified position in the tuple, and we can also intercept a section of elements in the index, as shown below:
Tuple:

L = (‘Google’, ‘Taobao’, ‘Runoob’)

**Code example: **

tup1 =("hello","world",123,12.3,456,233)print(tup1[2])print(tup1[-2])print(tup1[1:])

operation result:

123
 456
 (‘world’, 123, 12.3, 456, 233)

Tuple built-in functions

Recommended Posts

Python3 tuple
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python exec
Python notes
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
Centos6 install Python2.7.13
Python answers questions
Python basic syntax (1)
Centos7 install Python 3.6.
ubuntu18.04 install python2
Python classic algorithm
Relearn ubuntu --python3
Python2.7 [Installation Tutorial]
Python string manipulation
Python 3.9 is here!
Python study notes (1)
python learning route
CentOS7 upgrade python3
Python3 basic syntax
linux+ubuntu solve python
Functions in python
Python learning-variable types
CentOS install Python 3.6
7 features of Python3.9
Python file operation
ubuntu12.04 install python3
Python design patterns
Python - centos6 installation
Centos7 install Python2.7
01. Introduction to Python