tuple operations in python with examples - Python

tuple operations in python with examples

Hello Guys welcome to our new blog if you searching about tuple operations in python then you are on right place.tuple operations in python

What are tuple operations in python?

Since Tuple is Python’s data type, we can perform several operations on the data represented as Tuple. The operations commonly performed on Tuple are
  1. Combing the tuple or Joining the tuple
  2. Replicating the tuple
  3. Slicing the tuple
  4. Unpacking the tuple

Combining the Tuples:

  1. Also known as joining the Tuples,combining the Tuples is simply adding two or more Tuples to create a new. The concatenation operator  ‘+’ is used to join two Tuples.
  1. The ‘+’ operator requires that both the operands must be of Tuple types. Thus we cannot add  number or string to a Tuple. We can create a new Tuple. We can create a new Tuple by adding two or more Tuples.
  2. Syntax: <tuple1>+<tuple2>

Example : print(ageB+ageA)

Example will join Tuple ‘ageA’ and ‘ageB’. The resultant Tuple will be displayed on screen.

Example-2: ageC=ageA+ageB

This example will join Tuple ‘ageA’ and ‘ageB’. The resultant Tuple will stored by the new name ‘ageC’.

Q) Write a program to create two tuples areaA and areaB with the names of 5-5 salesman. Add both the Tuples into one and finally print the combined Tuple.

areaA=(‘Raj’,’Harsh’,’Anu’,’Abhinav’,’Abhiraj’)

areaB=(‘Shubh’,’vinay’,’yogya’,’Anushul’,’Abhinash’)

zone=areaA+areaB

print(“\nFirst Tuple:”,areaA)

print(“\nSecond Tuple:”,areaB)

print(“\nFinal Tuple:” zone)

tuple operations in python Replicating the Tuple:

  1.  The arithmetic operator of MULTIPLICATION ‘*’ is used to replicate the Tuples up to specified number of times.
  2. The ‘*’ operator requires that one operand must be of Tuple types and the other mus be an integer. We can create a new Tuple by replicating a Tuple up to a specified number of times.

Syntax :<tuple name>*<integer>

Example : print(marks*3)

The above statement will print a new tuple by replicating elements

of Tuple ‘marks’ three times.

What is tuple in Python with example

what is slicing in python with example

Slicing the Tuple

Slicing the Tuple is the process of extracting some part of the Tuple out of it. In other words, it is the process of creating sub-list from a main Tuple . We use indexes of the Tuple elements to create a slice

syntax

<tuple name>[start: stop: step]

Here <Tuple name> is name of the Tuple to be sliced. <start> represent the index from where extraction is to begin. The default of <start> is 0(zero). <stop> is the index numbers up to which extraction will take place. Actually extraction takes place up to <stop>-1 i.e. element at position <stop> index is not included in slice. The default of <stop> is the end of Tuple. <step> is used when all the numbers between <start> and <stop> are not to be sliced. <step> represent the number by

num=(11, 22, 33, 44, 55, 66, 77, 88, 99)

print(num[1:7:2])#[22,44,66]

Unpacking tuples in python example

Unpacking the tuple is the process of breaking the tuple as sequence and spliting its elements individually. Each element is than separate variable specified.

In the process of unpacking the tuple number of variable must be same as number of element in tuple and separated by comma.

Syntax

<list of variable>=<tuple name>

t=(1,shubh,99)

x,y,z=t

print(x)#1

print(z)#99

Also check our latest uploads

what is numpy in python

top digital marketing agency in delhi

Join whatsapp Group for notes and other information
Join

Subscribe Our Youtube Channel for Live classes

https://www.youtube.com/@olevelguruji

Leave a Comment