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. Similarly, the index values are always integer.Whenever we try to access an item with an index more than the tuple’s length, it will throw the 'Index Error'.As tuples are ordered sequences of items, the index values start from 0 to the tuple’s length.We can access an item of a tuple by using its index number inside the index operator and this process is called “Indexing”. It maintains the index value for each item. Using slicing, we can access a range of items from a tupleĪ tuple is an ordered sequence of items, which means they hold the order of the data insertion. Using indexing, we can access any item from a tuple using its index number.This section will guide you by accessing tuple using the following two ways Tuple can be accessed through indexing and slicing. Sample_tuple = tuple((1, 2, 3, "Hello", ))Īs we can see in the above output we are printing each and every item in the tuple using a loop. We can iterate a tuple using a for loop Let us see this with an example. This will return the number of items in the tuple. We can find the length of the tuple using the len() function. In case we assign fewer variables than the number of items in the tuple, we will get the value error with the message too many values to unpack Length of a Tuple # packing variables into tupleĪs we can see in the above output, three tuple items are assigned to individual variables i, j, k, respectively. Similarly, we can unpack the items by just assigning the tuple items to the same number of variables. Generally, this operation is referred to as tuple packing. Packing can be used when we want to collect multiple values in a single variable. In Python, we can create a tuple by packing a group of variables. Packing and UnpackingĪ tuple can also be created without using a tuple() constructor or enclosing the items inside the parentheses. So the variable type was class str, and the second time it was a class tuple. # without commaĪs we can see in the above output the first time, we did not add a comma after the “Hello”. If the tuple time is a string enclosed within parentheses and not followed by a comma, Python treats it as a str type. Create a tuple with a single itemĪ single item tuple is created by enclosing one item inside parentheses followed by a comma. Sample_tuple2 = tuple(('Jessa', 30, 45.75, ))Īs we can see in the above output, the different items are added in the tuple like integer, string, and list. # create a tuple using tuple() constructor String_tuple = ('Jessa', 'Emma', 'Kelly') Using a tuple() constructor: Create a tuple by passing the comma-separated items inside the tuple().Ī tuple can have items of different data type integer, float, list, string, etc # create a tuple using ().Using parenthesis (): A tuple is created by enclosing comma-separated items inside rounded brackets.Count the occurrence of an item in a tuple.
0 Comments
Leave a Reply. |