TUPLES IN PYTHON

18 Likes Comment
Tuples in python

Hey Shouters!! Today we learn about all the important functions and operations supported by tuples in python.

In this tutorial, we will learn about all the important things about tuples. The main parts that we will learn about are:

Introduction
Creating Tuple
Comprehension and Generator
List and Dictionaries
Operations over tuple
Methods used in the tuple
Tuple and Function
Boolean and tuple
Short Note

Introduction

Tuples are group of values within first brackets i.e. (1,2,3,’str’,22.333).

Tuples and Strings are immutable whereas list and dictionaries are mutable.

Tuples are faster than lists.

Tuple can be a dictionary keys but list cannot.

Tuples can be used as values in sets whereas lists can not.

Creating Tuple

No. 1> There are a Tuple class and using tuple class constructor, you can create tuples as well as you can apply conversion like string to tuple, list to tuple and so on.

Tuple allows indexing, slicing operations just like list and String.

Here, iterable is a single parameter(optional). Only the iterable types of objects are passed. Does not return anything, but creates a tuple.

What does iterable means?

In simple words, ‘iterable’ means using a loop, values can be fetched. If ‘iterable’ is not passed, an empty tuple is created. If ‘bool’ or ‘int’ is passed then the “TypeError” message is generated.

No. 2> Using first brackets (), You can declare a tuple.
To create empty tuple use only first brackets but when creating single element tuple then must put a comma(‘,’) after the first value.

Tuples can also be created without parenthesis when more than one value is assigned into a variable.

COMPREHENSION AND GENERATOR

The word “Comprehension” means “a short and concise way to construct new sequences”.
The word “Generator” means “a thing that generates something”.

List Comprehension can be made as follows:

Here, we initialize the list using a loop and make a single line compressed statement.

Mainly, List comprehension is enclosed in square brackets [] while the Generator is enclosed in plain parentheses ().

The Generator expression allows us to create a generator without the yield keyword.

Generator expression can be made as follows :

Note: In a list comprehension, Python reserves memory for the whole list. Whereas, The generator generates one item at a time and generates an item only when in demand. Thus we can say that Generator expressions are memory efficient.

LIST AND DICTIONARIES

Even now we know surprisingly little about Dictionarie’s and Tuple’s.

Is it possible that tuples and dictionaries can work together?

Answer is Yes, Tuples and dictionaries can work together, although tuples can be used as dictionary keys but lists can never be used as dictionary keys, also tuple containing list is not a valid key. For example :

Be careful about dictionaries, values can be updatable.

Here, ‘cars’ is my dictionary and ‘company’ is used as a key in the dictionary and all company manufactured models are stored as a tuple value by inputted as ‘model’ data. After that Display all car models with company names.

OPERATIONS OVER TUPLE

Tuples can be extended using ‘+’ operator or replicated using ‘*’ operator.

Tuple supports ‘in’ and ‘not in’ operations.

A Tuple’s elements can be variables, not only literals.

A Tuple’s element can be expressions if they’re on the right side of the assignment operator.

Here, “t2” is an empty tuple after this “t2 = E_tuple * 3” operation, we get empty tuple.

METHODS USED IN THE TUPLE

COUNT METHOD

Returns the number of times a specified value occurs in a tuple.

INDEX METHOD

Searches the tuple for a specified value and returns the first position of where it was found.

LEN METHOD

The len() function accepts tuples as a parameter and returns the number of elements containing in the tuple.

ENUMERATE METHOD

The enumerate() function returns a tuple containing a count for every iteration (from the start which defaults to 0) and the values obtained from iterating over a sequence.

DEL METHOD

Removing individual tuple elements is not possible. To remove the entire tuple, use the ‘del’ method. There is no ‘pop’ method used in the tuple.

Check this program Carefully.

TUPLE AND FUNCTIONS

IS FUNCTION PARAMETER LIST IS A TUPLE?

Answer is No, Function parameter is not a tuple. If function parameters are tuple then, we must use tuple methods or tuple operation over it always, but we can use any kind of operation over function parameters. So, function parameters are not tuples.

You can pass tuple as an argument to a function, in those cases, some operations are restricted.

BOOLEAN AND TUPLE

Why is “True, True, True == (True, True, True)” gives “(True, True, False)”?

Answer: The result “(True, True, False)” is correct.
For equality operator, In left hand side last element is compared with the first element of the right hand side and the second last element on the left hand side is compared with the second element of the right side and so on.

So, For ‘True’ last element on the left-hand side is compared with ‘(True, True, True)’. Here, boolean is compared with a tuple, that’s why the answer is ‘False’. Remaining values (‘True’,’True’) are put as it is.

Short Note

A tuple is a collection of dissimilar variables or values.
Tuples can also be used to keep records, such as weather reporters use tuples for recording temperature over an hour/day/month.

You might like

SK. Saifuddin

About the Author: SK. Saifuddin

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami