Python: tuple; python: tuple

Source: Internet
Author: User

Python: tuple; python: tuple

1. The value of the tuples cannot be modified.

#!/usr/bin/pythontup =("a","b","c");tup[1]=7

The following error occurs after execution:

Traceback (most recent call last ):
File "C: \ Program Files \ Sublime Text 3 \ time. py", line 3, in <module>
Tup [1] = 7
TypeError: 'tuple' object does not support item assignment


2. The elements of the tuples cannot be deleted separately, but only all elements can be deleted.

#!/usr/bin/pythontup =("a","b","c");del tupprint "After deleting tuple:\n"print tup
Execution result:
After deleting tuple:
Traceback (most recent call last ):
File "C: \ Program Files \ Sublime Text 3 \ time. py", line 6, in <module>

Print tup
NameError: name 'tup' is not defined

3. the parentheses of a single element must contain commas. Otherwise, they are not considered as tuples.

#!/usr/bin/pythontupleone=(1,)print "tupleone is :",type(tupleone)tupleoneerror=(1)print "tupleanotherone is :",type(tupleoneerror)

Execution result;

Tupleone is: <type 'tuple'>

Tupleanotherone is: <type 'int'>

Iv. Split tuples and define variables

#!/usr/bin/pythonmyinfo=("jinhoward",30,"Male")name,age,gender=myinfoprint "Name=",nameprint "Age=",ageprint "Gender=",gender
Execution result:

Name = jinhoward
Age = 30
Gender = Male

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.