Python – Tuples

Python – Tuples

This article covers about tuples in python. Tuples can be a collection of homogeneous or heterogeneous items. It can be seen as similar to list but the major difference is that list is mutable unlike tuple which is immutable and list uses square brackets but in tuple we use parentheses.

Creating a Tuple:

a) Empty tuple creation: It is created using two parentheses with no element in it.

tuple1 = ()
print(tuple1)

Output:

()

b) Creating a non-empty tuple with a single value: It is created by writing a single value within parentheses along with a comma.

# create a tuple with
# a single elment

tuple2 = (86,)

print(tuple2)

# check the data-type
print(type(tuple2))

# asigning as a tuple
# but python consider this 
# as a int value
tuple3 = (86)

print(tuple3)

# check the data-type
print(type(tuple3))

Output:

(86,)
<class 'tuple'>
86
<class 'int'>

c) Creating a non-empty tuple:

tuple4 = (23, "Delhi", 46, 
          "Mumbai")

print("Simple tuple:\n", 
      tuple4)


t1 = (10, 20, 30)
t2 = ("python", "java")

# nested tuple
tuple5 = (t1,t2)


print("Nested tuple:\n",
      tuple5)

# tuple with * operator
tuple6 = ("biochemithon",) * 3

print("tuple with same repeated value:\n",
      tuple6)

Output:

Simple tuple:
 (23, 'Delhi', 46, 'Mumbai')
Nested tuple:
 ((10, 20, 30), ('python', 'java'))
tuple:
 ('biochemithon', 'biochemithon', 'biochemithon')

Accessing elements in tuple:

Indexing starts from zero in a tuple. Elements from the tuple can be accessed using square brackets along with integer value.
a) Using Non-Negative indexing: Non-Negative index starts from the front of the tuple i.e., 0 means the first element, 1 means 2nd starting element and so on.

tuple1 = ('monday', 'wednesday', 
          'friday')

print("First element:", 
      tuple1[0])
      
print("Second element:",
      tuple1[1])

Output:

First element: monday
Second element: wednesday

b) Using Negative indexing: Negative index starts from the end of the tuple or back side of tuple i.e., -1 means the last element, -2 means 2nd last element and so on.

tuple1 = ('fruits', 'veggies', 
          'drinks')

print("last element:", 
      tuple1[-1])

print("2nd last element:", 
      tuple1[-2])

Output:

last element: drinks
2nd last element: veggies

Slicing in tuple:

We can also fetch some parts of elements from tuple by specifying the range of indexes.

tuple2 = (28, 48, 36,
          25, 67, 98, 
          88)

# tuple slicing
print(tuple2[3 : 5])

Output:

(25, 67)

Note: In this, the starting index 3 is included and ending index 5 is excluded.

tuple2 = (28, 48, 36,
         25, 67, 98,
         88)

# tuple slicing by using
# -ve indexing
print(tuple2[-5 : -2])

Output:

(36, 25, 67)

Joining or Concatenation of tuples:

We can join two or more tuples by addition (+) operator.

t1 = (4, 6, 8, 11)
t2 = ('four', 'six',
     'eight', 'eleven')

# concatenation 
tuple3 = t1 + t2

print(tuple3)

Output:

(4, 6, 8, 11, 'four', 'six', 'eight', 'eleven')

Finding Length of a tuple:

To find the length we use Python built-in len() function.

t1 = ('California', 'Texas', 
     'Florida', 'Washington', 
     'Michigan')
     
print(len(t1))

Output:

5

Deleting a tuple:

For deleting a tuple we can use Python del keyword.

t1 = ('broccoli', 'pumpkin', 
    'beetroot', 'corn')

# deleting t1
del t1

# error, since t1 
# no longer exists
print(t1) 

Output:

Traceback (most recent call last):
  File "main.py", line 9, in 
    print(t1) 
NameError: name 't1' is not defined

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds