Thursday, October 11, 2018

Working with Tuples

We learned about Tuples when data types were introduced. So far we know that in Python an immutable list is called a tuple. sometimes you’ll want to create a list of items that cannot
change. Tuples allow you to do so.

A tuple looks just like a list except you use parentheses () instead of square brackets []. Once you define a tuple, you can access individual elements by using each item’s index, just as you would for a list. Tuples cannot have their values modified, appended, or removed.

The perfect examples of a tuple would be days of week and months in a year, as these are always fixed and can't be changed. Let's define these tuples now -

days_of_week = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

months_in_year = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')


To access an element in the tuple individually, we use the same syntax as used for list. Thus, to print the first day of week from days_of_week tuple we'd simply write -

days_of_week[0]

Similarly first month of the year can be accessed by -

months_in_year[0]

See the screen shot shown below for the program and output - 



 If we try to change the value of any element in a tuple Python raises an error (shown in screenshot)

days_of_week[0] = 'Lunes'

 

Basically, because we’re trying to alter a tuple, which can’t be done to that type of object, Python tells us we can’t assign a new value to an item in a tuple.

Looping through Tuple

Like we did in List, a for loop can be used for looping over all the values in a tuple. To display allthe days in a week we can use the following syntax -

for day in days_of_week:
   
    print(day)

See the screen shot shown below for the program and output -



 Python returns all the elements in the tuple, just as it would for a list.

Overwriting a tuple

Although we can’t modify a tuple, we can assign a new value to a variable that holds a tuple. For example, we'll change our days_of_week tuple to hold name of days in Spanish.

days_of_week = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
days_of_week = ('lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado', 'domingo')

See the screen shot shown below for the program and output -



Python doesn’t raise any errors this time, because overwriting a variable is valid.

If you have only one value in your tuple, you can indicate this by placing a trailing comma after the value inside the parentheses. Otherwise, Python will think you’ve just typed a value inside regular parentheses. The comma is what lets Python know this is a tuple value. (Unlike some other programming languages, in Python it’s fine to have a trailing comma after the last item in a list or tuple.)

For example-

message = ('This is a tuple',)

If we write  message = ('This is a tuple') this will be treated as a string.

Enter the following type() function calls into the interactive shell to see the distinction. See the screen shot below -



If you need an ordered sequence of values that never changes, use a tuple. A second benefit of using tuples instead of lists is that, because they are immutable and their contents don’t change, Python can implement some optimizations that make code using tuples slightly faster than code using lists.

Converting Types with the list() and tuple() Functions

The functions list() and tuple() will return list and tuple versions of the values passed to them. Enter the following into the interactive shell, and notice that the return value is of a different data type than the value passed:



Thus converting a tuple to a list is handy if you need a mutable version of a tuple value.
Share:

0 comments:

Post a Comment