Python Tuples
Python Tuples
• We have to convert lists and strings to tuples using tuple() function before concatenation.
• We can have one tuple inside another. Such a tuple is called Nested tuple.
Eg: var2 = (1,3,5,7)
Var 3 = (2,4,6,8)
Nested_var = (var2, var3)
>>>nested_var
((1,3,5,7) , (2,4,6,8))
• Indexing and Slicing in Tuples:
• We can access each item in tuple using indexing.
• Eg: tuple_1 = (“Rose”, “Lotus”, “Sunflower”, “Lilly”, “Hibiscus”)
• Syntax : tuple_name[index]
>>>tuple_1[2]
Rose Lotus Sunflower Lilly Hibiscus
‘Sunflower’
>>>tuple_1[4] 0 1 2 3 4
‘hibiscus’
If we access an element not in the tuple, we get a tuple index out of range error.