Tuples ()

- Tuples = ( x, y, (m,n), k)

- Tuples are ordered

- Tuples allows duplicate elements

- Tuples are immutable -> tuple cannot be changed

        - You can not add an Item to a tuple

        - You can not remove an Item from a tuple

- Tuples can contain string, integer, float and even tuples (called nesting)

- Type of the variable is Tuple

- Index of first element of my_tuple (x) is 0

- Index of last element of my_tuple (k) is -1

Overview

- Creating a Tuple 

    - Usign tuple([ ])
    - Using ()

- Index of Tuples

- Range of Indexes (Slicing)

- Step Index

- Returning Index of Elements

- Concatenating Tuples (Combining Tuples)

- Converting a tuple to a list

- Converting a list to a tuple

- Count() Method

- Index() Method

- Type

- Lenghth of Tuples

- Sorting Tuples

- Tuples and Loop

- Tuples and if Statements

- Unpacking Single element

- Unpacking multiple elements using *

- Comparing Tuples with Lists

    - sys module
    - timeit Module

Creating a Tuple

- Usign tuple([ ])
- Using ()

Creating a Tuple using tuple([ ])

In [6]:
my_tuple = tuple(["Hello"])

my_tuple
Out[6]:
('Hello',)

Creating a Tuple usign ()

- If you creating a Tuple with one element: 

    - It is essential to add a comma after the item to be considered as a tuple

    - Otherwise, it will be considered as a string
In [7]:
my_tuple = ("Hello",)
print(type(my_tuple))

my_variable =  ("Hello")
print(type(my_variable))
<class 'tuple'>
<class 'str'>

Index of Tuples

- Showing an element of a Tuple
- Index of the first element is always 0
- Index of the last element is always -1
In [22]:
my_tuple = ( 10 , "hello", 1.50 , ("X is",3))

print(my_tuple[0])
print(my_tuple[1])
print(my_tuple[2])

print(my_tuple[3])
print(my_tuple[3][0])
print(my_tuple[3][1])

print(my_tuple[-1])
print(my_tuple[-1][0])
print(my_tuple[-1][1])
10
hello
1.5
('X is', 3)
X is
3
('X is', 3)
X is
3

Range of Indexes (Slicing)

- Showing multiple elements
In [26]:
my_tuple = ( 1, 3, 5, 7, 9)

print(my_tuple[0:1])

print(my_tuple[0:2])

print(my_tuple[3:5])

print(my_tuple[:])
(1,)
(1, 3)
(7, 9)
(1, 3, 5, 7, 9)

Step Index

   - step: the amount by which the index increases (defaults: 1)
In [29]:
my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)

print(my_tuple[::2])
(1, 3, 5, 7, 9)

Returning Index of Elements

In [21]:
my_tuple = ( 1, 3, 5, 7, 9)

check_tuple = my_tuple.index(7)

print(check_tuple)
3

Concatenating Tuples (Combining Tuples)

In [95]:
my_tuple = ( 2 , "bye", 100)

new_tuple = my_tuple + (0.5 , "Added", 150 ,)

print(new_tuple)
(2, 'bye', 100, 0.5, 'Added', 150)

Converting a tuple to a list

In [24]:
my_tuple = ( 2 , "bye", 100)

my_list = list(my_tuple)

my_list
Out[24]:
[2, 'bye', 100]

Converting a list to a tuple

In [25]:
my_list = [2, 'bye', 100]

my_tuple = tuple(my_list)

my_tuple
Out[25]:
(2, 'bye', 100)

Count() Method

In [96]:
my_tuple = ( 1, "hi", 5, 7, "hi", "hi")

check_tuple = my_tuple.count("hi")

print(check_tuple)
3

Index() Method

- Return index of the first occurrence of the element
In [1]:
my_tuple = ( 1, "hi", 5, 7, "hi", "hi")

check_tuple = my_tuple.index("hi")

print(check_tuple)
1

Type

In [16]:
my_tuple = ( 2 , "bye", 100)

type(my_tuple)
Out[16]:
tuple

Lenghth of Tuples

In [18]:
my_tuple = (1, 3, 5, 7, 9)

len(my_tuple)
Out[18]:
5

Sorting Tuples

In [100]:
my_tuple = ( 100, 3, 55, 70, 99)

sorted_tuple = sorted(my_tuple)

print(sorted_tuple)
[3, 55, 70, 99, 100]

Tuples and Loop

In [101]:
my_tuple = ( 100, 3, 55, 70, 99)

for x in my_tuple:
  
    print(x)
100
3
55
70
99

Tuples and if Statements

In [74]:
my_tuple = ( 100, 3, 55, 70, 99)

if "hello" in my_tuple:
  
    print("Yes, You won!")

else:
    
    print("No, You lost")
No, You lost

Unpacking Single element

In [41]:
my_tuple = ("John", "Smith", "50")

name, familyname, age = my_tuple

print(name)
print(familyname)
print(age)
John
Smith
50

Unpacking multiple elements using *

In [40]:
my_tuple = ( 100, 3, 55, 70, 99)

x1, *x2, x3 = my_tuple

print(x1)
print(*x2)
print(x3)
100
3 55 70
99

Comparing Tuples with Lists

- sys module
- timeit Module

Comparing Tuples with Lists - sys module

- Tuples are smallers 
- Tuples are more efficient
In [38]:
import sys

my_tuple = ( 100, 3, 55, 70, 99)

my_list = [100, 3, 55, 70, 99]

print(sys.getsizeof(my_tuple), "bytes")
      
print(sys.getsizeof(my_list), "bytes")
96 bytes
112 bytes

Comparing Tuples with Lists - timeit Module

- It takes longer time to create Lists than Tuples

- Tuples are more efficient
In [39]:
import timeit

print(timeit.timeit(stmt = "(100, 3, 55, 70, 99)" , number = 1000000 ))
      
print(timeit.timeit(stmt = "[100, 3, 55, 70, 99]" , number = 1000000))
0.022675175976473838
0.10135871398961172