Python tuples questions

What is the key difference between tuples and lists?
The main difference between the tuples and lists is that the tuples are immutable objects and lists are mutable. So, tuples cannot be modified while the lists can be modified. Tuples are more memory efficient than the lists and hence are typically faster
How to sort elements of a tuple?
Python tuples do not have the sort method. So, to sort the elements of a tuple, the sorted builtin function can be used by passing the tuple as the argument. The function returns a list with the items of tuple sorted in ascending order. This list can be converted into a tuple by using tuple( ) The default sorting order is ascending order. The reverse=True parameter sorts the items in descending order.
What are the two methods available for a tuple
Python tuples have two built-in methods:
count
index
How to find the number of occurrence of an element in a tuple?
my_tuple.count(element) will give the number of occurrence of an element in my_tuple
How to create a tuple?
An empty tuple can be created by using tuple constructor, tuple( ) or by using a pair of parentheses with no values in them.
A tuple with just one element (singleton tuple) can be created by using a trailing comma after the object or by using a pair of parentheses with the object in them.
A tuple with more than one element can be created by elements separated by commas or by using a pair of parentheses with the elements separated by commas in them.
How to delete a tuple?
del.my_tuple would delete my_tuple
How to add an element to a tuple?
Since tuples are immutable, elements cannot be added/modified/deleted from a tuple.
How to find the index of specific element in a tuple?
my_tuple.index(element,start,end) would retun the position of the first occurrence of the element in the tuple and If the element is not found, ValueError is raised.
How to compare two tuples to see if they have the same elements?
Python's python inbuilt cmp method can be used to compare two tuples to see if they have the same elements
Can a tuple be a member in another tuple?
Yes, tuples can be nested to any depth needed.
tup1 = (1, 2, 3)
tup2 = ('gk', 'nxt')
nested_tuple = (tup1, tup2)
How to convert a tuple into a list?
A tuple can be converted into a list by using the list constructor with the tuple as the argument
list(my_tuple)
How to convert a list into a tuple?
A list can be converted into a tuple by using the tuple constructor with the list as the argument
tuple(my_list)
What is a tuple?
A tuple is an immutable, ordered collection of objects. Python’s creator intended tuples for heterogenous data. Tuples are Python's way of packaging heterogeneous pieces of information in a composite object. For example, socket = ('www.python.org', 80) brings together a string and a number so that the host/port pair can be passed around as a socket, a composite object
Can a tuple be a member in itself?
Yes, but it will create a circular reference
gk