If the content is fixed and never changes then we should go for Tuple. t = 70 ValueError: tuple object does not support item assignment If the Content is not fixed and keeps on changing then we should go for List. once we create Tuple Object we cannot change its content. Eg: i = 70 Tuple Objects are Immutable i.e. once we create List Object we can perform any changes in that Object. Eg: i = The tuple is a Group of Comma-separated Values within Parenthesis and Parenthesis are optional. List Tuple The list is a Group of Comma-separated Values within Square Brackets and Square Brackets are mandatory. In both cases insertion, the order is preserved, duplicate objects are allowed, heterogeneous objects are allowed, index and slicing are supported. List and Tuple are exactly the same except small difference: List objects are mutable whereas Tuple objects are immutable. The sorted() function is going to sort the elements in the ascending order if we want to print the tuple elements in the descending order then, we have to follow the below method Tuple1=(35,21,60,20) The following example demonstrates the sorting of tuple elements Tuple1=(35,21,60,20) Python does not support sorting of elements present in the tuple, because the tuple is immutable, but we can sort the elements of the tuple by using the sorted() function.īut the sorted() function will return the list as the output, we can convert the sorted list into a new tuple by using the tuple() function. The following example demonstrates the reversed() function Tuple1=(10,20,30,40) The syntax for the reversed() function is: Rversed(Tuple) The python does not support the reversing of the tuple elements because the tuple is immutable,īut we can perform the reversed operation by creating a new tuple from the reversed objects.
0 Comments
Leave a Reply. |