python basics - Datatypes: Collections and Tuple

In the previous post, we have discussed the one of the collection type - List. and in this topic, we are going to discuss some traits of Tuple. 

Tuple is very much like the List except that you cannot modify the content of the tuple, and because of this, Tuple can often be used in passing arguments and return values in and out of one function. 

As in the Python basics - Datatypes : Collectons and List , we will also discusses the 

  • create a list of empty elements, one element, elements of the same type, elements of mixed types of tuples.

Please find the code below. 

'''
Created on 2012-10-22

@author: Administrator
file: Tuples.py
description: this module file demonstrate how to use the tuples
'''

def define_tuples():
    empty_tuple = ()
    one_element_tuple = (1,) # think why we must have (1,) rather than (1)
    isovalent_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 12)
    nonisovalent_tuple = (1, "two", 3, 4.0, ["a", "b"], (5, 6)) # it is not allowed to have 3L in python 3.x now

    print("*** empty tuple\n", empty_tuple)
    print("*** one element tuple\n", one_element_tuple)
    print("*** isovalent tuple\n", isovalent_tuple)
    print("*** nonisovalent_tuple\n", nonisovalent_tuple)

if __name__ == '__main__':
    define_tuples()

你可能感兴趣的:(python)