Featured post

Why should I learn Go?

Image
What is unique about GO language? Here are some of the advantages of GO programming language:           Code runs fast           Garbage collection           Simpler objects           Efficient concurrency Code runs faster: Before understanding why GO runs faster, let us know the process of software translation. Basically, we have three broad categories of languages:             Machine level language ·        Machine level language is a low-level language where instructions are directly executed on the CPU. Machine level instructions are small steps which are straight forward and simple (Ex: ADD, SUBTRACT, MULTIPLY ) Assembly language ·        Assembly language is similar to machine level language but a bit more specific for humans to understand. For example, 1000...

Tuples in Python






In the previous post, we have learnt about lists and their uses as stacks and queues. In this post, we are going to concentrate on another useful data structure called “TUPLE”.

Lists and Strings are two examples of sequence data types. Since Python is an evolving language, other sequence data types may be added. There is also another standard sequence data type: the tuple.

A tuple consists of a number of values separated by commas, for instance:

>>> = 1234554321'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')



>>> # Tuples may be nested:
... = t, (12345)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> # Tuples are immutable:
... t[0= 88888
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>



TypeError: 'tuple' object does not support item assignment
>>> # but they can contain mutable objects:
... = ([123], [321])
>>> v
([1, 2, 3], [3, 2, 1])

As you see, on output tuples are always enclosed in parenthesis.They may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression).

Tuples are immutable:

Tuples are immutable. By immutable, we mean that once a tuple has been declared, it is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists.

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking or indexing.

Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

A special problem is the construction of tuples containing 0 or 1 items. The syntax is somewhat different.
 Empty tuples are constructed by an empty pair of parentheses. A tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).

 For example:

>>> empty = ()
>>> singleton = 'hello',    # <-- note trailing comma



>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)

The statement t = 12345, 54321, 'hello!' is an example of tuple packing. ie, the values 1234554321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:

>>> x, y, z = t

This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side.
 Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. 

Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.

Source : Official Documentation on Tuples taken from "www.python.org"


Popular posts from this blog

Introduction to Big Data and Hadoop

LocationManager vs GoogleApiClient

Why should I learn Go?