Module 3
Module 3
Syllabus
● The code execution occurs sequentially in the same order in which they occur in the
program.
● Sometimes there is a need to implement logic to control the flow of execution based on
certain conditions or loop
● There are three types of control structures used in Python.
● They are:
1. Sequential
2. Selection
3. Repetition
● This structure is used for looping
● Repeatedly executing a certain piece of a code block.
Syntax:
if condition:
#Statements to execute if condition is true
Flowchart:
Example:
if 10 > 5:
print("10 greater than 5")
Output
10 greater than 5
if condition:
else:
#Executes this block if condition is false
● If the condition evaluates to true, the indented block of code under the if statement is
executed.
● Otherwise, the indented block of code under the else statement is executed.
Flowchart
Example
● The nested if statements in Python are the nesting of an if statement inside another if
statement with or without an else statement.
● There is an outer if statement, and inside it another if-else statement is present. These
type of statement is known as nested if statement.
Example
4. Elif Conditional Statements in Python
Syntax
Example
Flowchart
5. Ternary Statement (Short Hand If Else Statement)
● Whenever there is only a single statement to be executed inside the if block then
shorthand if can be used.
● The statement can be put on the same line as the if statemem
Syntax
The first option will be executed when the condition provided in the expression is True. If
the condition returns False, then option2 will be executed
Example:
a = 10
b=20
min = "a is minimum" if a < b else "b is minimum"
print(min)
Loops in Python
● In programming, loops are designed to execute a specified code block repeatedly.
● The python while statement is the simplest and the most basic iteration mechanism.
repeatedly executes a block of statements (usually indented) as long as the condition
evaluates to true.
● When the test condition becomes false, the control moves to the statement that follows
the while block.
● In case the test condition evaluates to false, to begin with, the body of the loop never run
and the while statement will be entirely skipped.
while expression:
Statement(s)
Flowchart:
2. For Loop in Python
● The for loops in Python are a special type of loop statement that is used for sequential
traversal.
● Python for loop is used for iterating over string, tuple, list, set, dictionary
● Python's for loop is designed to repeatedly execute a code block while iterating throup
list, tuple, dictionary, or other iterable objects of Python.
● In this case, the variable 'value' is used to hold the value of every item present in
sequence before the iteration begins until this particular iteration is completed.
Example
values = range(4)
For example,
Definition and Usage
● The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and stops before a specified number.
Syntax
Example
1. Lists in Python
List Characteristics
Lists are:
● Change the items of a list by assigning new values using the = operator
Remove an item from the list
● Can use the built in len() function to find the number of elements in a list
Iterating through a list
DECOMPOSITION AND
MODULARISATION
Task Assignment: By breaking a problem into smaller tasks, it becomes easier to assign specific
tasks to different processors or team members.
Simplification: Tackling smaller subproblems simplifies the development proces subproblem can
be solved more easily than addressing the entire problem at once
Modularisation
1. Ease of debugging
2. Reusable Code
3. Readability
4. Reliability
All these advantages add up to one big advantage: reliability. Code that is easier to read, easier
to debug, easier to maintain and easier to share will always run smoother with less errors. This
becomes necessary when working on extremely large projects, with hundreds of developers, all
of which have to either share code or work on code
1. Manageability
2. Team Programming
3. Quality Modularisation
4. Code is easier to text
5. Code is easier to test
6. Reusability
7. Easy collaboration
● It is a challenging task to combine all the modules. There is a need for extra time and
budget for a product in modular programming.
● Careful documentation is required so that other program modules are not affected.
● Some modules may partly repeat the task performed by other modules. Hence, Modular
programs need more memory space and extra time for execution
● Integrating various modules into a single program may not be a task because different
people working on the design of different modules may not have the same style.
● It reduces the program's efficiency because testing and debugging are time-consuming,
where each function contains a thousand lines of code.
Functions in Python
● A function declaration tells the compiler about a function's name, return type, and
parameters.
● It also tells the compiler that there is a function with the given name defined somewhere
else in the program.
● The function definition contains a function declaration and the body of a function.
● The body is a block of statements that perform the work of the function.
1. Built-in Functions These functions are pre-defined in Python and can be used directly
without any further declaration.
Example
mylist[1, 2, 3, 4, 5]
print(len(mylist))
Output: 5
2. User-defined Functions - These are functions that users create to perform specific tasks.
● We can define a function in Python, using the def keyword
Example
def greet ():
print('Hello World!')
3. Recursive Functions - These are functions that call themselves within their definition.
They help solve problem that can be broken down into smaller, similar problems.
Recursion Defined
● Recursion can be defined as the process in which a function calls itself repeatedly.
● Using a recursive algorithm, certain problems can be solved quite easily.
● Examples of such Recursion
● Towers of Hanoi (TOH),
● Inorder/Preorder/Postorder Tree Traversals
● DFS of Graph
● A complicated function can be split down into smaller sub-problems utilizing recursion.
● Sequence creation is simpler through recursion than utilizing any nested iteration.
● A lot of memory and time is taken through recursive calls which makes it expensive for
use..Recursive functions are challenging to debug.
● The reasoning behind recursion can sometimes be tough to think through.
Syntax:
def func():
(recursive call)
func()
Example
Reasons for using Recursion
● A call stack is a data structure used by computer programs to keep track of function
calls.
● When a function is called, a new frame is added to the call stack.
● This frame contains information about the function call, such as the function arguments
and the current position in the code
● When the function completes its execution, the frame is removed from the call stack, and
the program returns to the previous frame.
● The call stack operates in a Last-In-First-Out (LIFO) manner, which means that the last
function called is the first to be completed.
Overhead of Recursion
● Memory overhead
● Function call overhead
● Performance
● Readability and Maintainability
● Difficulty to debugging and understanding.