HOW TO USE TUPLE IN PYTHON?

286 viewsPYTHONPython
0

HOW TO USE TUPLE IN PYTHON?

Saraswat World Changed status to publish June 16, 2023
0
TUPLE

Aย Tupleย is an ordered and unchangeable collection of python objects, each object is separated by a comma and the whole set is enclosed by parenthesis. Tuples allow duplicate values.

PROPERTIES OF TUPLE

Tuple items or objects are ordered, unchangeable, and allow duplicate values.

Tuple items are indexed, the first item has an index [0], the second item has an index [1], etc.

The items of a tuple are arbitrary Python objects. Tuples of two or more items are formed by comma-separated lists of expressions.

s = ('ABC', 100, 2.5)

Sometimes the () is omitted in the syntax.

s = 'ABC', 100, 2.5

A tuple of one item (a โ€˜singletonโ€™) can be formed by affixing a comma to an expression (an expression by itself does not create a tuple, since parentheses must be usable for grouping of expressions).

t2 = ('ABC', )        # A 1-item tuple

An empty tuple can be formed by an empty pair of parentheses.

t1 = ()                # An empty tuple
TUPLE UNPACKING

To use the tuple elsewhere, you can unpack its parts into variables.

name, quantity, price = s
print('Cost', quantity * price)

The number of variables on the left must match the tuple structure.

name, quantity = s     # ERROR
Traceback (most recent call last):
...
ValueError: too many values to unpack
TUPLE CONSTRUCTOR

tuple()โ€“
It is also possible to use the tuple() constructor to make a tuple.

Example
Using the tuple() method to make a tuple:

Code

fruits = tuple(("apple", "banana", "cherry")) 
print(fruits)

Output

('apple', 'banana', 'cherry')
MULTIPLY TUPLES

If you want to multiply the content of a tuple a given number of times, you can use theย *ย operator:

Example
Multiply the fruits tuple by 2:

Code

fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2
print(mytuple)

Output

('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
TUPLE METHODS

Python has two built-in methods that you can use on tuples.

Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was found
HOW TO USE COUNT() IN PYTHON?

Code

a = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = a.count(5) #returns no. of times 5 occurs in tuple a
print(x)

Output

2
HOW TO USE INDEX() IN PYTHON?

Theย index()ย method finds the first occurrence of the specified value.

Theย index()ย method raises an exception if the value is not found.

Code

#Search for the first occurrence of the value 7, and return its position:

a = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = a.index(7)
print(x)

Output

2
DIFFERENCE BETWEEN TUPLE AND LIST

The difference between a tuple and a list is that a tuple is immutable whereas a list is mutable. We can store any type of data in Tuple.

Example of Tuple Syntax in Python-

Code
a=(5,10) ย  ย  ย  ย  ย  ย ย ย ย ย ย ย #values in the tuple are surrounded by parenthesis ()
print(a) ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย  #tuple is aย collection of values
b=(1,2.50,โ€helloโ€) ย ย #tuple as collection of different types of values
print(b) ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย #print tuple
print(b[1]) ย  ย  ย  ย  ย ย ย ย ย ย #provide the value of 2ndย element having index 1
print(len(b)) ย  ย  ย  ย ย ย ย #provide the length
print(b[0:2]) ย  ย  ย  ย ย ย ย #slice the tuple
Output
(5, 10)
(1, 2.5, โ€˜helloโ€™)
2.5
3
(1, 2.5)

Example of Immutability of Tuple-

b=(1,2.50,โ€helloโ€)

b[1]=4.50 ย ย ย ย ย ย  #it will show error, as tuples are immutable

Example of type casting and concatenation of Tuple-

Code
list1=[1,3.5,โ€helloโ€] ย ย #variable of list type
list2=[4,2.1,8] ย  ย  ย  ย ย #variable of list type
print(list1) ย  ย  ย  ย  ย ย #printing variable list1
print(type(list1))
tuple1=tuple(list1) ย  ย #type casting of list variable to tuple
tuple2=tuple(list2) ย  ย #type casting of list variable to tuple
print(type(tuple1))
ctup=tuple1+tuple2 ย #concatinating tuple variables
print(ctup)
print(ctup[3]) ย  ย  ย  ย  ย ย #print 4th element,its immutable so ctup[3]=5 gives error
a=(4) ย  ย  ย  ย  ย  ย  ย  ย  ย ย #it will be treated as expression having int value
print(type(a)) ย  ย  ย  ย  ย ย #value of a is enclosed in () but missing comma
a=(4,) ย  ย  ย  ย  ย  ย  ย  ย  ย ย #a singleton tuple is defined by adding comma as suffix
print(type(a))
Output
[1, 3.5, โ€˜helloโ€™]
<class โ€˜listโ€™>
<class โ€˜tupleโ€™>
(1, 3.5, โ€˜helloโ€™, 4, 2.1, 8)
4
<class โ€˜intโ€™>
<class โ€˜tupleโ€™>
Saraswat World Changed status to publish June 16, 2023
Write your answer.

Posted

in

Tags:

Comments

Leave a Reply

Ganesh Ji Arti A Tribute to Dilip Kumar