6. Tuples

A tuple is a sequence of immutable Python objects of any type and are indexed by integer. The index value of tuple starts from 0.Tuples are just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses (), whereas lists use square brackets [ ]. Creating a tuple is as simple as putting different comma-separated values.

Example

1. Creation of Tuples

Creating empty tuple

Creating tuple with single element

Creation of Tuples with Multiple Values

Example :

Example :

2. Add new element to Tuple

3. Accessing values in Tuples

The values in tuples can be accessed using squared brackets , index number and slicing as illustrated below :

4. Updating Tuples

Tuples are immutable and hence cannot change the value of tuple elements . Consider the following example :

In the above example attempt is made to create tuple of the form (100, 34.56, ‘abc’, ‘xyz’) by item assignment . Type error is generated because python does not support item assignment for tuple objects. However a new tuple can be generated using concatenation as illustrated below :

5.Deleting Tuple Elements

Tuple object does not support item or range of items deletion :

However the entire tuple can be deleted as illustrated below :

6. Basic Tuple Operations

6.1 Length of a tuple

6.2 Concatenation

6.3 Repetition

6.4 Membership

6.5 Iteration

7.0 Tuple Assignment

If we want to interchange (swap) any two variable values, we have to use temporary
variable. For example;

But in python, tuple assignment is more elegant as illustrated below :

Example 1 :

Example 2

The left side is a tuple of variables, while the right side is a tuple of expressions. Each
value is assigned to its respective variable. All the expressions on the right side are
evaluated before any of the assignments. The number of variables on the left and the number of values on the right have to be the same:

8.0 Tuple Slices

Slice operator works on Tuple also. This is used to display more than one selected value
on the output screen. Slices are treated as boundaries and the result will contain all the
elements between boundaries.

Syntax is:   Seq = T [start: stop: step] 
Where start, stop & step all three are optional. If we omit first index, slice starts from ‘0’. 
On omitting stop, slice will take it to end. Default value of step is 1.

9. Comparing Tuples

Tuples can be compared using comparison operators like <, >, and == as illustrated below:

10. max( ) and min() functions

max()

It returns its largest item in the tuple. 
Syntax:  max(t) #t tuples 
returns maximum value among the given tuple.

min( )

Questions for Practice:

  1. Define Tuple , List , Strings and Dictionary in Python.
  2. Compare tuples with Lists. Explain how tuple can be converted into list and list into tuples.
  3. Write a program to input 5 subject names and put it in tuple and display that tuple information on the output screen.
  4. Write a program to input any two tuples and interchange the tuple values.
  5. Write a program to input ‘n’ numbers and store it in a tuple and find maximum & minimum values in the tuple.
  6. Find the output from the following code:
    • T=(10,30,2,50,5,6,100,65)
    • print max(T)
    • print min(T)
  7. Find the output from the following code:
    • t=tuple()
    • t = t +(PYTHON,)
    • print t
    • print len(t)
    • t1=(10,20,30)
    • print len(t1)
  8. Write a program to input two set values and store it in tuples and also do the comparison.
  9. Write a program to input ‘n’ employees’ salary and find minimum & maximum salary among ‘n’ employees.
  10. Write a program to input ‘n’ customers’ name and store it in tuple and display all customers’ names on the output screen