In this article, let's take a look at the tuples in the Python variable type, which is a common variable element. Hopefully this article will give you some help with the python you just touched.
python tuples: a tuple is another data type, similar to a list. The tuple is identified with a "()". The inner elements are separated by commas. However, tuples cannot be assigned two times, which is equivalent to a read-only list.
Let's give an example:
#!/usr/bin/python#-*-coding:utf-8-*-tuple = (' Runoob ', 786, 2.23, ' John ', 70.2) Tinytuple = (123, ' John ') print Tup Le # output full tuple print tuple[0] # The first element of the output tuple print Tuple[1:3] # outputs the second to third element of print tuple[2:] # Output all elements from the third start to the end of the list print Tinytuple * 2 # output tuple two times print tuple + tinytuple # printing combination of tuples
The results of the above example output are as follows:
(' Runoob ', 786, 2.23, ' John ', 70.2) Runoob (786, 2.23) (2.23, ' John ', 70.2) (123, ' John ', 123, ' John ') (' Runoob ', 786, 2.23, ' J Ohn ', 70.2, 123, ' John ')
The following are tuples that are not valid because tuples are not allowed to be updated. And the list is allowed to be updated , let's give an example as follows:
#!/usr/bin/python#-*-coding:utf-8-*-tuple = (' Runoob ', 786, 2.23, ' John ', 70.2) list = [' Runoob ', 786, 2.23, ' Joh N ', 70.2]tuple[2] = $ # tuple is illegal to apply list[2] = # # is a legitimate application in the list