Python Tuple

Python tuple is another collection of immutable objects, which means its data items/values can not be changed during execution. There are much similar characteristics between lists and tuples. Because both of these are used to store ordered objects/items. The major difference between list and tuple is that list values can be change or update but tuple is immutable (cannot be changed).

While creating tuples data items are enclosed in parentheses.

Tuple Syntax

Python tuple syntax is much similar to list except parentheses. Similar to lists each object is separated by comma.

my__tuple = ( item1, item2, item3,..........itemN)

Python Tuple Example

my_tuple = (1,2,3,4,5)

print(my_tuple)

Output

(1, 2, 3, 4, 5)

Empty Tuple

In python empty tuple is created by using empty parentheses. This type of tuple also cannot be update.

Syntax

my_tuple = ()

Python create tuple

In python to create a tuple the use of parentheses is optional but it is good practice to use parentheses while creating tuples. It enhance the code readability.

Example

# Tuple with no element
null_tuple = ()
print("Empty tuple: ", null_tuple)

# Tuple having numbers
num_tuple = (1,3,5,7,9)
print("Tuple with numbers: ", num_tuple)

# Tuple with different objects
mix_tuple = (1.1, "IMS", 30)
print("Tuple having mix objects: ", mix_tuple)

Output

Empty tuple:  ()
Tuple with numbers:  (1, 3, 5, 7, 9)
Tuple having mix objects:  (4, 'Python', 9.3)

Python tuple to string

Similar to list we can also convert any tuple into string using string join method. Here we have to provide tuple to join method as parameter.

Syntax

str = ' '.join(tuple_identifier)

Example

str_tuple = ("1","3","5","7","9")
print(':'.join(str_tuple))

Output

In output of the above script we can see colon (:) as element separator in output string. We can use space or any other character/ string in place of colon.

1:3:5:7:9

Indexing in Tuple

We can access elements of tuples using index value. Similar to list the index of first value in tuple is also 0. And in a sequence the next element index will be 1 an so on. We can also use negative indexing to access tuple elements. In this case ethe index of last element will be -1 and the second last element's index will be -2 and so on.

Access tuple elements using index

To access any element from the tuple we have to specify its index value in square brackets.

Example

fruit_tuple = ("Apple","Mango","Banana","Orange")

# Accessing 2nd element
print(fruit_tuple[1])

# Accessing last element
# Index will be N - 1
# Where N is total elements

print(fruit_tuple[3])

Output

Mango
Orange

Tuple Slicing

In python to access particular range of tuple we use slicing technique. In this technique colon operator is used in index operator to specify range of elements. We can also store the resultant elements into new list for further processing.

Syntax

tuple_identifie[start_index : end_index]

Here both start and end index are optional. Means we can also use only colon operator, then result will include all elements from the tuple.

Start index will be included in result and end index value will be excluded

Example

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

# This will print all elements
print(int_tuple[:])

# This will print last two elements
# Second last element index is 8

print(int_tuple[8:])

# This will print first three elements
print(int_tuple[:3])

# This will print empty tuple
print(int_tuple[3:3])

Output

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

 

Comments
Login to TRACK of Comments.