0% found this document useful (0 votes)
2 views

Algorithmic thinking with python

The document provides an overview of Python programming concepts, focusing on selection and iteration, sequence data types, and the use of tuples. It details tuple characteristics, creation, accessing elements, operations, functions, and advantages, along with classroom exercises for practical understanding. Additionally, it presents two problems related to inventory and employee management systems that require the use of tuples.

Uploaded by

nanoclassroom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithmic thinking with python

The document provides an overview of Python programming concepts, focusing on selection and iteration, sequence data types, and the use of tuples. It details tuple characteristics, creation, accessing elements, operations, functions, and advantages, along with classroom exercises for practical understanding. Additionally, it presents two problems related to inventory and employee management systems that require the use of tuples.

Uploaded by

nanoclassroom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

26-11-2024

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

1
26-11-2024

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

2
26-11-2024

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

3
26-11-2024

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.


► fruits = ("apple", "banana", "cherry")
► Example: "apple" in fruits returns True if "apple" is in fruits.

Page 8

4
26-11-2024

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

5
26-11-2024

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 11

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 12

6
26-11-2024

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.
► Slice the tuple to print the last two colors. print(colors[0:3])
► 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 13

Classroom Exercises

person = ("Alice", 25, "Engineer")


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

print(age)
# Output: 25

print(profession)
# Output: Engineer

Page 14

7
26-11-2024

Problem 1: Inventory Management System for a Store

► A retail store wants to create an inventory system that stores each


item as a tuple containing:
► The item name (string)
► The quantity in stock (integer)
► The unit price (float)
► The store needs to:
► Calculate the total value of the stock for each item.
► Find the item with the highest value in stock.
► Write code to:
► Define a list of tuples to represent the inventory.
► Calculate and display the total stock value for each item.
► Find and print the item with the highest total stock value.

Page 15

8
26-11-2024

9
26-11-2024

Problem 2: Employee Management System

► A company wants to manage its employee information and includes


each employee as a tuple containing:
► Employee name (string)
► Department (string)
► Monthly salary (float)
► The company needs to:
► Display all employees who have a monthly salary above a specified threshold.
► Find the total annual payroll expense for all employees in the company.
► Write code to:
► Define a list of tuples with employee information.
► Display employees earning above a salary threshold.
► Calculate and display the total annual payroll expense.

Page 19

10
26-11-2024

11

You might also like