div { display:none; }

Trending technologies

#AWS #Azure #Blockchain #IOT #machine learning #Virtual Reality #Analytics #Business Intelligence

Tutorials

Blogs

Tuple is a collection which is ordered and immutable. It allows duplicate members. tuples are similar to a list in terms of indexing but tuples are unchangeable and Tuples are defined by enclosing the elements in parentheses (()) instead of square brackets ([]).

Declaration :


>>> a = (1, 2.2, 'python')

Example

/* Defination */

l = (1,2,3,4,5,6,7,8)

# l[2] = 1
print("l[2] = ", l[2])  /* Print use to print the Output*/

# l[0:3] = [1, 2, 3]
print("l[0:3] = ", l[0:3]) /* Print use to print the Output*/

# l[5:] = [5, 10, 15]
print("l[5:] = ", l[5:])  /* Print use to print the Output*/

Basic operations

Indexing 

>>> t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')

>>> t[0]
'foo'

>>> t[-1]
'corge'

Slicing

 
>>> t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')

>>> t[1::2]

('bar', 'qux', 'corge')

Deleting

 
>>> t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')

>>>print t;

('foo', 'bar', 'baz', 'qux', 'quux', 'corge')

>>> del t;

NameError: name 't' is not defined
 

Summary:

  1. Tuple are ordered.
  2. Tuple can contain arbitrary objects.
  3. Tuple can be indexed.
  4. Tuple can sliced.
  5. Tuples are unchangeable.
error: Content is protected !!