PYTHON PPTM
PYTHON PPTM
PYTHON PPTM
UNIT-I
UNIT I
• STEP BY STEP
• PROBLEM SOLVING
ALGORITHMIC PROBLEM SOLVING
Definition of Algorithm:
• Algorithm is defined as "a step by step procedure for solving any problem".
It is also a set of rules specifying how to solve a problem
Characteristics of Algorithm
• Step1: Start
•.
•.
•.
• Step'n': Stop
Find Sum of two numbers
• Step1:Start
• Step2: Read the valuesofa, b
• Step3: Find the sumofa, bSum=a+b
• Step4: Print the sum
• Step5:Stop
Building blocks of Algorithm
• Instructions/Statements
• State
• Control
• FlowFunction
Building blocks of Algorithm
• Statements:
The algorithms consists of finite number of 'statements’
The statements must be in 'ordered form
'The timetaken to execute all the statements of the algorithm
should be finite and within a reasonable limit.
Building blocks of Algorithm
• Simple statement:
assignment
goto:goto next
return
• Compound statement :
if
else
for-loop
while-loop
switch
Building blocks of Algorithm
• Simple statement:
assignment : a=10
Building blocks of Algorithm
• State:
The State determines the logic flow of the program.
Building blocks of Algorithm
• Control flow:
Control flow or flow of control is used to determine what
section of code is to be run in a program at a given time.
Building blocks of Algorithm
Answer:
Step1: Start
Step2: Read values of a,b
Step3: Calculate c = a+b
Step4: Print the value of c
Step5: Stop
Building blocks of Algorithm
• Answer:
Step1: Start
Step2: Read values of a,b
Step3: Compare a,b.lf a is greater than b then print a is big
Step4: Stop
Building blocks of Algorithm
• CASE STRUCTURE:
• This is a multiway selection structures that is used to choose one
option from many options
• Example: Switch, if, elseif, else
Building blocks of Algorithm
• CASE STRUCTURE:
Question : Find the greatest among three numbers?
Question : Find the greatest among three
numbers?
• Answer:
Step1: Start
Step2: Read values of a,b,c
Step3: Compare the values of ab and c
Step4: if a is greater than b then compare a and c
Step5: If a a is greater than c then print a is big
Step6:lf a is not greater than b and c then compare b and c.
Step7 If b is greater than c , then print b is big else print c is big
Building blocks of Algorithm
Function:
• Function is a named container for a block of code that performs a specific task
when it is called.
• Reduces maximum number. of lines of code
• Makes debugging efficient
• Take in - Process – Return
Example : Dr. Nithya
Dr keyword Nithya function name
Building blocks of Algorithm
• Function:
1. User-defined functions
2. Built-in functions
Function Algorithms
• PSEUDOCODE:
Pseudocode is an artificial and informal language that helps
programmers develop Algorithm
Imitation or False
PSEUDOCODE
Programs VS Pseudocode
PSEUDOCODE
• Disadvantages:
Not standardized
It is not visual
Cannot be compiled and executed
PSEUDOCODE
• Question:
Find the area and circumference of a circle?
PSEUDOCODE
• Step1: Start
• Step2: Read the value of radius
• Step3: Find the area and circumference using the formula Area=3.14*r*r
Circumference=2*3.14*r
• Step4: Print the area and circumference
• Step5:Stop
PSEUDOCODE
• Definition
• Symbols
• Basic guidelines
• Advantages and Disadvantages
FLOWCHARTS
• DEFINITIONA :
Flowchart is a 'diagrammatic representation' that illustrates sequence
of operations to be performed
FLOWCHARTS
FLOWCHARTS
• Algorithm:
Step1: Start
Step 2: Read values of a , b,
Step3: Scoop the values using a temporary variable. (temp-temporary vou able)a=b
b= temp
Step4: print a , b-temp
Step5: St0p
FLOWCHARTS
• Flowchart image
FLOWCHARTS
• ADVANTAGES:
• * Better way of Communication.
• * Problem analysis is efficient.
• Flowchart serves as a quide .
• * Helps in debugging process.
• DISADVANTAGES :
• Logic is complicated .
• Flowchart should be redrawn in case of alternations.
RECAP
• Algorithm
• Pseudocode
• Flowchart
RECAP
• Feb 29,2020
• Feb2024
• Feb 2028
RECAP
• Leap Years:
1800-24001834 1934 1834 2804 2004 2504 1200 1900 2000 2008 2008 2145 1014
1912 2012 202 242 2/12 1190 1806 2176 2290 2923 1824 1924 2034 2124 2224
2824 8 709 7:37 2/3) 1832 1930 2036 2116 2216 2135 1840 240 1940 2040 2140
2240 2243 1904 1904 2014 2144 2242 241 1851 1950 2052 2122 2232 2552 184454
5054 2164 206 1219/2 28/221/22/23/2 MBC 1990 2000 2150 22892140 1254 1954
2034 2864 2284 2884 151 1950 200 2118 220925 189 1900 1900 2116 2296 2196
RECAP
• 4/2 = 2
• 4% 2 = 0
Question
• Algorithm:
• Step 1: Start
• Step 2: Read the year
• Step 3: If(year4)=0) then print leap year.
• Step4: Else print, not a leap year
• Step5: Stop
• Pseudocode:
• READ the value of year
• IF ((year)WRITE the yean a leap year
• ELSE
• WRITE the year is not a leap year
• ENDIF
• STOP
flowchart
ITERATION & RECURSION
• ITERATION:
• Repeated execution of a set of statements is called iteration
• RECURSION:
• Recursion is a problem solving approach by which a function calls itself
repeatedly until some specified condition has been satisfied
ITERATION & RECURSION
• RECURSION
• * A function calls itself through repetition
• * Recursion terminates when base condition is reached
• Large memory space is required
• ITERATION
• It is one that loops to repeat some part of the code
• * Iteration terminates when loop condition be comes false
• * Less memory requirement
ITERATION & RECURSION
• Example:
* GCD using recursion
• Tower of Hanoi
• * Factorial of a given number using recursion
• Example:
• * Sum of digits
• * Fibonacci series
• * Reverse of a number
• * Factorial of a given number