Python TuplesA Python Tuple is a group of items that are separated by commas. The indexing, nested objects, and repetitions of a tuple are somewhat like those of a list, however unlike a list, a tuple is immutable. The distinction between the two is that while we can edit the contents of a list, we cannot alter the elements of a tuple once they have been assigned. Example Features of Python Tuple
Creating of Tuple:To create a tuple, all the objects (or "elements") must be enclosed in parenthesis (), each one separated by a comma. Although it is not necessary to include parentheses, doing so is advised. A tuple can contain any number of items, including ones with different data types (dictionary, string, float, list, etc.). Code: Output: Empty tuple: () Tuple with integers: (4, 6, 8, 10, 12, 14) Tuple with different data types: (4, 'Python', 9.3) A nested tuple: ('Python', {4: 5, 6: 2, 8: 2}, (5, 3, 5, 6)) Tuples can be constructed without using parentheses. This is known as triple packing. Code Output: (4, 5.7, 'Tuples', ['Python', 'Tuples']) <class 'tuple'> <class 'TypeError'> The construction of a tuple from a single member might be hard. Simply adding parenthesis around the element is insufficient. To be recognised as a tuple, the element must be followed by a comma. Code Output: <class 'str'> <class 'tuple'> <class 'tuple'> Accessing Tuple ElementsWe can access the objects of a tuple in a variety of ways.
To access an object of a tuple, we can use the index operator [], where indexing in the tuple starts from 0. A tuple with 5 items will have indices ranging from 0 to 4. An IndexError will be raised if we try to access an index from the tuple that is outside the range of the tuple index. In this case, an index above 4 will be out of range. We cannot give an index of a floating data type or other kinds because the index in Python must be an integer. TypeError will appear as a result if we give a floating index. The example below illustrates how indexing is performed in nested tuples to access elements. Code Output: Python Tuple tuple index out of range tuple indices must be integers or slices, not float l 6
Python's sequence objects support negative indexing. The last item of the collection is represented by -1, the second last item by -2, and so on. Code Output: Element at -1 index: Collection Elements between -4 and -1 are: ('Python', 'Tuple', 'Ordered') SlicingIn Python, tuple slicing is a common practise and the most popular method for programmers to handle practical issues. Think about a Python tuple. To access a variety of elements in a tuple, you must slice it. One approach is to use the colon as a straightforward slicing operator (:). We can use a slicing operator, a colon (:), to access a range of tuple elements. Code Output: Elements between indices 1 and 3: ('Tuple', 'Ordered') Elements between indices 0 and -4: ('Python', 'Tuple') Entire tuple: ('Python', 'Tuple', 'Ordered', 'Immutable', 'Collection', 'Objects') Deleting a TupleA tuple's components cannot be altered, as was previously said. As a result, we are unable to get rid of or remove tuple components. However, a tuple can be totally deleted with the keyword del. Code Output: 'tuple' object does not support item deletion name 'tuple_' is not defined Repetition Tuples in PythonCode Output: Original tuple is: ('Python', 'Tuples') New tuple is: ('Python', 'Tuples', 'Python', 'Tuples', 'Python', 'Tuples') Tuple MethodsPython Tuples is a collection of immutable objects that is more like to a list. Python offers a few ways to work with tuples. These two approaches will be thoroughly covered in this essay with the aid of some examples. Examples of these methods are given below.
The number of times the specified element occurs in the tuple is returned by the count () function of Tuple. Code Output: Count of 2 in T1 is: 5 Count of java in T2 is: 2 Index() Method:The first instance of the requested element from the tuple is returned by the Index() function. Parameters: The element to be looked for.
Code Output: First occurrence of 1 is 2 First occurrence of 1 after 4th index is: 6 Tuple Membership TestUsing the in keyword, we can determine whether an item is present in the given tuple or not. Code Output: True False False True Iterating Through a TupleWe can use a for loop to iterate through each element of a tuple. Code Output: Python Tuple Ordered Immutable Changing a TupleTuples, as opposed to lists, are immutable objects. This suggests that we are unable to change a tuple's elements once they have been defined. The nested elements of an element can be changed, though, if the element itself is a changeable data type like a list. A tuple can be assigned to many values (reassignment). Code Output: 'tuple' object does not support item assignment ('Python', 'Tuple', 'Ordered', 'Immutable', [1, 2, 10, 4]) ('Python', 'Items') To merge multiple tuples, we can use the + operator. Concatenation is the term for this. Using the * operator, we may also repeat a tuple's elements for a specified number of times. This is already shown above. The results of the operations + and * are new tuples. Code Output: ('Python', 'Tuple', 'Ordered', 'Immutable', 4, 5, 6) Following are Some Advantages of Tuples over Lists:Lists take longer than triples. The code is protected from any unintentional changes thanks to tuples. It is preferable to store non-changing data in "tuples" rather than "lists" if it is required by a programme. If a tuple includes immutable values like strings, numbers, or another tuple, it can be used as a dictionary key. Since "lists" are mutable, they cannot be utilized as dictionary keys.
Next TopicPython Set
|
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week