Tuples

Introduction

Python tuples are meant to hold various types of data, pretty much like lists one of the only differences are that instead of using “[ ]” you use  “()”, but are still indexed and can be updated and modified pretty much like Tuples.

You can declare a variable tuple empty:

Tuple1 = ()


Or you can initialize it:

Tuple2 = ( 1, 2, 3, 4, 5, 6, 7, 8, ….)


And you can even place different types of data in the same tuple:

Tuple3 = ( 1, “Hello”, 4.68, True, “World” )


There is a variable assignment which is a tuple assignment.

int, int2 = (25, 31)


That will respectively assign 25 to int and 31 to int2. Now imagine you want to trade two variable’s values:

temp = a
a = b
b = temp


Multiple variables assignment in Tuples

With tuple assignment this is super easy:

(a, b) = (b, a)


And, an error will be produced if you try to assign less or more variables than you should:

(a, b, c, d) = (1, 2, 3)
ValueError: need more than 3 values to unpack


Accessing Tuples

are indexed by integers starting on position 0 and until n-1, being n the length of the Tuple.

Example:

Tuple4 = ( "Hey", "nice", "to", "meet", "you")


Has length 5 and can be indexed like this:

 

0 1 2 3 4

 

print( Tuple4[0] )
print( Tuple4[1] )
print( Tuple4[2] )
print( Tuple4[3] )
print( Tuple4[4] )

 

Output:

Hey
nice
to
meet
you


Which will print out every member of Tuple4.

But we can also access it with negative numbers which do it from the end to the start. Like this:

 

-5 -4 -3 -2 -1

 

print( Tuple4[-1] )
print( Tuple4[-2] )
print( Tuple4[-3] )
print( Tuple4[-4] )
print( Tuple4[-5] )

 

Output:

you
meet
to
nice
Hey


This will print the Tuple4 backward

Or we can also use a for to iterate over the Tuple.

int = 0
while int < len(Tuple4):
    print(Tuple4[int])
    int += 1

 

Output:

Hey
nice
to
meet
you


This consists of a while loop that increments a variable until it is out-of-bounds of Tuple4.
Important: Since the Tuple indexes start at 0, then the last index is the size of the Tuple - 1.

Operations

 

Repetition
 

We can repeating the contents by x times. Example:

Tuple5 = Tuple4 * 3
print( Tuple5 )

 

Output:('Hey', 'nice', 'to', 'meet', 'you', 'Hey', 'nice', 'to', 'meet', 'you', 'Hey', 'nice', 'to', 'meet', 'you')

Will print the Tuple5 which is 3 times the Tuple4.
    
Concatenation
 

We can also concatenate Tuples by adding them.

Example:

Tuple6 = ["How’s" , "the", "weather"]
print( Tuple4 + Tuple6 )   

 

Output:('Hey', 'nice', 'to', 'meet', 'you', 'How’s', 'the', 'weather')


Will print a bigger Tuple containing Tuple4 and Tuple6    

Membership
 

There is a way to finding out if a specific value exists in a specific Tuple.

Example:

print( "Hey" in Tuple4)

 

Output: True  


This will print out True because the string “Hey” indeed exists in Tuple4.

Iteration
 

Using the Membership operator, we can iterate over every member of a Tuple.
 

for member in Tuple4:
    print(member)

 

Output:

Hey
nice
to
meet
you


For each iteration of this loop, the temporary variable member will be assigned a value of the Tuple being printed out and passing on to the next value.

Length
 

To get the size of a specific Tuple. Using the method len().

Example:

print( len( Tuple4 ) )

 

Output: 5


This code will print out the size of Tuple4 which is 5.

Slicing
 

Tuples can be sliced to show only desired values or to iterate over desired indexes.

print(Tuple4[1:3])
print(Tuple4[2:])     


First-line will print from index 1 to 3 and the second will print starting on index 2.

Working with Tuples

Tuples can be very useful to hold data, like student names for a runtime, trying to compare students or sort the Tuple alphabetically. Since Tuples are very versatile on what data can be held, it eases a lot of its use.

Advantages of using tuples over lists

Since tuples are quite like lists, both are used in similar situations as well.
However, there are certain advantages of implementing a tuple over a list. 
-  We generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes.
-  Since tuples are immutable, iterating through tuple is faster than with list. -> slight performance boost.

Function and Methods

Python Tuple Built-in functions

 

Function

Description

E.g

len(Tuple)

It is used to calculate the length of the Tuple.

len( Tuple7 ) will return 3

max(Tuple)

It returns the maximum element of the Tuple.

max( Tuple7 ) will return 8

min(Tuple)

It returns the minimum element of the Tuple.

min( Tuple7 ) will return 1

Tuple(seq)

It converts a list to a tuple.

Tuple([1,2,3,4,5,6,7]) will create the Tuple

( 1, 2 , 3, 4, 5, 6, 7)

Python Tuple built-in methods

Function

Description

E.g

Tuple.count(obj)

It returns the number of occurrences of the specified object in the Tuple.

Tuple9 = ("Hello", "World" )

print(Tuple9.count("Hello"))

It will print out 1 which is the number of times the string “Hello” is in Tuple9.

Tuple.index(obj)

It returns the lowest index in the Tuple that object appears.

print(Tuple9.index("Hello"))

It prints out 0 which is the index of “Hello”.

Related Tutorials