0% found this document useful (0 votes)
5 views3 pages

Tuples 06.06.2025

A tuple is an ordered and immutable collection that can hold different data types. You can access tuple items using index numbers and perform operations like slicing, converting to a list for modification, and adding tuples together. Tuple methods include count() for occurrences and index() for finding positions of values.

Uploaded by

debayankar23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Tuples 06.06.2025

A tuple is an ordered and immutable collection that can hold different data types. You can access tuple items using index numbers and perform operations like slicing, converting to a list for modification, and adding tuples together. Tuple methods include count() for occurrences and index() for finding positions of values.

Uploaded by

debayankar23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

TUPLE

1 A tuple is a collection which is ordered and immutable.


2 t=tuple()
t=() creating empty tuple
3 mytuple = ("apple", "banana", "cherry")
4 thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
5 A tuple can contain different data types:
tuple1 = ("abc", 34, True, 40, "male")
6 You can access tuple items by referring to the index number, inside square
brackets:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
7 thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])
8 thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[:4])
9 thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:])
10 Convert the tuple into a list to be able to change it:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
11 Add tuple to a tuple.
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple)
12 fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
13
Loop Through a Tuple

thistuple = ("apple", "banana", "cherry")


for x in thistuple:
print(x)
14
thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
print(thistuple[i])
15 thistuple = ("apple", "banana", "cherry")
i=0
while i < len(thistuple):
print(thistuple[i])
i=i+1
16
Tuple Methods

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 whe

You can access tuple items by referring to the index number, but what is the
index number of the first item?

You cannot change the items of a tuple, but there are workarounds. Which of the
following suggestion will work?
Convert tuple into a list, change item, convert back into a tuple.
Convert tuple into a set, change item, convert back into a tuple.
Convert tuple into a dictionary, change item, convert back into a tuple.

fruits = ('apple', 'banana', 'cherry')


(x, y, z) = fruits
print(y)

What is a correct syntax for looping through the items of a tuple?


for x in ('apple', 'banana', 'cherry'):
print(x)
for x in ('apple', 'banana', 'cherry')
print(x)
foreach x in ('apple', 'banana', 'cherry')
print(x)

If a = (5, 4, 3, 2, 1, 0) evaluate the following expression:


a[a[-1]]
Slicing
tuplex = (2, 4, 3, 5, 4, 6, 7, 8, 6, 1)
tuplex[3:5]
tuplex[:6]
tuplex[5:]
tuplex[:]
tuplex[-8:-4]
tuplex[2:9:2]
tuplex[::4]
tuplex[9:2:-4]

You might also like