In these Python tuples tutorial lern Tuples. Accessing Values in Tuples. To access values in tuple, use the square brackets for slicing along with the index. A tuple is like a list of a sequence of immutable python objects. Tuple is collection of ordered and unchangeable they are written in round brackets. They are separated by commas in python. Similar like the list, sequencing indexing and repetition but tuple is immutable and list are mutable..The difference between the list and the tuple is the list is declared in square brackets and tuple are declared in circle brackets. The values to be stored in the tuple must be of any type and index by the integers. The tuple()b function is also called as build in function used to create a tuple. This function accepts the single parameter.
tuple1= (‘java’,’python’)
tuple2= (‘ram’,’sita’)
#nesting of tuple is done
tuple3=(tuple1+tuple2)
Print (tuple3)
O/p:-
((‘java’,’python’),(’ram’,’sita’))
Slicing in Tuples:-
tuple1 = (1, 2, 3, 4)
Print (tuple1 [1 :])
O/p:-
2
O/p:-
(1, 2, 3, 4)
Finding Length of a Tuple:-
e.g:-
tuple1= (‘ram’,’sita’,’savita’)
Print (len(tuple1))
O/p:-
3
e.g:-
List1= [‘ram’,’sita’,’savita’]
Print (tuple(list1))
Print (tuple('java')
O/p:-
(‘ram’,’sita’,’savita’)
(‘p’,’y’,’t’,’h’,’o’,’n’)
It will take the single parameter which may be list string or dictionary and convert it into the tuple.
e.g:-
thistuple =("apple", "banana", "cherry")
print(thistuple[1])