0% found this document useful (0 votes)
20 views6 pages

Tuple Programs

The document defines and demonstrates the use of tuples in Python. Tuples are defined using parentheses and can contain a mix of data types. They are immutable, but can contain mutable elements like lists. Tuples support operations like indexing, slicing, concatenation, and membership testing. They can be iterated over in for loops and their values counted, indexed, etc. using built-in functions.

Uploaded by

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

Tuple Programs

The document defines and demonstrates the use of tuples in Python. Tuples are defined using parentheses and can contain a mix of data types. They are immutable, but can contain mutable elements like lists. Tuples support operations like indexing, slicing, concatenation, and membership testing. They can be iterated over in for loops and their values counted, indexed, etc. using built-in functions.

Uploaded by

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

TUPLES – PYTHON PROGRAMS

1. DEFINING TUPLE
my_tuple = () # empty tuple-Output: ()
print(my_tuple)
my_tuple = (1, 2, 3) # tuple having integers-Output: (1, 2, 3)
print(my_tuple)
my_tuple = (1, "Hello", 3.4) # tuple with mixed datatypes-Output: (1,
"Hello", 3.4
print(my_tuple)
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3)) # nested tuple- Output:
("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
# tuple can be created without parentheses - also called tuple packing
my_tuple = 3, 4.6, "dog"
print(my_tuple) # Output: 3, 4.6, "dog"
# tuple unpacking is also possible
a, b, c = my_tuple
print(a) #3
print(b) # 4.6
print(c) # dog

Output:
()
(1, 2, 3)
(1, 'Hello', 3.4)
('mouse', [8, 4, 6], (1, 2, 3))
(3, 4.6, 'dog')
3
4.6
dog

2. TUPLE WITH TYPE FUNCTION


# only parentheses is not enough
my_tuple = ("hello")
print(type(my_tuple)) # Output: <class 'str'>

# need a comma at the end


my_tuple = ("hello",)
print(type(my_tuple)) # Output: <class 'tuple'>

# parentheses is optional
my_tuple = "hello",
print(type(my_tuple)) # Output: <class 'tuple'>

Output:
<class 'str'>
<class 'tuple'>
<class 'tuple'>

3. NESTED TUPLE AND INDEX VALUES


my_tuple = ('p','r','o','g','r','a','m')
print(my_tuple[0]) # Output: 'p'
print(my_tuple[5]) # Output: 't'

# index must be in range otherwise you will get an error.


# IndexError: list index out of range
#print(my_tuple[9])

# index must be an integer otherwise you will get an error.


# TypeError: list indices must be integers, not float
#my_tuple[2.0]

n_tuple = ("mouse", [8, 4, 6], (1, 2, 3)) # nested tuple


print(n_tuple[0][3]) # nested index- Output: 's'
print(n_tuple[1][2]) # Output: 4
Output:
p
a
s
6

4. SLICING OPERATOR IN TUPLE


my_tuple = ('p','e','r','m','i','t')
print(my_tuple[-1]) # Output: 't'
print(my_tuple[-6]) # Output: 'p'
my_tuple = ('p','r','o','g','r','a','m','i','z')
print(my_tuple[1:4]) # elements 2nd to 4th-Output: ('r', 'o', 'g')
print(my_tuple[:-7]) # elements beginning to 2nd-Output: ('p', 'r')
print(my_tuple[7:]) # elements 8th to end-Output: ('i', 'z')
print(my_tuple[:]) # elements beginning to end- Output: ('p', 'r', 'o', 'g', 'r',
'a', 'm', 'i', 'z')

Output:
t
p
('r', 'o', 'g')
('p', 'r')
('i', 'z')
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

5. CHANG IN VALUES
my_tuple = (4, 2, 3, [6, 5])
# we cannot change an element in tuple
# TypeError: 'tuple' object does not support item assignment
#my_tuple[1] = 9

# but item of mutable element can be changed


my_tuple[3][0] = 9
print(my_tuple) # Output: (4, 2, 3, [9, 5])

# tuples can be reassigned


my_tuple = ('p','r','o','g','r','a','m','i','z')
print(my_tuple) # Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

Output:
(4, 2, 3, [9, 5])
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

6. CONCATNATION AND DEL KEYWORDS


# Concatenation
print((1, 2, 3) + (4, 5, 6)) # Output: (1, 2, 3, 4, 5, 6)
print(("Repeat",) * 3) # Output: ('Repeat', 'Repeat', 'Repeat')

my_tuple = ('p','r','o','g','r','a','m','i','z')
print(my_tuple)
# can't delete items
# TypeError: 'tuple' object doesn't support item deletion
#del my_tuple[3]

# can delete entire tuple


# NameError: name 'my_tuple' is not defined
#del my_tuple
#print(my_tuple)

Output:
(1, 2, 3, 4, 5, 6)
('Repeat', 'Repeat', 'Repeat')
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

7. COUNT AND INDEX FUNCTION AND MEMBERSHIP OPERATOR


my = ('a','p','p','l','e',)
print(my.count('p')) # Output: 2
print(my.index('e')) # Output: 4
#Tuple membership test
my = ('a','p','p','l','e',)
print('a' in my ) # Output: True
print('b' in my ) # Output: False
print('g' not in my) # Output: True

Output:
2
4
True
False
True

8. FOR LOOP WITH TUPLE


for name in ('John','Kate','Mick','harry'):
print("Hello",name)

Output:
Hello John
Hello Kate
Hello Mick
Hello harry

9. ALL FUNCTIONS AND OPERATOR


tup1 = ('Rohan', 'Physics', 21, 69.75)
tup2 = (1, 2, 3, 4, 5)
tup3 = ("a", "b", "c", "d","a","d","g")
tup4 = (25.50, True, -55, 1+2j)
tup5= ("mouse", [8, 4, 6], (1, 2, 3))
print(tup1) #('Rohan', 'Physics', 21, 69.75)
print(tup2) #(1,2,3,4,5)
print(tup3) #('a', 'b', 'c', 'd', 'a', 'd', 'g')
print(tup4) #(25.5, True, -55, (1+2j))
print(tup5) #('mouse', [8, 4, 6], (1, 2, 3))
print(tup3[2]) #c
print(tup3[-2]) #d
print(tup3[1:4]) #('b', 'c', 'd')
print(tup3[:-3]) #('a', 'b', 'c', 'd')
print(tup3[2:]) #('c', 'd', 'a', 'd', 'g')
print(tup3.count('a')) #2
print(tup3.count('b')) #1
print(tup3.index('c')) #2
print("c" in tup3) #True
print("p" in tup3) #False
print("a" not in tup3) #False
print("p" not in tup3) #True

Output:
('Rohan', 'Physics', 21, 69.75)
(1, 2, 3, 4, 5)
('a', 'b', 'c', 'd', 'a', 'd', 'g')
(25.5, True, -55, (1+2j))
('mouse', [8, 4, 6], (1, 2, 3))
c
d
('b', 'c', 'd')
('a', 'b', 'c', 'd')
('c', 'd', 'a', 'd', 'g')
2
1
2
True
False
False
True

You might also like