Python basic data type--tuple

Source: Internet
Author: User

Meta-group

Tuples are immutable sequences that cannot be modified after they are created.

1 #Create a tuple that contains only one element2>>a = (3,)3>>Print(a)4(3,)5 6 #convert to tuples using tuple ()7>>atuple = Tuple ('ABCDA')8>>Print(atuple)9('a','b','C','D','a')Ten  One #You can only delete an entire tuple, you cannot delete an element A>>delAtuple

Tuples are faster to access and process than lists,

If a series of constant values are defined, the main purpose is only to traverse, without modification,

Recommended use of tuples

Sequence Unpacking

1 #use sequence unpacking to assign values to multiple variables at the same time2>>x,y,z = A3>>vtuple = (false,3.5,'Exp')4>>a,b,c =Vtuple5>>Print(a,b,c)6False 3.5Exp7 8 #traversing multiple sequences at the same time9>>keys = ['a','b','C']Ten>>values = [A] One>> forKvinchZip (keys,values): A>>Print(k,v) -A 1 -B 2 theC 3 -  - #Generator Derivation - #the result of the builder derivation is a generator object +>>g = ((i+2) **2 forIinchRange (10)) ->> forIinchg: +>>Print(i,end=' ') A4 9 16 25 36 49 64 81 100 121

Python basic data type--tuple

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.