tuples in python

Tuples -: Tuples are data structures this is very similar to lists but tuples can not be modified.  Tuples  can only be created. Tuples are so much like lists that you may wonder why Python bothers to include them. The reason is that tuples have important roles that ca not be efficiently filled by lists such as keys for dictionaries.

tuple1 = (10, 20, 30, 40)  
print(tuple1)  
count = 0  
for i in tuple1:  
print("tuple1[%d] = %d"%(count, i));

Output of this Programme

(10, 20, 30, 40)
tuple1[0] = 10
tuple1[0] = 20
tuple1[0] = 30
tuple1[0] = 40

Basic Tuples-: Creating a tuple is similar to creating a list. assign a sequence of values to a variable. A list is a sequence that are enclosed by [ and ]; a tuple is a sequence that’s enclosed by ( and ) as like this

>>> x = ('a', 'b', 'c','d','e')

This line 5 dimension  tuples created .

After a tuple is created we are using it. It is using a list that it’s easy to forget that tuples and lists are different data types such as like

>>> x[2]
'c'
>>> x[1:]
('b', 'c')
>>> len(x)
3
>>> max(x)
'c'
>>> min(x)
'a'
>>> 5 in x
False
>>> 5 not in x
True

One-element tuples need a comma-: A small syntactical point is associated with using tuples. Because the square brackets used to enclose a list is not used elsewhere in Python. It is  clear that [] means an empty list and that [1] means a list with one element. The same thing is not true of the parentheses used to enclose tuples. Parentheses can also be used to group items in expressions to force a certain evaluation order. If we say (x + y) in a Python program  do It  means that x and y should be added and then put into a one-element tuple or It means that the parentheses should be used to force x and y to be added before any expressions to either side come into play?

This situation is a problem only for tuples with one element  because tuples with more than one element always include commas to separate the elements and the commas tell Python that the parentheses indicate a tuple not a grouping. In the case of one-element tuples Python requires that the element in the tuple be followed by a comma to disambiguate the situation. In the case of zero-element (empty) tuples there’s no problem. An empty set of parentheses must be a tuple because it’s meaningless otherwise.

>>> x = 5
>>> y = 4
>>> (x + y)   # This line adds x and y.
9
>>> (x + y,)  # Including a comma indicates that the parentheses denote a tuple.
(9,)
>>> ()        # To create an empty tuple, use an empty pair of parentheses.
()

Packing and unpacking tuples-:  Python permits tuples to appear on the left side of an assignment operator  in which case variables in the tuple receive the corresponding values from the tuple on the right side of the assignment operator. example of this …

>>> (one, two, three, four) =  (1, 2, 3, 4)
>>> one
1
>>> two
2

This example can be written even more simply, because Python recognizes tuples in an assignment context even without the enclosing parentheses. The values on the right side are packed into a tuple and then unpacked into the variables on the left side.

one, two, three, four = 1, 2, 3, 4

One line of code has replaced the following four lines of code as like

one = 1
two = 2
three = 3
four = 4

This technique is a convenient way to swap values between variables. Instead of saying

temp = var1
var1 = var2
var2 = temp

simply say

var1, var2 = var2, var1

To make things even more convenient, Python 3 has an extended unpacking feature allowing an element marked with * to absorb any number of elements not matching the other elements. Again, some examples make this feature clearer here..

>>> x = (1, 2, 3, 4)
>>> a, b, *c = x
>>> a, b, c
(1, 2, [3, 4])
>>> a, *b, c = x
>>> a, b, c
(1, [2, 3], 4)
>>> *a, b, c = x
>>> a, b, c
([1, 2], 3, 4)
>>> a, b, c, d, *e = x
>>> a, b, c, d, e
(1, 2, 3, 4, [])

The starred element receives all the surplus items as a list and that if there are no surplus elements  the starred element receives an empty list. Packing and unpacking can also be performed by using list delimiters.

>>> [a, b] = [1, 2]
>>> [c, d] = 3, 4
>>> [e, f] = (5, 6)
>>> (g, h) = 7, 8
>>> i, j = [9, 10]
>>> k, l = (11, 12)
>>> a
1
>>> [b, c, d]
[2, 3, 4]
>>> (e, f, g)
(5, 6, 7)
>>> h, i, j, k, l
(8, 9, 10, 11, 12)

Converting between lists and tuples-: Tuples can be easily converted to lists with the list function which takes any sequence as an argument and produces a new list with the same elements as the original sequence. lists can be converted to tuples with the tuple function which does the same thing but produces a new tuple instead of a new list.For example..

>>> list((1, 2, 3, 4))
[1, 2, 3, 4]
>>> tuple([1, 2, 3, 4])
(1, 2, 3, 4)
list is a convenient way to break a string into characters
>>> list("Hello")
['H', 'e', 'l', 'l', 'o']

Difference between tuples and lists -: The main difference between tuples and lists is that tuples are immutable. An attempt to modify a tuple results in a confusing error message which is Python way of saying that it doesn’t know how to set an item in a tuple.

List is mutable.The tuple is immutable.The List has the variable length.The tuple has the fixed length.The list provides more functionality than tuple. The tuple provides less functionality than the list.The list Is used in the scenario in which we need to store the simple collections with no constraints where the value of the items can be changed. The tuple is used in the cases where we need to store the read-only collections as like the value of the items can not be changed. It can be used as the key inside the dictionary.

 

Leave a Comment