Module 3.2.2 Tuples PDF
Module 3.2.2 Tuples PDF
Page 2
Module 3
► SELECTION AND ITERATION USING PYTHON:- if-else, elif, for loop, range, while loop.
► SEQUENCE DATA TYPES IN PYTHON -list, tuple, set, strings, dictionary, Creating and
using Arrays in Python (using Numpy library).
Page 3
Python Tuple
Page 4
Introduction to Python Tuples
Page 4
Characteristics of Tuples
► Ordered: Tuples maintain the order of elements, so each item has a specific
index.
► Allows Duplicates: Tuples can hold multiple instances of the same value,
e.g., (1, 1, 2, 3).
► Supports Mixed Data Types: Tuples can contain a mix of integers, floats,
strings, or other data types.
Page 5
Creating Tuples in Python
Page 6
Accessing Tuple Elements
► Negative Indexing: Access elements from the end by using negative indices.
► Example: fruits[-1] returns "cherry".
Page 7
Operations on Tuples
Page 8
Tuple Functions and Methods
Page 9
Advantages of Using Tuples
► Data Integrity: Useful when data should not be modified after creation,
such as in database records.
► Dictionary Keys: Since tuples are immutable, they can be used as keys in
dictionaries, unlike lists.
► Code Readability: Tuples can signify that data is constant and should not be
changed, making code easier to read and understand.
Page 10
Classroom Exercises
Page 11
Classroom Exercises
Page 12
Classroom Exercises
# Concatenation
even_numbers = (2, 4, 6)
► Concatenation:
odd_numbers = (1, 3, 5)
► Create two tuples even_numbers =(2, 4, 6) and
numbers = even_numbers + odd_numbers
odd_numbers =(1, 3, 5).
print(numbers)
► Concatenate these tuples and store the result in a
new tuple numbers.
# Output: (2, 4, 6, 1, 3, 5)
# Membership Test
print("banana" in fruits)
# Output: True
Page 13
Classroom Exercises
► Define a tuple colors = ("red", "green", colors = ("red", "green", "blue", "yellow", "orange")
"blue", "yellow", "orange"). # Slicing examples
► Slice the tuple to print the first three colors.
print(colors[0:3])
► Slice the tuple to print the last two colors.
► Slice the tuple to print colors from the second to # Output: ("red", "green", "blue")
the fourth element.
print(colors[-2:])
# Output: ("yellow", "orange")
print(colors[1:4])
# Output: ("green", "blue", "yellow")
Page 14
Classroom Exercises
print(name)
# Output: Alice
print(age)
# Output:
Page 15
25