Module – III (1)
Module – III (1)
Compiled By
Mr. Shyam Krishna K
Assistant Professor
Dept. of AI & DS
Muthoot Institute of Technology & Science
[email protected]
Syllabus (10 Hours)
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
RECURSION:- Recursion Defined, Reasons for using Recursion,
The Call Stack, Recursion and the Stack, Avoiding Circularity in
Recursion,
The statement
odd = [num for num in numbers if num%2 != 0]
essentially directs the interpreter to iterate through the list numbers
and copy all odd numbers to another list odd.
• You can also replace a single list item with a new list.
Replacing elements
• You can use the slice operator to replace a single element or
multiple elements in a list.
• In the above example, the white space where you split the string is
known as the delimiter.
• If you want to specify a different delimiter, you can pass that
character (or even a string) as an argument to the split()
method.
• The keyword del can be used to delete the set itself, that is, after
the del operation, the set will not exist anymore. Any subsequent
access to the set will throw an error.
• Here Name, Age and Class are keys whereas Ram, 18 and S1 are
the corresponding values.
• An empty dictionary without any items is written as {}.
Creating arrays
• To create an array, you use the array() method of numpy package
as shown below:
Function Body
def countdown(n):
if n == 0: # Base case
print(“Hello All!")
else:
print(n)
countdown(n - 1) # Recursive call
def sum_numbers(n):
if n == 0: # Base case
return 0
else:
# Progressing towards base case
return n + sum_numbers(n - 1)
• Progress Toward the Base Case: Ensure that each recursive call moves
the problem closer to the base case. For instance, decrementing n in
factorial(n - 1) ensures progress.
• Test Your Function Thoroughly: Check edge cases to verify that the
base case is reached under all conditions.