PYTHON PPTM

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 59

PYTHON

UNIT-I
UNIT I

• FUNDAMENTALS OF COMPUTING : Identification of Computational


Problems (Example) Algorithms, building blocks of algorithms (statements)
(state) (control flow) (functions) Notation (pseudo code) (flow chart)
(programming language) Algorithmic problem solving (Example) simple
strategies for developing algorithms (Iteration) (Recursion) ILLUSTRATIVE
PROBLEMS : Find minimum in a list (Example) Insert a card in a list of sorted
cards (Example) Guess an integer number in a range (Example) Towers of
Hanoi (Example)
ALGORITHMIC PROBLEM SOLVING

• 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

• 1) Finite no. of.steps


• 2)Atleast one input
• 3) Every instructions should be precise and unambiguous
• 4)Ensure it has proper termination
• 5) Effectiveness of each step6)The desired output must be obtained
Qualities of Algorithm

• Accuracy- GREAT WOW! GOOD RESULTS


• Memory
• Time
• Sequence
• Result
Representation 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

• CLASSIFICATION OF CONTROL FLOW:


1)Sequence control flow
2) Selection control flow
3)Case structure
Building blocks of Algorithm

• 1)Sequence control flow:


Sequence control flow is used to perform actions one after another.
Building blocks of Algorithm

•Sequence control flow:


Question:Find the sum of two numbers?
Question:Find the sum of two numbers?

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

• Selection control flow:


Selection control flow or decision structures allows the
program to make" choice between two alternate paths“.
Example:if&if-else
Building blocks of Algorithm

• Selection control flow:


Question:Find the greater number?
Question: Find the greater number?

• 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

Step1: Every function begins with def


Step2: Every function has a name
Step3:Function may or may not have the parameters/arguments
Step4:Body of the function to perform tasks
Step5: Return the output
PSEUDOCODE

• PSEUDOCODE:
Pseudocode is an artificial and informal language that helps
programmers develop Algorithm
Imitation or False
PSEUDOCODE

Programs VS Pseudocode
PSEUDOCODE

• Basic guidelines of pseudocode


PSEUDOCODE

• 1) Write only one Statement per line?


READ the value of n
IF (n%2==0) THEN
WRITE n is even number
ELSE
WRITE n is odd number
ENDIF
Stop
PSEUDOCODE

• 2)Capitalize initial keyword


READ name
• Examples:
READ, WRITE,IF,ELSE,ENDIF,WHILE,ENDWHILE, REPEAT, UNTIL
PSEUDOCODE

• 3)Indent to show hierarchy


Indentation
IF a>b
print a
ELSE
print b
PSEUDOCODE

• 4) End multiline structures


IF WHILE
.
.
.
ENDIF ENDWHILE
PSEUDOCODE

• 5)Keep statement language independent


Simple language
PSEUDOCODE
• Advantages:
It is simple to write
No specific symbols are used
No specific syntax is used
Easy to translate from statements to high level language

• Disadvantages:
Not standardized
It is not visual
Cannot be compiled and executed
PSEUDOCODE

• Question:
Find the area and circumference of a circle?
PSEUDOCODE

• Area of Circle : A=πr*r


• Circumference of Circle: C=2nr
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

• READ the radius Area=3.14*r*r Circumference=2*3.14*r


• WRITE area and circumference STOP
FLOWCHARTS

• Definition
• Symbols
• Basic guidelines
• Advantages and Disadvantages
FLOWCHARTS

• DEFINITIONA :
Flowchart is a 'diagrammatic representation' that illustrates sequence
of operations to be performed
FLOWCHARTS
FLOWCHARTS

• BASIC GUIDELINES FOR PREPARING FLOWCHART:


• In drawing a proper flowchart, all necessary requirements should be listed
put in logical order
• It should be clear neat and easy to follow
• Direction of flow should be left to right/top to bottom.
• Only one flow line should come out from a process symbol.
FLOWCHARTS

• Only one flowline should enter a decision symbol.


• Only one flow line is used in conjunction with terminal symbol.
FLOWCHARTS

• Write within standard symbols briefly . If necessary ,use annotation symbol


for calculation part
• Calculation part
• If flowchart becomes complex better use connector symbols.
• Ensure that flowchart has logical start and stop.
• Validity of flowchart can be tested by passing through it with simple test
data
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

• Find the given year in a leap year or not 2000?


• Algorithm
• Pseudocode
• Flowchart
Answer

• 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

You might also like