0% found this document useful (0 votes)
17 views15 pages

Module 3.2.2 Tuples PDF

Uploaded by

arjunkutappi
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)
17 views15 pages

Module 3.2.2 Tuples PDF

Uploaded by

arjunkutappi
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/ 15

Module 3

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).

► DECOMPOSITION AND MODULARIZATION* :- Problem decomposition as a strategy for


solving complex problems, Modularization, Motivation for modularization, Defining and
using functions in Python, Functions with multiple return values

Page 3
Python Tuple

Page 4
Introduction to Python Tuples

► Definition: A tuple is an ordered collection in Python used to store multiple


items in a single variable. It’s similar to a list but with one key difference:
tuples are immutable.

► Syntax: Defined using parentheses ( ). Items are separated by commas.

► Example: my_tuple = (1, 2, 3, "apple", "banana")

Page 4
Characteristics of Tuples

► Ordered: Tuples maintain the order of elements, so each item has a specific
index.

► Immutable: Once a tuple is created, its elements cannot be altered, making


them unchangeable.

► 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

► Empty Tuple: empty_tuple = ()


► Useful when you want a variable to hold an immutable empty collection.

► Single Element Tuple: single_element = (42,)


► Must include a comma after the element to differentiate from just using
parentheses.

► Multiple Elements: my_tuple = (10, 20, 30, "hello", "world")


► Any data types can be mixed within a tuple.

Page 6
Accessing Tuple Elements

► Indexing: Use the index number to access elements.


► Example: fruits = ("apple", "banana", "cherry"), fruits[1] returns "banana".

► Negative Indexing: Access elements from the end by using negative indices.
► Example: fruits[-1] returns "cherry".

► Slicing: Extract a subset of the tuple using : notation.


► Example: fruits[0:2] returns ("apple", "banana").

Page 7
Operations on Tuples

► Concatenation: Join two tuples using +.


► Example: tuple1 = (1, 2), tuple2 = (3, 4), result = tuple1 + tuple2 yields (1,
2, 3, 4).

► Repetition: Repeat elements in a tuple using *.


► Example: repeat_tuple = ("hello",) * 3 results in ("hello", "hello", "hello").

► Membership Test: Check if an item exists within a tuple using in.


► Example: "apple" in fruits returns True if "apple" is in fruits.

Page 8
Tuple Functions and Methods

► len(tuple): Returns the total number of elements.


► Example: len((1, 2, 3)) returns 3.

► max(tuple) and min(tuple): Find the largest or smallest element,


respectively (only for comparable elements).
► Example: max((1, 5, 3)) returns 5.

► tuple(): Converts other data structures (like lists) into tuples.


► Example: tuple([1, 2, 3]) returns (1, 2, 3).

Page 9
Advantages of Using Tuples

► Performance: Faster to process than lists due to immutability.

► 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

► Create a Tuple: Define a tuple named fruits


with the values "apple", "banana", "cherry",
"date".
► Access Elements:
► Print the first and last elements of the fruits tuple.
► Use negative indexing to print "cherry".

Page 11
Classroom Exercises

► Create a Tuple: Define a tuple named fruits # Create a Tuple


with the values "apple", "banana", "cherry", fruits = ("apple", "banana", "cherry", "date")
"date".
► Access Elements: # Access Elements
print(fruits[0]) # Output: apple
► Print the first and last elements of the fruits tuple.
print(fruits[-1]) # Output: date
► Use negative indexing to print "cherry".
print(fruits[-2]) # Output: cherry

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)

► Repetition: Repeat the tuple ("hello",) three # Repetition


times, and print the result. repeat_tuple = ("hello",) * 3
► Membership Test: Check if "banana" is print(repeat_tuple)
present in the fruits tuple from Exercise 1. # Output: ("hello", "hello", "hello")

# 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

person = ("Alice", 25,


► Create a tuple person = ("Alice", 25, "Engineer")
"Engineer").
► Unpack the tuple into three variables: name, age,
and profession. # Unpack into variables
► Print each variable to confirm the values. name, age, profession =
person

print(name)
# Output: Alice

print(age)
# Output:
Page 15
25

You might also like