Type Here to Get Search Results !

Write Python program to perform following operations on Tuples: Create Tuple, Access Tuple, Update Tuple, Delete Tuple

 Write Python program to perform following operations on Tuples: Create Tuple, Access Tuple, Update Tuple, Delete Tuple


A tuple is a sequence of immutable Python objects. Tuples are sequences, just like

lists. The differences between tuples and lists are, the tuples cannot be changed unlike

lists and tuples use parentheses, whereas lists use square brackets.


Write Python program to perform following operations on Tuples: Create Tuple, Access Tuple, Update Tuple, Delete Tuple


a) Creating a Tuple: Creating a tuple is as simple as putting different comma separated values. Optionally you can put these comma-separated values between

parentheses also.

#totalprogrammingcode

Example

tup1 = ('physics', 'chemistry', 1997, 2000);

tup2 = (1, 2, 3, 4, 5 );

tup3 = "a", "b", "c", "d";

The empty tuple is written as two parentheses containing nothing

tup1 = ();

To write a tuple containing a single value you have to include a comma, even though

there is only one value.

tup1 = (50,);

Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so

on.


b) Accessing Values in Tuples

To access values in tuple, use the square brackets for slicing along with the index or

indices to obtain value available at that index.

Example:

#!/usr/bin/Python

tup1 = ('physics', 'chemistry', 1997, 2000);

tup2 = (1, 2, 3, 4, 5, 6, 7 );

print "tup1[0]: ", tup1[0];

print "tup2[1:5]: ", tup2[1:5];

Output:

tup1[0]: physics

tup2[1:5]: [2, 3, 4, 5]


c) Updating Tuples: Tuples are immutable which means you cannot update or change

the values of tuple elements. You are able to take portions of existing tuples to create

new tuples.

Example:

#!/usr/bin/Python

tup1 = (12, 34.56);

tup2 = ('abc', 'xyz');

# Following action is not valid for tuples

# tup1[0] = 100;

# So let's create a new tuple as follows

tup3 = tup1 + tup2;

print tup3;

Output:

(12, 34.56, 'abc', 'xyz')


d) Delete Tuple Elements: Removing individual tuple elements is not possible. There

is, of course, nothing wrong with putting together another tuple with the undesired

elements discarded. To explicitly remove an entire tuple, just use the del statement.

Example:

#!/usr/bin/Python

tup = ('physics', 'chemistry', 1997, 2000);

print tup;

del tup;


Examples


1. Create a tuple and find the minimum and maximum number from it.


tuple = (20,30,50,70,90)

print("Maximum value from tuple is",max(tuple))
print("Minimum value from tuple is",min(tuple))

2. Write a Python program to find the repeated items of a tuple


tuple = (2,3,9,9,8,8,8,2,2,5,5,5,5)
print("Default tuple is :",tuple)
num = int(input("Choose the item from above tuple :"))
for i in range(len(tuple)):
    tuple1 = tuple[i]
    a =tuple.count(num)
print(num,"is repeated",a,"times")

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Below Post Ad