The practical application and significance of the Python variable type-tuple

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.