What is Tuples In Python

 

What is Tuples In Python

  • A tuple is a one-dimensional, fixed-length, immutable sequence of Python objects. Immutable implies that its content cannot be modified.

  • The easiest way to create a tuple is to provide a comma-separated sequence of values. In this example, a tuple is assigned a bunch of mixed comma separated data type values enclosed within parentheses.

  • Let’s view it by referencing the tuple object. To access the tuple element at index 1, use the syntax shown here. The index usually starts from 0.

  • If you try to modify the value at a specified index, it throws an error since a tuple is immutable.


How To Print The Values In a Tuple

Example: 

tup1 = ('physics', "chemistry", 1997, 2000)

print (tup1)

Output:

('physics', 'chemistry', 1997, 2000)

Explanation:


Here in the Tuple called tup1 we have stored the values and with print command we have printed the tuple values


How To Access elements in Tuples

tup1 = ('physics', "chemistry", 1997, 2000)
print (tup1)
print (tup1[2])

Output:

('physics', 'chemistry', 1997, 2000)
1997


Some Operations on Tuple

We can use the index of an element to view and access it. To access elements with the help of positive indices, count from the left, starting with 0. You can also use negative indices by counting from the right, starting with -1. Negative indices are useful as they help you to easily refer to elements at the end of a long tuple. Once you have seen how to access individual elements in a tuple. You can also access a range or slice of elements within a tuple. Slicing allows you to create a subset of the tuple. To slice a tuple, mention the indices of the first element and that of the element immediately after the last element. This is because while the first index is inclusive, the second one is not. For example, here we can see that referencing indices 1 to 4, creates a tuple subset containing the elements from index 1 to 3. You can also use negative indices to slice tuples, as shown here.

How To Access Tuple Elements Using Indexes

tup1 = ('abcd', 786, 2.23, 'john', 70.2) tinytuple = (123, 'jane') print (tup1) print () print (tinytuple)

Output:

('abcd', 786, 2.23, 'john', 70.2)

(123, 'jane')

print (tup1[0])
print()             # for space

print (tup1[1:3])
print()

print (tup1[2:])       # access the ele at 2nd index till the end
print()

print (tinytuple * 2) # repetition
print()

print (tup1 + tinytuple)  # concat

Output:


abcd

(786, 2.23)

(2.23, 'john', 70.2)

(123, 'jane', 123, 'jane')

('abcd', 786, 2.23, 'john', 70.2, 123, 'jane')


How To Access Tuple Elements Using Negative Indexes

To access the Tuple Elements from the right of th tuples you can use the negative index

var = ("Red", "Black", "Blue") print("Value in Var[-3] = ", var[-3]) print("Value in Var[-2] = ", var[-2]) print("Value in Var[-1] = ", var[-1])

Output:

Value in Var[-3] = Blue Value in Var[-2] = Black Value in Var[-1] = Red

How To Create Nesting Tuples

tuple1 = (0, 1, 2, 3) tuple2 = ('Red', 'Black') tuple3 = (tuple1, tuple2) print(tuple3)


Output:

((0, 1, 2, 3), ('Red', 'Black'))

How To Find The Length Of A Tuple

tuple2 = ('Red', 'Black') print(len(tuple2))

Output:

2

How To Convert A Tuple In List

list1 = [0, 1, 2] print(tuple(list1)) print(tuple('python'))

Output:

(0, 1, 2) ('p', 'y', 't', 'h', 'o', 'n')


How To Create Loops In Tuples

tup = ('Tuple',) n = 5 # Number of time loop runs for i in range(int(n)): tup = (tup,) print(tup)


Output:

(('Tuple',),) ((('Tuple',),),) (((('Tuple',),),),) ((((('Tuple',),),),),) (((((('Tuple',),),),),),)



Comments