Algorithmic thinking with python
Algorithmic thinking with python
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).
Page 3
1
26-11-2024
Python Tuple
Page 4
Page 4
2
26-11-2024
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
Page 6
3
26-11-2024
► Negative Indexing: Access elements from the end by using negative indices.
► Example: fruits[-1] returns "cherry".
Page 7
Operations on Tuples
Page 8
4
26-11-2024
Page 9
► 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
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)
# 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
print(age)
# Output: 25
print(profession)
# Output: Engineer
Page 14
7
26-11-2024
Page 15
8
26-11-2024
9
26-11-2024
Page 19
10
26-11-2024
11