Tuples in Python

 Python Tuples

Tuple is a collection of objects, separated by comma and are placed in parentheses. Which are immutable.


Any sequence in '()' represents tuple.


Ones tuple is declared or initialized it can not be  updated or modified.


Initialization of Tuple

There are 2 ways to initialize tuple.


Direct way of initializing tuple

Comma separated sequence is placed in parenthesis and in this way the tuple is initialized, as follows.

Program

>>> a = (1, 2, 3)
>>> print(a)
(1, 2, 3)
>>> 
>>> b = (5, 5.6, True, "Hi")
>>> print(b)
(5, 5.6, True, 'Hi')



Using tuple() method

The collection of objects are placed in a square bracket or in parentheses, and are passed as an argument to the tuple() method.

Program

>>> a = tuple((1, 2, 3))
>>> print(a)
(1, 2, 3)
>>> b = tuple([3, 5.6, False, "Hello"])
>>> print(b)
(3, 5.6, False, 'Hello')



Operations with tuple

Before going further keep in mind that, tuples are immutable i.e. Once tuple is declared or initialized, it can not be updated or modified ( elements can not be added or removed after declaration or initialization of tuple).


Program

>>> a = (1, 2, 3)
>>> a[3] = 4
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    a[3] = 4
TypeError: 'tuple' object does not support item assignment



Trick to modify a tuple

Follow the following steps to modify a tuple.

  1. Convert a tuple into a list, by using list() method.
  2. Do the modifications.
  3. Then convert it again into tuple, by using tuple() method.

Program

>>> a = (1, 2, 3, 4, 5, 6, 7, 8)
>>> a = list(a)
>>> del a[5:]
>>> a = tuple(a)
>>> print(a)
(1, 2, 3, 4, 5)


Accessing elements in tuple

Elements can not be added or removed but, it can be accessed.


Accessing an element in tuple

Provide index to access an element in a tuple.


Program

>>> num = ('zero', 'one', 'two', 'three')
>>> num[0]
'zero'
>>> num[3]
'three'


Accessing element with negative number in tuple

It's useful when accessing an element from the end of a tuple.


Use '-1' to access the last element, '-2' to access the second last element and soo on.


Program

>>> num = ('zero', 'one', 'two', 'three')
>>> num[-1]
'three'
>>> num[-2]
'two'



Accessing elements in tuple (range)

In tuple, more elements can be accessed by specifying range.


Syntax / format :- 

tuple_name[start index: end index]


Start index :- start the selection from the index


End index :- till where the selection should be done.

Note:- this value is not included.


Program


>>> a = (1, 2, 3, 4, 5, 6)
>>> a[1:4]
(2, 3, 4)



If start index is not specified, then selection start from the beginning.

Program


>>> a = (1, 2, 3, 4, 5, 6)
>>> a[:4]
(1, 2, 3, 4)



If end index is not specified, then it will select till end.

Program


>>> a = (1, 2, 3, 4, 5, 6)
>>> a[2:]
(3, 4, 5, 6)



When both starting index and ending index is not specified, then whole tuple is returned.

Program


>>> a = (1, 2, 3, 4, 5, 6)
>>> a[:]
(1, 2, 3, 4, 5, 6)



Accessing elements in an interval.

Syntax / format :-

typle_name[start index: end index: steps]

Steps :- interval.

Program


>>> a = (1, 2, 3, 4, 5, 6)
>>> a[1:5:2]
(2, 4)



Interval selection without specifying start index and end index.

Program


>>> a = (1, 2, 3, 4, 5, 6)
>>> a[::2]
(1, 3, 5)



Range accessing with negative number in tuple

Selection of elements ranging last in a tuple, by using negative indexing.

Program


>>> a = (1, 2, 3, 4, 5, 6)
>>> a[-5:-1]
(2, 3, 4, 5)



Joining two or more tuples

Creating a new tuple by joining two or more tuples.

Use '+' operator to join tuples.


Joining two tuples.

Program


>>> a = (1, 2, 3)
>>> b = (4, 5, 6)
>>> c = a + b
>>> print(c)
(1, 2, 3, 4, 5, 6)



Joining more tuples.

Program


>>> st = ("Hello", "world")
>>> num = (1, 2, 3)
>>> boolean = (True, False)
>>> All = st + num + boolean
>>> print(All)
('Hello', 'world', 1, 2, 3, True, False)




Tuple length

Use 'len()' built-in function to know the length of a tuple.


Program


>>> tup = (1, 2, 3, 4, 5, True)
>>> len(tup)
6




Python tuple count() build-in function

Returns number of occurrences of an element in a tuple.


Program


>>> number = (1, 5, 6, 1, 2, 1, 4, 6, 1)
>>> number.count(1)
4
>>> word = ('hi', 'hello', 'hi', 'hi')
>>> word.count('hi')
3




Python tuple index() build-in function

The index(), tuple's built in function, returns the index of a value.


Program


>>> num = (0, 1, 2, 3, 4, 5)
>>> num.index(5)
5
>>> char = ('a', 'b', 'c', 'd', 'e', 'f')
>>> char.index('f')
5
>>> char.index('c')
2




Conclusion

Tuple is the collection of objects, which are ordered and immutable.

In this we have learned all the methods available with Tuple.





Previous                          Next

If you have any query, feel free to ask.

إرسال تعليق

If you have any query, feel free to ask.

Post a Comment (0)