![]() ![]() ![]() We should keep in mind that the index value always starts with a 0.įor easy understanding, we will be using an integer tuple with values from 0 to 9 similar to how an index value is assigned. The output will be a tuple, and it includes the range of items with the index values from the start till the end of the range. We can specify the start and end values for the range of items to be accessed from the tuple. We can even specify a range of items to be accessed from a tuple using the technique called ‘Slicing.’ The operator used is ':'. # Output P, Y, T, H, O, N, Slicing a tuple # iterate a tuple using negative indexing Access tuple items using the negative index valueĮxample tuple1 = ('P', 'Y', 'T', 'H', 'O', 'N'). ![]() The index values can also be negative, with the last but the first items having the index value as -1 and second last -2 and so on.įor example, We can access the last item of a tuple using tuple_name. # TypeError: tuple indices must be integers or slices, not float tuple1 = ('P', 'Y', 'T', 'H', 'O', 'N')Īlso, if you mention any index value other than integer then it will throw Type Error. Note: If we mention the index value greater than the length of a tuple then it will throw an index error. tuple1 = ('P', 'Y', 'T', 'H', 'O', 'N')Īs seen in the above example, we print the tuple’s first four items with the indexing. In the above image, we can see that the index values start from zero and it goes till the last item whose index value will be len(tuple) - 1 . If we give any other type, then it will throw Type Error.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |