python-notes
python-notes
Python Notes
UNIT I
Sequence:
All the instructions are executed one after another is called sequence execution.
Example:
Selection:
A selection statement causes the program control to be transferred to a specific part of the
program based upon the condition.
If the conditional test is true, one part of the program will be executed, otherwise it will
execute the other part of the program.
Example
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop
Iteration:
In some programs, certain set of statements are executed again and again based upon
conditional test. i.e. executed more than one time. This type of execution is called looping or
iteration.
Example
Write an algorithm to print all natural numbers up to n
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
NOTATIONS
FLOW CHART
Flow chart is defined as graphical representation of the logic for problem solving.
The purpose of flowchart is making the logic of the program clear in a visual representation.
4. Only one flow line should enter a decision symbol. However, two or three flow lines may
leave the decision symbol.
Advantages of flowchart:
PSEUDO CODE:
Pseudo code consists of short, readable and formally styled English languages used
for explain an algorithm.
It does not include details like variable declaration, subroutines.
It is easier to understand for the programmer or non programmer to understand the
general working of the program, because it is not based on any programming
language.
It gives us the sketch of the program before actual coding.
It is not a machine readable
Pseudo code can’t be compiled and executed.
There is no standard syntax for pseudo code.
Guidelines for writing pseudo code:
Write one statement per line
Capitalize initial keyword
Indent to hierarchy
End multiline structure
Keep statements language independent
...
ELSE
statement
...
ENDIF
statement
...
ENDWHILE
Advantages:
Pseudo is independent of any language; it can be used by most programmers.
It is easy to translate pseudo code into a programming language.
It can be easily modified as compared to flowchart.
Converting a pseudo code to programming language is very easy as comparedwith
converting a flowchart to programming language.
Disadvantages:
It does not provide visual representation of the program’s logic.
There are no accepted standards for writing pseudo codes.
It cannot be compiled nor executed.
For a beginner, It is more difficult to follow the logic or write pseudo code ascompared
to flowchart.
Example:
PROGRAMMING LANGUAGE
A programming language is a set of symbols and rules for instructing a computer to perform
specific tasks. The programmers have to follow all the specified rules before writing program
using programming language. The user has to communicate with the computer using
language which it can understand.
1. Machine language
2. Assembly language
3. High level language
Machine language:
The computer can understand only machine language which uses 0’s and 1’s. In
machine language the different instructions are formed by taking different combinations of
0’s and 1’s.
Advantages:
Translation free:
Machine language is the only language which the computer understands. For executing any
program written in any programming language, the conversion to machine language is
necessary. The program written in machine language can be executed directly on computer. In
this case any conversion process is not required.
High speed
The machine language program is translation free. Since the conversion time is saved, the
execution of machine language program is extremely fast.
Disadvantage:
It is hard to find errors in a program written in the machine language.
Writhing program in machine language is a time consuming process.
Machine dependent: According to architecture used, the computer differs from each other.
So machine language differs from computer to computer. So a program developed for a
particular type of computer may not run on other type of computer.
Assembly language:
To overcome the issues in programming language and make the programming process easier,
an assembly language is developed which is logically equivalent to machine language but it is
easier for people to read, write and understand.
Assembly language is symbolic representation of machine language. Assembly languages
are symbolic programming language that uses symbolic notation to represent machine
language instructions. They are called low level language because they are so closely related
to the machines.
Assembler
Assembler is the program which translates assembly language instruction in to a machine
language.
Disadvantage
Machine dependent
The assembly language program which can be executed on the machine depends on the
architecture of that computer.
Hard to learn
It is machine dependent, so the programmer should have the hardware knowledge to create
applications using assembly language.
Less efficient
Execution time of assembly language program is more than machine language program.
Because assembler is needed to convert from assembly language to machine language.
High level language contains English words and symbols. The specified rules are to be
followed while writing program in high level language. The interpreter or compilers are used
for converting these programs in to machine readable form.
10
Compiler:
A compiler is a program which translates the source code written in a high level language in
to object code which is in machine language program. Compiler reads the whole program
written in high level language and translates it to machine language. If any error is found it
display error message on the screen.
Interpreter
Interpreter translates the high level language program in line by line manner. The interpreter
translates a high level language statement in a source program to a machine code and
executes it immediately before translating the next statement. When an error is found the
execution of the program is halted and error message is displayed on the screen.
Advantages
Readability
High level language is closer to natural language so they are easier to learn and understand
Machine independent
High level language program have the advantage of being portable between machines.
Easy debugging
Easy to find and correct error in high level language.
Disadvantages
Less efficient
The translation process increases the execution time of the program. Programs in high level
language require more memory and take more execution time to execute.
11
Scripting language:
Scripting language are programming languages that control an application. Scripts can
execute independent of any other application. They are mostly embedded in the application
that they control and are used to automate frequently executed tasks like communicating with
external program.
Examples:
shoppingmode Apple script
VB script
Markup languages:
A markup language is an artificial language that uses annotations to text that define
hoe the text is to be displayed.
Examples:
HTML
XML
13
14
Analysing an Algorithm
15
1. Efficiency.
Time efficiency, indicating how fast the algorithm runs,
Space efficiency, indicating how much extra memory it uses.
2. simplicity.
An algorithm should be precisely defined and investigated with mathematical
expressions.
Simpler algorithms are easier to understand and easier to program.
Simple algorithms usually contain fewer bugs.
Coding an Algorithm
Most algorithms are destined to be ultimately implemented as computer programs.
Programming an algorithm presents both a peril and an opportunity.
A working program provides an additional opportunity in allowing an empirical analysis
of the underlying algorithm. Such an analysis is based on timing the program on several
inputs and then analysing the results obtained.
1. iterations
2. Recursions
1. Iterations:
16
17
Recursions:
Main function:
Step1: Start
Step2: Get n
Step3: call factorial(n)
Step4: print fact
Step5: Stop
Main function:
BEGIN
GET n
CALL factorial(n)
PRINT fact
BIN
More examples:
19
Step 1: Start
Step 2: get l,b values
Step 3: Calculate A=l*b
Step 4: Display A
Step 5: Stop
BEGIN
READ l,b
CALCULATE A=l*b
DISPLAY A
END
Step 1: Start
Step 2: get r value
Step 3: Calculate A=3.14*r*r
20
BEGIN
READ r
CALCULATE A and C
A=3.14*r*r
C=2*3.14*r
DISPLAY A
END
Step 1: Start
Step 2: get P, n, r value
Step3:Calculate
SI=(p*n*r)/100
Step 4: Display S
Step 5: Stop
21
BEGIN
READ P, n, r
CALCULATE S
SI=(p*n*r)/100
DISPLAY SI
END
Step 1: Start
Step2: get P,C,M value
Step3:calculate
Cutoff= (P/4+C/4+M/2)
Step 4: Display Cutoff
Step 5: Stop
22
BEGIN
READ P,C,M
CALCULATE
Cutoff= (P/4+C/4+M/2)
DISPLAY Cutoff
END
Step 1: Start
Step 2: get a,b value
Step 3: check if(a>b) print a is greater
Step 4: else b is greater
Step 5: Stop
23
BEGIN
READ a,b
IF (a>b) THEN
DISPLAY a is greater
ELSE
DISPLAY b is greater
END IF
END
Step 1: Start
Step 2: get y
Step 3: if(y%4==0) print leap year
Step 4: else print not leap year
Step 5: Stop
24
BEGIN
READ y
IF (y%4==0) THEN
DISPLAY leap year
ELSE
DISPLAY not leap year
END IF
END
Step 1: Start
Step 2: get num
Step 3: check if(num>0) print a is positive
Step 4: else num is negative
Step 5: Stop
25
BEGIN
READ num
IF (num>0) THEN
DISPLAY num is positive
ELSE
DISPLAY num is negative
END IF
END
Step 1: Start
Step 2: get num
Step 3: check if(num%2==0) print num is even
Step 4: else num is odd
Step 5: Stop
26
BEGIN
READ num
IF (num%2==0) THEN
DISPLAY num is even
ELSE
DISPLAY num is odd
END IF
END
Step1: Start
Step2: Get A, B, C
Step3: if(A>B) goto Step4 else goto step5
Step4: If(A>C) print A else print C
Step5: If(B>C) print B else print C
Step6: Stop
27
BEGIN
READ a, b, c
IF (a>b) THEN
IF(a>c) THEN
DISPLAY a is greater
ELSE
DISPLAY c is greater
END IF
ELSE
IF(b>c) THEN
DISPLAY b is greater
ELSE
DISPLAY c is greater
END IF
END IF
END
Step 1: Start
Step 2: Get n value.
28
BEGIN
GET n
IF(n==0) THEN
DISPLAY “ n is zero”
ELSE
IF(n>0) THEN
DISPLAY “n is positive”
ELSE
DISPLAY “n is positive”
END IF
END IF
END
29
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 8
Step 5: Print i value
step 6 : increment i value by 1
Step 7: go to step 4
Step 8: Stop
BEGIN
GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i
i=i+1
ENDWHILE
END
30
Step 1: start
step 2: get n value
step 3: set initial value i=1
step 4: check if(i<=n) goto step 5 else goto step 8
step 5: print i value
step 6: increment i value by 2
step 7: goto step 4
step 8: stop
BEGIN
GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i
i=i+2
ENDWHILE
END
31
Step 1: start
step 2: get n value
step 3: set initial value i=2
step 4: check if(i<=n) goto step 5 else goto step8
step 5: print i value
step 6: increment i value by 2
step 7: goto step 4
step 8: stop
BEGIN
GET n
INITIALIZE i=2
WHILE(i<=n) DO
PRINT i
i=i+2
ENDWHILE
32
END
Step 1: start
step 2: get n value
step 3: set initial value i=1
step 4: check i value if(i<=n) goto step 5 else goto step8
step 5: print i*i value
step 6: increment i value by 1
step 7: goto step 4
step 8: stop
BEGIN
GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i*i
i=i+2
33
ENDWHILE
END
BEGIN
GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i*i*i
i=i+2
34
ENDWHILE
END
Step 1: start
step 2: get n value
step 3: set initial value i=1, sum=0
Step 4: check i value if(i<=n) goto step 5 else goto step8
step 5: calculate sum=sum+i
step 6: increment i value by 1
step 7: goto step 4
step 8: print sum value
step 9: stop
BEGIN
GET n
INITIALIZE i=1,sum=0
WHILE(i<=n) DO
sum=sum+i
35
i=i+1
ENDWHILE
PRINT sum
END
Step 1: start
step 2: get n value
step 3: set initial value i=1, fact=1
Step 4: check i value if(i<=n) goto step 5 else goto step8
step 5: calculate fact=fact*i
step 6: increment i value by 1
step 7: goto step 4
step 8: print fact value
step 9: stop
BEGIN
GET n
INITIALIZE i=1,fact=1
36
WHILE(i<=n) DO
fact=fact*i
i=i+1
ENDWHILE
PRINT fact
END
ILLUSTRATIVE PROBLEM
Algorithm:
Step1: Start
Step 2: Declare hidden, guess
Step 3: Compute hidden= Choose a random value in a range
Step 4: Read guess
Step 5: If guess=hidden, then
Print Guess is hit
Else
Print Guess not hit
Print hidden
Step 6: Stop
Pseudocode:
BEGIN
COMPUTE hidden=random value in range
READ guess
IF guess=hidden, then
PRINT Guess is hit
ELSE
PRINT Guess not hit
PRINT hidden
END IF-ELSE
37
END
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read n
Step 3:Initialize i=0
Step 4: If i<n, then goto
step 4.1, 4.2 else goto
step 5 Step4.1: Read a[i]
Step 4.2: i=i+1 goto step 4
Step 5: Compute min=a[0]
Step 6: Initialize i=1
Step 7: If i<n, then go to step 8 else goto step 10
Step 8: If a[i]<min, then goto
step 8.1,8.2 else goto 8.2
Step 8.1: min=a[i]
Step 8.2: i=i+1 goto 7
38
Pseudocode:
BEGIN READ n
FOR i=0 to n, then
READ a[i]
INCREMENT i
END FOR
COMPUTE min=a[0]
FOR i=1 to n, then
IF a[i]<min, then
CALCULATE min=a[i]
INCREMENT i
ELSE
INCREMENT i
END IF-ELSE
END FOR
PRINT min
END
Flowchart:
39
Algorithm:
Step 1: Start
Step 2: Read n
40
Pseudocode:
BEGIN
READ n
FOR i=0 to n, then
READ a[i]
INCREMENT i
END FOR
READ item
FOR i=n-1 to 0 and item<a[i], then
CALCULATE a[i+1]=a[i]
DECREMENT i
END FOR
COMPUTE a[i+1]=a[i]
COMPUTE n=n+1
FOR i=0 to n, then
PRINT a[i]
INCREMENT i
END FOR
END
41
Flowchart:
4. Tower of Hanoi
42
Algorithm:
Step 1: Start
Step 2: Read n
Step 3: Calculate move=pow(2,n)-1
Step 4: Function call T(n,Beg,Aux,End) recursively until n=0
Step 4.1: If n=0, then goto
step 5 else goto step
4.2 Step
4.2: T(n-1,Beg,End,Aux) T(1,Beg,Aux,End) , Move disk from source to desti ation T(n-
1,Aux,Beg,End)
Step 5: Stop
Pseudcode:
BEGIN
READ n
CALCULATE move=pow(2,n)-1
FUNCTION T(n,Beg,Aux,End) Recursiv ly until n=0
PROCEDURE
IF n=0 then,
No disk to move
Else
T(n-1,Beg,End,Aux)
T(1,Beg,Aux,End), move isk from source to destination
T(n-1,Aux,Beg,En )
END PROCEDURE
END
Flowchart:
43
44
45
46
47
48