Lesson 7: Python Tuples

Tuples are very similar to lists except a main difference: mutability. Tuples are immutable meaning they can not be changed unlike lists.

Otherwise, like lists, they can be consisted of different kind of elements and they have indexing meaning there is an order of elements inside them.

Index starts with 0 (zero) just like lists and negative index starts with -1, which refers to the last item in the tuple.

Used Where?

Tuples are another commonly used data sequences in Python. 
 
  • .index(): used to show the index of an element
  • .count(): used for counting how many elements there are in the list
  • sum(): sums all the elements in the list
  • min(): shows the element with lowest value in the list
  • max(): shows the element with highest value in the list

Syntax do(s)

1) Tuples are constructed using parenthesis: ()

2) Elements inside your tuple should be separated with commas.

3) Syntax regarding data types need to be followed inside the tuples: strings in quotes and integers, booleans and float not in quotes.

Syntax don't(s)

1) Elements inside your tuple should be separated with commas.

Function 1: tuple()

tuple() creates an empty tuple which can also be achieved by simply typing empty parenthesis: ().

Below is a tuple consisting of different types of elements. 

Example 1

>>> p_tup = (“Oslo”, “Stockholm”, 44, True)
>>> print(p_tup)

(“Oslo”, “Stockholm”, 44, True)

Accessing Elements: List elements can be accessed with their index number in brackets.

p_tup = (“Oslo”, “Stockholm”, 44, True)

for instance to access the first element “Oslo” the correct syntax would be: 

p_tup[0]

and to access 44:

p_tup[2]

Let’s look at some examples.

 

Last element of the below tuple is a boolean with the value: True. We will access it inside the print function below.

Example 2

>>> p_tup =  (“Oslo”, “Copenhagen”, 44, True)
>>> print(p_tup[3])

True

And accessing the first element of the tuple.

Example 3

>>> p_tup = (“Oslo”, “Copenhagen”, 44, True)
>>> print(p_tup[0])

“Oslo”

Tips

1- Reverse Indexing: Similar to the lists elements in the tuple can also be accessed starting from the end which is reverse indexing. Reverse indexing starts with -1 which indicates the last element. 

So, to access the last element of p_tup:
p_tup[-1] can be used and it would give you the last element of the tuple. p_tup[-2] would give you the second from the last, p_tup[-3] third from the last and so on.

2- Main difference of tuples from lists is that they are immutable which means they can’t be changed after they are created. Other than re-creating them you can’t append or remove elements the way you could for the lists.

So, it makes sense to use tuples where you know you won’t change your elements and otherwise it makes more sense to use lists.

Example 4

>>> p_tup = (“Oslo”, “Copenhagen”, 44, True)
>>> print(p_tup[-1])

True

Function 2: .index()

.index() is another useful method for tuples. It’s used for identifying the index number of an element inside a tuple.

Let’s see an example.

And here is a similar example for an empty dictionary assignment.

Example 5

>>> p_tup = (“Oslo”, “Copenhagen”, 44, True)
>>> print(p_tup.index(“Oslo”))

0

Function 3: .count()

.count() method is useful to find out how many occurrences there are in a tuple for a specific element. 

In the example below, you can see that integer 5 is counted inside the list lucky_numbers. When we print, we get the output 1, meaning 5 occurs once in the list.

Example 6

>>> p_tup = (5, 101, 42, 3, 101)
>>> print(p_tup.count(101))

2

Function 4: sum()

.sum function will give you the total sum of a tuple consisted of numbers. 

Example 7

>>> lucky_numbers = (5, 55, 4, 3, 101, 42)
>>> print(sum(lucky_numbers))

210

Function 5: min()

min() function will show you the minimum value of a tuple. 

Let’s see an example.

Example 8

>>> lucky_numbers = (5, 55, 4, 3, 101, 42)
>>> print(min(lucky_numbers))

5

Function 6: max()

max() function will show you the maximum value of a tuple. 

Let’s see an example.

Example 9

>>> lucky_numbers = (5, 55, 4, 3, 101, 42)
>>> print(max(lucky_numbers))

101

Advanced Concepts (Optional)

There are some other methods and Python functions we will see in the upcoming lessons. Some of them deserve their own lesson for the sake of keeping this lesson at a reasonable length.

Next Lesson: Python Dictionaries

“Have you installed Python yet?”
Having Python setup on your computer can expedite the learning experience and help you gain the real life skills in a non-simulation environment. Installing Python can have different nuances that can be confusing for everyone. We compiled a complete tutorial about the best and most convenient Python installing practices Check it out by clicking the button below.
SANDRA FILES