Python Tuples

How to work with Python tuples

Python tuple is an immutable, ordered sequence of elements.

Python tuples are similar to Python list, except that we can't change the elements in a tuple. If you don't know how python list works, please go through this Python Lists

Python tuples are,
  • Immutable (cannot change the items of a tuples)
  • Ordered, follows insertion order. Can be accessed by index

How to create a Python tuple

# creates an empty tuple

t = ()
t1 = tuple()

# creating a tuple with some data

t = (1, 2, 3)
t1 = (1, 'hello', 4.5, [1, 2, 3])

# a tuple can have a tuple, list or dictionary as its item

t = (1, 2, ['a', 'b'], (1.5, 2.5), {1: 'x', 2: 'y'})
Special case with one element in a Python tuple,
t = ('one_element')
print(type(t))

# output : class 'str'
# even though the element is inside the `()` it will be treated as String.

Please put a `,` after the element, then only Python will treat it as a Python tuple.

t = ('one_element',)
print(type(t))

# output : class 'tuple'

How to access elements in a Python tuple

Python tuples can be accessed using index.
t = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

print(t[0])

# output : 1

print(t[-1])

# output : 10

print(t[100])

# output : IndexError: tuple index out of range


**When you try to access an element with index out of range. Python will trow an error.

Slicing in Python tuples

t = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

print(t[0:])

# output : (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

print(t[-1::-1])

# output : (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

print(t[2:5])

# output : (3, 4, 5)

Modifying the elements of a tuple

Ohh...! Python tuples are immutable. Can I change the elements of a tuple??
Yes, elements inside a tuple can be mutable as long as the element is mutable. Confused?? Lets see an example.
t1 = ('first', 'x', 'y', [1, 2, 3])

t1[0] = 'modified'

# output : TypeError: 'tuple' object does not support item assignment
In the above case Python tuple doesn't allow us to change the item because strings are immutable. Lets see another case.
t1 = ('first', 'x', 'y', [1, 2, 3])

t1[3][1] = 'modified'

print(t1)

# output : ('first', 'x', 'y', [1, 'modified', 3])
In this case Python tuple allowed us to modify the value inside a list. Because list is mutable. So to conclude, if the item inside a Python tuple is mutable then we can modify that value inside the mutable item. Like the case of a list that we did in the above code snippet.

Deleting the items from a tuple

Items from a tuple cant be deleted because Python tuple is immutable. We can delete the entire tuple.
t = (1, 2, 3)

del t

**If you try to access it after deletion, an error will be thrown.

Other useful functions of a Python tuple

t = (4, 2, 3, 4, 5, 6, 7, 4, 9, 10)

print(t.count(4))

# output : 3
# prints the no of times the given element is found in the tuple

print(t.index(5))

# output : 4
# prints the index of the first occurence of the given element

print(5 in t)

# output : True
# Returns True if the element is present in the tuple

print(5 not in t)

# output : False

print(len(t))

# output : 10

new_t = sorted(t)
print(new_t)

# output : [2, 3, 4, 4, 4, 5, 6, 7, 9, 10]
# remember returns a sorted list not a tuple

print(max(t))

# output : 10

print(min(t))

# output : 2

print(sum(t))

# output : 54

** mathematical functions like min, max and sum can be applied on tuples with numbers only

Comments

Post a Comment