7 C
Jaipur
Wednesday, January 27, 2021

How to work with Python Tuples? – Linux Hint

Must read

In this article, we are going to discuss tuples, an ordered and immutable data type (read only). It can have similar or different data type elements, and is declared in parenthesis ().

Syntax:

t = (1,2,0.5,’jki’,’hi’,6)

We need to take special care when we declare tuple with single element.

If we omit comma (,) here, it will be a normal integer variable.

In the first example, the type is tuple.

In the second example, the type is integer.

Other way of declaring a tuple:

How to enter into Python interpreter?

Open Linux terminal and type “python”, then hit enter so we will see python interpreter. For python3+ version, type “python3”, these are the following info we are going to see on the terminal. If we want to check python version, type “python -v”.

Output:

Python 3.5.0 (default, Sep 20 2019, 11:28:25)
[GCC 5.2.0] on Linux

Type “help”, “copyright”, “credits”, or “license” for more information.

The following operations can be performed on tuple:

Tuple Slice

This is useful when we want only part of the tuple.

Note: Tuple index always starts from 0. Tuple can be traversed in forward and reverse direction (using negative index).

Example:

t = (1,2,3,4,’hi’,’good’,10.5)
Forward traverse index: [0,1,2,3]
reverse traverse index: [,3,2,1] here t[1]=10.5, t[2]=”good”,
 t[3]=”hi”, t[4]=4,

Syntax:

variablename[start:stop:step].

Here, stop is excluded. If we provide only start, it will extract all the elements from start to end of tuple. If we provide only stop, it will extract from 0th index to stop index. We can omit both start and stop, in that case, we need to provide at least colon (t[:]). If we don’t provide step value default, the value will be 1.

Ex:

t = (1,2,3,4,5,’i’,’hi’,10.5)

In this example, we would want to extract elements “1,2,3,4”.

Suppose we want to extract elements “3,4,5,’i’,’hi’,10.5”

Suppose we want to extract elements “2,3,4,5,’I’,’hi’ ” (using reverse index)

Suppose we want to reverse a tuple

Nested Tuples

We can declare tuple in a tuple, i.e., nested tuples.

t = (1,2, (3,4,5),6,7,(‘a’,’b’,’c’))

Consider nested tuple as another tuple and its index also starts from 0.

We can access nested tuples elements as below:

  1. Find nested tuple index in main tuple
  2. Find nested tuple index

Ex:

In the example below, we want extract “3” from nested tuple. Here, the main tuple index is “t[2]”, and nested tuple “(3,4,5)” index is “0”. So, the final expression is “t[2][0]”.

In the second example, we extracted “b” from nested tuple using expression “t[5][1]”.

Length

This method returns number of elements in tuple.

Syntax:

Access tuple by element using loop

Syntax:

For variable in tuple variable:

Repetition

This is useful when we want to repeat the tuple for given number.

Syntax:

variable * number of times repetition

Example:

Here, the tuple is repeated 2 times, as shown below.

Concatenation

This is concatenate or combination of 2 tuples.

Syntax:

Search element in a tuple

This return “True” if element found in tuple returns as “False”. The membership operators “in” and “not in” are used to check this.

Syntax:

Element in tuple
Element not in tuple

Index

This method is used to find the index of element in tuple. If found returns as “index of the element” then value error exception is raised.

Syntax:

variable.index(element, beg=0,end=len(string))

Count

This method is used to count occurrence of element in tuple.

Syntax:

Delete tuple

We can’t remove individual elements from tuples since it is immutable. But we can delete entire tuple.

Syntax:

In the above example, we declared tuple t and printed t. After that, we deleted a tuple using “del t” and tried to print tuple. It throws nameerror exception because “tuple t” doesn’t exist.

Minimum

This method is used to find minimum value of element in a tuple.

Syntax:

Maximum

This method is used to find minimum value of element in a tuple.

Syntax:

Compare 2 tuples

This method is used to compare elements of 2 tuples.

  1. Return 0 if elements of both tuples are equal
  2. Return 1 if elements of the first tuple are greater than the second tuple
  3. Return -1 if elements of the first tuple are less than the second tuple

Syntax:

If elements types are mismatched, then element is converted to int type.

Tuples are compared index by index. The 1st element of the 1st tuple is compared to the 1st element of the 2nd tuple. If they are not equal, this is the result of the comparison, else the 2nd element is considered, then the 3rd element, and so on.

Conclusion

Tuple is immutable data type, and any operation we perform should be stored in another tuple variable. It is faster compared to the other data types (ex: list, dictionary). Since tuple is immutable in our program, the data is not going to change the entire software life cycle, we can use tuple like system configuration data.

The above is most commonly and generally used operation on tuple. If we want to check what all the operations are supported for tuple, type dir(tuple) on interpreter and hit enter. It will display all methods/function. If we want to check documentation for tuple method/function, type help(tuple) and hit enter.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article