Python Module2 Complete
Python Module2 Complete
Page 2
Module 2
Page 3
Algorithms and pseudocodes
Page 4
Algorithms and pseudocodes
► Algorithm
► A step-by-step procedure for solving a problem.
► Uses pure English phrases or sentences.
► Pseudocode
► A high-level representation of an algorithm.
► Uses a mix of natural language and programming-like syntax.
► More structured than algorithms.
Page 5
Pseudocodes
Pseudocode
Algorithm
Page 6
Why pseudocodes?
Wondering why pseudocodes are important? Here are a few motivating reasons:
► Ease of understanding: Since the pseudocode is programming language independent,
novice developers can also understand it very easily.
► Focus on logic: A pseudocode allows you to focus on the algorithm’s logic without
bothering about the syntax of a specific programming language.
► More legible: Combining programming constructs with English phrases makes
pseudocode more legible and conveys the logic precisely.
► Consistent: As the constructs used in pseudocode are standardized, it is useful in sharing
ideas among developers from various domains.
► Easy translation to a program: Using programming constructs makes mapping the
pseudocode to a program straightforward.
► Identification of flaws: A pseudocode helps identify flaws in the solution logic before
implementation.
7
Difference between Algorithm and Pseudocode
Page 8
Constructs of a pseudocode
Page 9
Constructs of a pseudocode
Page 10
Constructs of a pseudocode - Sequence
► This is the most elementary construct where the instructions of the
algorithm are executed in the order listed.
► It is the logical equivalent of a straight line.
► Consider the code below.
Page 11
Constructs of a pseudocode - Sequence
► Example: Write a pseudocode to find the area of a square
1. Start
2. Read the side length of the square
3. area: area = side_length * side_length
4. Print the area
5. End
Page 12
Constructs of a pseudocode - Decision or Selection
Page 13
Constructs of a pseudocode - Decision or Selection
► Simple If
► The general form of this structure is
Page 14
Constructs of a pseudocode - Decision or Selection
► if .. else structure
► The general form of this structure is
► This structure contains two blocks of statements.
► If the test condition is met, the first block (denoted by
true_instructions) is executed and the algorithm skips over the
second block (denoted by false_instructions).
► If the test condition is not met, the first block is skipped and only
the second block is executed.
Page 15
Constructs of a pseudocode - Decision or Selection
Page 17
Constructs of a pseudocode - Case Structure
Page 18
Constructs of a pseudocode - Case Structure
Page 19
Constructs of a pseudocode - Case Structure
Page 20
Constructs of a pseudocode - Case Structure
Page 21
Constructs of a pseudocode - Case Structure
► Example
Page 22
Constructs of a pseudocode - Repetition or loop
Page 23
Constructs of a pseudocode - Repetition or loop
► There are three types of loop constructs as discussed below
► while loop
► repeat-until loop
► for loop
Page 24
Constructs of a pseudocode – while loop
► A while loop is generally used to implement indefinite iteration. The
general form of the while loop is as follows:
Page 25
Constructs of a pseudocode – repeat-until loop
Page 26
Constructs of a pseudocode – repeat-until loop
► There are two major differences between while and repeat-until loop
constructs:
► In the while loop, the pseudocode continues to execute as long as the resultant of
the condition is True; in the repeat-until loop, the looping process stops when the
resultant of the condition becomes True.
► In the while loop, the condition is tested at the beginning; in the repeat until loop,
the condition is tested at the end.
► For this reason, the while loop is known as an entry controlled loop and the
repeat-until loop is known as an exit controlled loop.
Page 27
Constructs of a pseudocode – for loop
Page 29
Constructs of a pseudocode – for loop
Page 30
Constructs of a pseudocode – for loop
lists some examples of for loops. In these examples, var is the loop variable.
Page 31
► Problem: To find simple interest
Page 32
Solved problems - Algorithms
LargerTwo
1 Start
2 Read(number1, number2)
3 if (number1 > number2)
4 large = number1
5 else
6 large = number2
7 endif
8 Print(large)
9 Stop.
Page 33
► Problem: To determine the smallest of three numbers.
SmallestThree
1 Start
2 Read(number1, number2, number3)
3 if (number1 < number2)
4 small = number1
5 else
6 small = number2
7 endif
8 if (number3 < small )
9 small = number3
10 endif
11 Print(small )
12 Stop.
Page 34
Solved problems - Algorithms
TicketFare
1 Start
2 Read(age)
3 if (age < 10)
4 fare = 7
5 else if (age < 60)
6 fare = 10
7 else
8 fare = 5
9 endif
10 Print(fare)
11 Stop
Page 35
► Problem: To print the colour based on a code value as follows:.
PrintColour
1 Start
2 Read(code)
3 caseof (code)
4 case ‘R’:
5 Print(“Red”)
6 break
7 case ‘G’:
8 Print(“Green”)
9 break
10 case ‘B’:
11 Print(“Blue”)
12 break
13 default :
14 Print(“Wrong code”)
15 endcase
16 Stop
Page 36
Solved problems - Algorithms
PrintDown
1 Start
2 for count = 50 downto 1
3 Print(count)
4 endfor
5 Stop
Page 37
► Problem: To find the factorial of a number.
Factorial
1 Start
2 Read(number)
3 fact = 1
4 for var = number downto 1
5 fact = fact ∗ var
6 endfor
7 Print(fact)
8 Stop
Page 38
Solved problems - Algorithms
Page 39
► Problem: To determine the average age of students in a class. The user will
stop giving the input by giving the age as 0.
Page 40
Solved problems - Algorithms
► Problem: To determine the average age of students in a class. The user will
stop giving the input by giving the age as 0. use repeat-until loop
Page 41
► Problem: To find the average height of boys and average height of girls in a
class of n students
Page 42
Module 2-
part 2
Page 2
Flowchart
Page 6
Flowchart
Page 8
Flowchart
Flowchart
Page 10
Flowchart
Page 12
Flowchart
Page 14
Flowchart
Page 16
Flowchart
► To find the average height of boys and average height of girls in a class
of n students.
Page 18
Page 19