[Python] Tuples

Source: Internet
Author: User

Python provides another useful built-in type:tuples. Tuples is used to store related pieces of information. Consider this example involving latitude and longitude:

>>> Angkorwat = (13.4125, 103.866667)>>>Print(Type (Angkorwat))<class 'tuple'>>>>Print("Angkor Wat is at Latitude: {}". Format (angkorwat[0])) Angkor Wat isAt latitude:13.4125>>>Print("Angkor Wat is at longitude: {}". Format (angkorwat[1])) Angkor Wat isAt longitude:103.866667

Tuples is similar to lists in, they store an ordered collection of objects which can is accessed by their indexes (fo R example AngkorWat[0] and AngkorWat[1] ). Unlike lists, tuples is immutable. You can ' t add and remove items from the tuples, or sort them in place.

Why tuples?

Why does we have the tuples if they ' re like lists and less features? Tuples useful when you have both or more values that is so closely related that they'll always be used together, like LA Titude and longitude coordinates.

Tuples can used to assign multiple variables in a compact by:

 >>> dimensions = =print  ( the dimensions is {}x{}x{}   .format ( Length, width, height) The dimensions is 52x40x100  
" Angkor Wat " ,                            ("Ancient Thebes",                            ("Petra",                            ("Machu Picchu"}

Notice that the values assigned to the tuple dimensions aren ' t surrounded with parentheses as previous examples were. The parentheses is optional when making tuples, and programmers frequently omit them if parentheses don ' t clarify the cod E.

Tuple Unpacking

The second line, three variables is assigned from the content of the tuple dimensions . This is called tuple unpacking. You can use tuple unpacking to assign the information from a tuple into multiple variables without have to access them O NE by one and make multiple assignment statements.

In this example, if we won ' t need to use dimensions directly, we could shorten those, and lines of code into a single Assigns three variables in one go!

Length, width, height = 52, 40, 100

Returning tuples
deffirst_and_last (sequence):"""Returns the first and last elements of a sequence"""    returnSequence[0], sequence[-1]>>> First_and_last (["Spam","Egg","Sausage","Spam"])('Spam','Spam')

A function that returns a tuple can also is used to assign multiple variables:

 >>> start, end = First_and_last ([ " spam  , "  egg  , "  sausage  , "   Spam   ])  >>> print   (start) Spam  >>> print   
def hours2days (_hours):     = _hours//24    = _hours%24    return days , Hours     hours2days ("" "(1, 0)"" "hours2days (" "" "" "" "" "" ""hours2days ( 10000) "" "  (416, +) " ""

[Python] Tuples

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.