web analytics

Python Tuple – Complete Tutorial

Tuple Matching in Python is a technique for gathering the tuples by matching the second component in the tuples. It is accomplished by using a dictionary by checking the second component in each tuple in python programming. However, we can make new tuples by taking bits of existing tuples.

Tuple Syntax

Tup = ('Jan','feb','march')

To write a empty tuple, you want to write as two parentheses containing nothing-

tup1 = ();

For composing tuple for a single value, you want to include a comma, despite the fact that there is a single value. Additionally toward the end you really want to write semicolon as shown below.

Tup1 = (50,);

Tuple lists start at 0, and they can be concatenated, sliced, etc.

In this tutorial, we will learn-

  • Packing and Unpacking
  • Comparing tuples
  • Using tuples as keys in dictionaries
  • Deleting Tuples
  • Slicing of Tuple
  • Built-in functions with Tuple
  • Advantages of tuple over list

Tuple Assignment
Python has tuple task assignment feature which empowers you to allot more than each factor at a time. In here, we have assigned tuple 1 with the people data like name, father name, birth year, and so on and another tuple 2 with the qualities in it like number (1,2,3,… .,7).

For Example,

(name, father name, birth year, most loved film and year, profession, birthplace) = Sachin

Here is the code,

tup1 = ('Sachin', 'Satbir','1999','Terminator 1995', 'Actor','Siddharth Malhotra');
tup2 = (1,2,3,4,5,6,7);
print(tup1[0])
print(tup2[1:4])
  • Tuple 1 includes list of data of Sachin
  • Tuple 2 remembers list of numbers in it
  • We call the value for [0] in tuple and for tuple 2 we call the value in the range of 1 and 4
  • Run the code-It gives name Sachin for first tuple while for second tuple it gives number (2,3 and 4)

Packing and Unpacking

In packing, we place value into a new tuple while in unpacking we extricate those values once again into variables.

x = ("Basicittopic", 20, "Education") # tuple packing
(company, emp, profile) = x # tuple unpacking
print(company)
print(emp)
print(profile)

Comparing tuples

An comparison operator in Python can work with tuples.

The comparison begins with a first component of each tuple. If they don’t compare to =,< or > then, at that point, it continue to the second element, etc.

It begins with contrasting the first element from each of the tuples

Let’s study this with an example-

case 1

a=(5,6)
b=(1,4)
if (a>b):print("a is bigger")
else: print("b is bigger")

Case 2

a=(5,6)
b=(5,4)
if (a>b):print("a is bigger")
else: print ("b is bigger")

case 3

a=(5,6)
b=(6,4)
if (a>b):print("a is bigger")
else: print("b is bigger")

Case1: Comparison begins with a first component of each tuple. For this situation 5>1, so the output a is greater

Case 2: Comparison begins with a first component of each tuple. For this situation 5>5 which is inconclusive. So it continues to the next component. 6>4, so the result a is greater

Case 3: Comparison begins with a first component of each tuple. For this situation 5>6 which is misleading or false. So it goes into the else block and prints “b is greater.”

Using tuples as keys in dictionaries

Since tuples are hashable, and list isn’t, we should utilize tuple as the key if we really want to make a composite key to use in a dictionary.

Example: We would go over a composite key if we really want to make a phone directory that maps, first-name, last-name, pairs of phone numbers, Assuming that we have announced the variables as last and first number, we could write a dictionary task statement as displayed below:

directory[last,first] = number

Inside the sections, the expression is a tuple. We could use tuple task in a for loop to explore this dictionary.

for last, first in directory:

print first, last, directory[last, first]

This loop explores the keys in the directory, which are tuples. It allocates the elements of each tuple to last and first and afterward prints the name and comparing phone number.

Tuples and Dictionary

dictionary can return the list of tuples by calling items, where each tuple is a key value pair.

a = {'x':100, 'y':200}
b = list(a.items())
print(b)

Deleting Tuples

Tuples are changeless and can’t be erased. You can’t erase or eliminate things from a tuple. However, erasing tuple completely is conceivable by utilizing the keyword.

del

Slicing of Tuple

To fetch specific arrangements of sub-elements from tuple or list, we utilize this unique function called slicing. Slicing isn’t simply pertinent to tuple yet in addition for array and list.

x = ("a", "b","c", "d", "e")
print(x[2:4])

The output of this code will be (‘c’, ‘e’).

Here is the Python 2 Code for all above Example

tup1 = ('Sachin', 'Satbir','1995','Terminator 1995', 'Actor','Siddharth Malhotra');
tup2 = (1,2,3,4,5,6,7);
print tup1[0]
print tup2[1:4]
Packing and Unpacking
x = ("Basicittopic", 20, "Education") # tuple packing
(company, emp, profile) = x # tuple unpacking
print company
print emp
print profile
Comparing tuples
case 1
a=(5,6)
b=(1,4)
if (a>b):print "a is bigger"
else: print "b is bigger"
case 2
a=(5,6)
b=(5,4)
if (a>b):print "a is bigger"
else: print "b is bigger"
case 3
a=(5,6)
b=(6,4)
if (a>b):print "a is bigger"
else: print "b is bigger"
Tuples and dictionary
a = {'x':100, 'y':200}
b = a.items()
print b
Slicing of Tuple
x = ("a", "b","c", "d", "e")
print x[2:4]

Built-in functions with Tuple

To perform different assignment, tuple permits you to utilize many built-in functions like all(), any(), enumerate(), max(), min(), sorted(), len(), tuple(), and so on

Advantages of tuple over list

  • Repeating through tuple is quicker than with list, since tuples are permanent.
  • Tuples that comprise of permanent components can be utilized as key for dictionary, which is not possible with list
  • if you have information that is permanent, implementing it as tuple will ensure that it remains write safeguarded

Related Posts

Leave a Reply

Your email address will not be published.