Python Unit 1
Python Unit 1
Python Unit 1
Preface
Brief Contents x
Road Map to Syllabus xv
1 Problem Solving
(2MOTVU3WOR JORTMOD
Corcepts
anietocodapaiore go toubo.ar bggle seioogto eif a an
20 baniovgasd neo cotiogle ns 1o (wof loto09edhmo lonuosto dt
1.1 INTRODUCTION lsubviboi daidur gt Tobto sdi 201o0g loinoo
Aprogram is a set of instructions that tells the computer how to solve a particular problem. Various program
design tools like algorithms, pseudocdes and fowcharts are used to design the blueprint of the solution
(or the program to be written). Computer programming goes a step further in problem solving process.
Programming means writing computer programs. While programming, the programmers take an algorithm
and code the instructions in a particular programming language so that it can be executed by a computer
These days, there are many programming languages available in the market. The programmer can choose any
language depending on his expertise and the problem domain.
The following sections will deal with different program design tools, knowledge of which is compulsory
to develop programming skills.
1.2 ALGORITHMS
In computing, we focus on the type of problems categorically known as algorithmic problems, where their
solutions are expressible in the form of algorithms. The term 'algorithm' was derived from the name of
Mohammed al-Khwarizmi, a Persian mathematician in the nineth century (Al-Khwarizmi Algorism
(in Latin)Algorithm). The typical meaning of an algorithm is a formally defined procedure for performing8
some calculation. Ifa procedure is formally defined, then it must be implemented using some formal language,
and such languages are known as programming languages. The algorithm gives the logic of the program, that
is, a step-by-step description of how to arrive at a solution.
In general terms, an algorithm provides a blueprint to writing a program to solve a particular problem. It
is considered to be an effective procedure for solving a problem in a finite number of steps. That is, a well-
defined algorithm always provides an answer, and is guaranteed to terminate.
Algorithms are mainly used to achieve software reuse. Once we have an idea or a blueprint of a solution,
we can implement it in any language, such as C, CH, Java, and so on. In order to qualify as an algorithm, a
sequence of instructions must possess the following characteristics:
Precision: The instructions should be written in a precise manner.
Uniqueness: The outputs of each step should be unambiguous, i.e., they should be unique and only depend
on the input and the
output of the preceding steps.
Finiteness: Not even a single instruction
must be repeated infinitely.
2 Problem Solving and Programming with Python
operation which when executed converts one state to other. for further
in computer's memory
In the course of processing, data is read from an input device, stored
device.
processing, and then the result of the processing is written to output
an
The data is stored in the computer's memory in the form of variables or constants. The state of an algorithmn
is defined as its condition regarding current values or contents of the stored data.
An algorithm is a list of precise steps and the order of steps determines the functioning of the algorithm.
The fiow ofcontrol (or the control fow) of an algorithm can be specified as top-down or bottom-up approach.
Thus, the flow of control specifies the order in which individual instructions of an algorithm are executed
equivalence.
Dictionaries: Allows data to be stored as a key-value pair.
is art which may neverbe fully automated.
Algorithm design techniques Developing algorithmfor
an an
Step 5: Compare B and C. If B is greater, output "B is greatest" else autput "C is
add two numbers greatest"
Example 1.1 Algoithm to
Step 6: End
Step 1 : Start
gto eroie gteaiol
Input first number as A
n s boshgonge ed 1oeiod
Step
Step
2
3: Input second number as B
Example 1.4 Algorithm to find the greatest of three numbers using an additional variable MAX
Step 4 Set Sum= A +B
step 1: Start
Step 5 : Print Sum
Step 2: Read the three numbers A,B,C
Step 6 : End
Step 3: Compare A and B. If A is greater, store Ain MAX, else store B in MAX
Step 4: Compare MAX and C. If MAX is greater, output MAX i s greater else output
"C is greater"
Decision Decision statements are used when the outcome of the process depends on some condition. For Step 5: End
example, ifx =y, then print "EQUAL". Hence, the general form of the if construct can be given as followS
IF condition then process
Both the algorithms given in Examples 1.3 and 1.4 accomplish same goal, but in different ways. The
programmer selects the algorithm based on the advantages and disadvantages of each algorithm. For example,
Acondition in this context is any statement that may evaluate either to a true value or a false value. In the
the first algorithm has more number of comparisons, whereas in the second algorithm an additional variable
preceding
false. If
example, the variable can either be equal or not equal to .
x However, it cannot be both true and
the condition is true then the process is executed. MAX is used to do the comparison.
Adecision statementcan also be statedinthefollowing manner:wotb elpeobuecg 3o gedpr diw Iteration or repetition which involves executing one or more steps for a number of times, can be
implemented using constructs such as the while loops and for loops. These loops execute one or more steps
IF condition eaerdopmea eprinouis.RYe until some condition is true.
then processi ble oldiazog sl lls 101 250i30loe zoltqeo2
ELSE process2
Example 1.5 Algorithm that prints the first 10 natural numbers
This form is commonly known as the if-else construct. Here, if the condition is true then
executed, else process2 is executed. An algorithm to check the equality of two numbers is shown processI is
Step 1: Start
below.
Step 2: [initialize] Set I = 1, N 10
Example 12 Algorithm to test the quality of two numbers Step 3: Repeat Steps 3 and 4 while I <= N
Step 4: Print I
Step 1 : Start Step 5: Set I = I + 1 [END OF LOOP]
Step 2
Input first number as A
: Step 6: End
Step 3: Lnputt
Step 4 IF A=B Second number as B
Print Equal 9HTRo238 DIgoJaVad Example 1.6 Design an algorithm for adding the test scores given as: 26, 49, 98, 87, 62, 75.
ELSE
Pránt Not equal"
[END of IF] Step 1: Start
Step 5:End
Step 2: sum 0
Step 3: Get a value
Problem Solving and
Programming
with Python
Algorithmic Problem Solving 7
Example 1.10 Write an algoríthm to prínt the grade obtained by a student using the following rules
8O t o step b
Step 4:
Sum sum+ value
is present, go
to step 3.
0therwise
Marks Grade
I f next value Above 75
Step 5:
P r i n t the sum
Step 6: 60-75
Step 7: End
algorithm. 50-60
and iterationlogic in an
demonstrate the application ofrepetition in algorithms
are given as
follows. -50
1.6
Examples 1.5 and
structures
second number as B
Step 3: Input
.
Step 4: IF A> B Example 1.11 Write an algorithm to find the sum of first N natural numbers.
Print A
ELSE IF A<B Step 1: Start
Print B Step 2: Input N
ELSE Step 3: Set I = 1, sum =
Print The numbers are equal Step 4: Repeat Steps 4 and 5 while I <= N
[END OF IF] Step 5: Set sum= sum+I
Step 5: End Step 6: Set I = I+1
[END OF LOOP]
Step 7: Print sum 0
Example 1.9 Write an algorithm to find whether a number is even or odd. Step 8: End
Step 1: Start
Step 2: Input number asA Recursion It is a technique of solving a problem by breaking it down into smaller and smaller sub-problems
Step 3: IF A % 2 until you get to a small enough problem that it can be easily solved. Usually, recursion involves a function
Print"Even" calling itself until a specified condition is met, Since a recursive function repeatedly calls itself, it makes use
ELSE of the system stack to temporarily store the return address and local variables ofthe calling function. Every
Print "Odd" recursive solution has two major cases. They are:
[END OF IF]
Step 4: End Base case, in which the problem is simple enough to be solved directly without making any further calls
to the same function.
Programming with Python
Problem Solving and er Algorithmic Problem Solving
into simpler sub-parts.
Second the function
first the problem at hand
is divided is obtained by com. npuiOutput symbols are represented using a parallelogram and are used to get inputs from the users or
in which Third, the result display the results to them.
Recursive case, obtained in the first step.
callsitself but with sub-parts of the problem A condifional
or decision
symbol is represented using a diamond. It is basically used to depict a Yes/No
the solutions of simpler sub-parts.
bining terms of
smaller and more easily solvable question True/False test. The two symbols coming out of it, one from the bottom
or a
problems in problems and the simplest
point and the other
Therefore, recursion is defining
large and complex from the right point, corresponds to Yes or True, and No or False, respectively. The arrows should always
is defined in terms ofsimpler
recursive functions, a complex problem be labelled. A decision
symbol in a flowchart can have more than two arrows, which indicates that a
problems. In
problem is given explicitly
complex decision is being taken.
Iteration, refer to Chapter
4)
and Labelled connectors are
(For a detailed stud on Recursion represented by ap identifying label inside a circle and are used in complex or
multi-sheet diagrams to substitute for arrows. For each label, the
factorial of a number. 'outfow connector must have one or
to find the more 'inflow' connectors. A
pair of identically labelled connectors is used to indicate a continued fow
Write a recursive algorithm
Example 1.12 when the use of lines becomes confusing.
Apre-definedprocess symbolisamarkerfor anotherprocessstep orseries
defined elsewhere. This
ofprocessfow stepsthatareformaly
Step 1: Start
Step 2: Input
number a s n
shape commonly depicts sub-proceses (or
subroustines in
programming flowcharts)
factorial (n) Significance of Flowcharts
Step 3: Call
A
Step 4: End flowchart is a diagrammatic representation that illustrates the sequence of steps that must be performed
to solve a problem. It is usually drawn in the early stages of formulating computer solutions. It facilitates
factorial (n) communication between programmers and users. Once a flowchart is drawn, programmers can make users
f= 1 understand the solution easily and clearly.
Step 1: Set
1
Step 2: IF ne-1 then return Flowcharts are very important in the programming of a problem as they help the programmers to
ELSE understand the logic of complicated and lengthy problems. Once a flowchart is drawn, it becomes easy for
Set fnfactorial (n-1) the
programmers to write the program in any high-level language. Hence, the flowchart has become a necessity
Step 3: Print f for better documentation of complex programs.
A flowchart follows the top-down approach in solving problems. Some examples are given as follows.
NO
Start and end symbols are also known as the terminal symbols and are represented as circles, ovals, or Is I = 10? Print SUM
rounded rectangles. Terminal symbols are always the first and the last symbols in a flowchart,
Arrows depict the flow of control of the program. They illustrate the exact sequence in which the instructions YES
are executed
Display SUM STOP
Generic processing step, also called activity, represented using a rectangle. Activities include
as an is
instructions such as add a to b or save the result. Therefore, a processing symbol represents arithmetic and
data movement instructions. When more than one process has to be executed simultaneously, they can STOP
beplaced in the same processing box. However, their execution will be carried out in the order of their
appearance.
with Python
Problem Solving
and Programming
Algorithmic Problem Solving 11
10
1.16 Draw aflowehart to detemine thelargest of It is basically meant for human reading rather than machine reading, so it omits the details that are not
Draw a flowchart to
Example
Example
1.15 three numbers.
calculate the of
salary essential for humans. Such details include variable declarations, system-specifc code, and subroutines.
Pseudocodes are an outline of a program that can easily be converted into
a daily wager programming statements. 1 hey
START
consistofshort English phrases that explain specific tasks within a program's algorithm. They should not
include keywords in any specific computer language.
START
The sole purpose of pseudocodes is to enhance human understandability of the solution They are commonly
Read the
usedintextbooks and scientific publications for documenting algorithms, andfor sketching out the program
values of
SALARY Input the
ade-lar structure beforetheactual codingis done. This heipsevennon-programmerstounderstand thelogic ofthedesigned
A, B, and C solution.
no_of_hrs, paype Print B Thereare no standards
program. Flowcharts
defined for writing a pseudocode, because a pseudocode is not an executable
be considered graphical altematives to pseudocodes, but require more
and travel_allowance
can as
spaceon paper
YES
C a l c u l a t e SALARY=(no_of_hr x
ds A>B Bs
C Print c Example 1.17 its
Write apseudocodefor calculating the price of aproduct afteradding the salestax to
original price.
p a yper_hr)+ travel_allowance
YES 1. Start
Print SALARY ds A
C 0Print c 2. Read the price of the product
3. Read the sales tax rate
YES 4. Calculate sales tax = price of the item xsales tax rate
Print A 5. Calculate total price = price of the product + sales tax
STOP 6. Print total price
7. End
STOP
Variables: price of the item, sales tax rate, sales tax, total price
Advantages
They are very good communication tools to explain the logic of a
analyse the problem in a more effective manner.
system to all concerned. They help to Example 1.18 Write a pseudocode to calculate the weekly wages of an employee. The pay depends
on wages per hour and the number of hours worked. Moreover, if the employee has
hey are also used for program documentation. They are even more helpful in the case of complex
progralns. worked for more than 30 hours, then he or she gets twice the wages per hour, for every
They act as a guide or blueprint for the programmers to code the solution in any programming language wonD 99802 extra hour that he or she has worked.
1heydirect the programmers to go from the starting point of the program to the ending point without 1. Start
missing any step in between. This results in error-free programs. Read hours wOrked
They can be used to debug programs that have error(s). They help the programmers to 2
easily detect, locate, 3. Read wages per houn
and remove mistakes in the program in a
systematic manne 4. Set overtime charges to a
Limitations 5. Set Overtime hrs to 0
6. IF hours worked 30 then
Drawing fiowcharts is a laborious and a
of a program having 50,000 time-consuming activity. Just imagine the effort required to
a ffowchart
draw 3 a Calculate overtime hrs hours worked 30
Many a times, the fiowchart of a complexstatements in it! b Calculate overtime charges=overtime hrs (2 * wages per hour)
Elasticity of a language that implies the ease with which new features (or functions) can be added to the
1. Start existing program.
2. Set pass to Portability.
Set fail to e Better speed of development that includes the time it takes to write a code, time taken to find
4 Set no of
students to 1
the problem at hand, time taken to find the bugs, availability of development tools, a solution to
S. HILE no of students
1e
the programmers, and
testing regime. experience and skill of
a inpt the marks For example, FORTRAN is a
b. 1F marks se then particularly good language for processing numerical data, but it does
Set pasSpas +1 not lend itself very well to
organizing large programs. Pascal can be used for writing well-structured and
readable programs, but it is not as flexible as the C
ELSE
programming
doorol incorporating powerful object oriented features, but it is complexlanguage. C goes one step ahead of C by
Set f a i l - f a i l + 1
and difficuit to leam.
ENDIF
good mix of the best features of all these languages. Python, however is a
ENDNHTLE
6. Display pass 1.6 ILLUSTRATIVE PROBLEMS
7. Display fail
In this section, we will look into some common
End problems in computer science and wil specify its solution by
Variables: pass, fail, no of students, marks writing an algorithm and pseudocode, and drawing its corresponding flowchart.
1.6.1 Find Minimum in a List
Consider the following requirement specification.
1.5.4 Programming Languages You are given a list of numbers from which you are supposed to find the minimum
to computations performed
that can be
value. An algorithm is
Apragrammg damgauage is a language specihcally designed express that control the behav1our of a system, required for entering the numbers in the list and then calculate the minimum value. The count of numbers to
bya computer. Programming languages are used to create programs be entered in the list should also be asked from the user
to express algorithms,or as a mode of human communication.
Usaly, programming languages have a vocabulary of syntax and semantics for instructing a computer Before writing the algorithm, just visualize how it works. Takethe first mumber inthelist and call
it minimum. Compare the minimum's value with all other values in the list one
toperfom specific tasks. The tem programming language refers to high-level languages such as BASIC by one. The moment
you find a smaller element than the minimum, call it the minimum. Figure 1.2
(Beginners All-purpose Symbolic Instruction Code). C, C+, COBOL (Common Business Oriented Language),
this concept.
given below 1llustrates
FORTRAN (Fomula Translator), Python,Ada, and Pascal, to name a few. Each of these languages has a unique
set ofkeywords(wordsthatit understands) and a special syntax for organizing program instructions.
hough hghievel programming languages are easy humans to read and understand, the computer can
for
understand only machne language, which consists of only numbers. Each type of central processing unit
(CPU) has ts own unique machine language. Let MIN = 3, Compare MIN with every element in the list ane by one
n between machine languages and high-level languages, there is another type of language known as
assenmbly language. Assembly languages are similar to machine languages, but they are much easier to The ste w
program because they allow a programmer to substitute names for numbers. MIN<5, so no change
However, imespective of the language that a programmer uses, a program written using any
anguage has to be converted into machine language so that the computer programming
can understand it, There are two
ways to do this: compile the program or interpret the MIN< 9, so no change
program.
When planning a software solution, the software
waich programming language to use? Many development team often faces a common question
ts own suengths and weaknesses. Python can programming languages are available today and each one has
be used to write
MIN< 1, so set MIN = 1
an efficient
casy to wrle and understand; some languages are code, whereas a code in BASIC 15
are well known to the programmers, whereas otherscompiled, whereas others are interpreted; some languages MIN<4,
paricular application at hand is a are
completely so no change
daunting task.
new.
Selecting the perfect language for a
Theselecton of language for writing a
The type of computer hardware and program depends on the
following factors: MIN < 7 so no change
*The typeof software on which the
program. program is to be executed.
The expertise and
availability of the
Features to write the programmers. MIN<2, so no change
application.
The built-in features that
Lower development andsupport the development of software
that are
maintenance
Stability and capability to support evencosts. reliable and less prone to crash. MIN< 6, so no change
more than the
expected simultaneous users. Flgure 1.2 Finding minimum value in a list
15
Algorithmic Problem Solving
with Python
Programming
and
Problem Soving
14 through
in a list 1s
demonstrated
gh
for solving the problem
ot hnding
the
minimum
Pseudocode 1.1
The step-wise approach Pseudocode
1.1.
Flowchart 1.1, and Read the count of numbers as N
Algorithm 1.1, Set I
Read the first element as MIN
Algorithm 1.1 While I <N 1
Read the next number as NUM
Step 1: start numbers as N
IF MIN < NUM
count of
the
Step 2: READ Set MIN = NUM
I = 8
Step 3: SET element as MIN Set I I +1
the first
Step 4: READ 6-8 WHILE I <N Print MIN
Step 5: REPEAT Steps NUN
next number as End
READ the
Step NUM Variables: I, N, NUM, MIN
Step 7: IF MIN <
SET MIN = NUM
I+1
SET I 1.6.2 Insert a Card in a List of Sorted Cards
=
Step 8:
PRINT MIN
into a sorted list of numbers. For this,
Inserting a card in a list of sorted cards is same as inserting an element
Step 9:
Step 10: End start from the end of the list and compare the new element with the elements of the list to find a suitable
position at which the new element has to be inserted. While comparing, also shift the elements one step ahead
to create a vacancy for the new element. Figure 1.3 given below illustrates this comcept
Flowchart 1.1
Is I 0 AND NO maximum number of guesses for linear search and binary search for a few number sizes:
SET List[il X
X<List[I]?
Max Linear Search Guesses Max Binary Search Guesses
VALUE OF N
Stop 10
10
SET List[I+1] = List[I] 100 100
hSET I =I - 1
1,000 1,000
|10,000 10,000
100,000 100,000
g h gnivior not lopoxgge oeivegofe erll 1,000,000 20
1,000,000
Pseudocode 1.2 an integer number in range is
The step-wise approach for solving the problem of guessing
a given
demonstrated through Algorithm 1.3, Flowchart 1.3, and Pseudocode 1.3.
Start
Read Number of elements in the
Set I=
sorted 1ist as N Algorithm 1.3
WHILE I <N
Step 1: Start
Read the Sorted 1ist element
Set I =I+1
as List[I] Step 2: SET I 0
numbers as N
Read element to be
inserted as X
Step 3: READ the range of
selected number from 1 to N
Set I = N-1 Step 4: SET NUM as a random.ly
WHILE I >= 0 AND Step 5: READ VAL as a value guessed by the user
List[I+1] = List[I)
X<List[I] SET I I+1
step 6:
Set I = I 1 Step 7: IF VAL NUM
List[I+1] =X THEN PRINT "YOU WIN.. You guessed the number in Ith Turn
End Go to Step 10
Algorithmic Problem Solving 19
Programming with Python
Problem Solving and
18
RG bpop
vsie Pseudocode 1.3
VAL >NUM
Step 8: I FPRINT "VALUE TOO HIGH..
PLAY AGAIN.."
THEN Start
Go to Step 5
Set I =
IF VAL <NUM
Step 9: Read the range of numbers as N
TOO LONW PLAY AGAIN.
THEN PRINT "VALUE Set NUM as a randomly selected number from 1 to N
Go to Step 5 owhum 75d Read VAL as a value guessed by the user
Step 10: End Set I = I +1
SETI e slog 2s ow zs ( )
1.6.4 Tower of Hanoi
9Upindosl dozs52 1892 sltiw
Read the range The tower of Hanoi is one of the main applications of recursion. It says, ifyou can solve n- cases, then you
of numbers as N can easily solve the nth case'.
We will discuss the tower of Hanoi problem one, using
two, then three rings.
and finally Figures1.4(a)and
(b) explain the working of the tower of Hanoi problem using one and two rings.
SETNUM as a randomly T0 BUJAV
selected number from 1 to N
OF
finding square
or logarithm of negative
execution, so the
code must be
written in such a way that it handles Exercises
terminate program
Run-time errors may
terminating
it u n e x p e c t e d l y Fill in the blanks
e r r o r s rather
sorts ot unexpected
generated when rules
of a progran
all
as compile-time
errors) are amming A jS a set of instructions that tells the 3. symbol is always the first and the last
errors (also
known
program line by line. The moment
the The
Syntax Errors Syntax (executes) each
instruction in the
computer how to solve a particular problem. symbol in a flowchart
violated. Python interprets execution of the program.
language are error, it stops
further 2. Algorithms are mainly used to achieve i s a form of structured English that describes
interpreter
encounters a syntactic rules of the programmint
with 3. and statements are used to change
are those errors which may comply ning algorithms.
Semantic errors the sequence of execution of instructions.
Semantic o r Logical Errors which is obviOusly not correct. For example, if you wri
write a 10 is used to express algorithms and as a mode
undesirable output Python will subtrars
language but gives an unexpected and the- symbol.Then 4. is a formally defined procedure for of human communication
of writing+ symbol, you put
program to add two numbers but instead
But, actually the output is difterent
from what you expected. performing some calculation. 11.
The process of dividing an algorithm into modules/
the numbers and returns
the result. and programmers . statements are used when the outcome of the functions 15 called
not detected by the compiler,
code. Such errors are
Logical errors are errors in the program locate and rectity the errors. Logical errors occur due to
process depends on some condition. 12. i s a technique of solving a problem by
line or use a debugger to 6. Repetition can be implemented using constructs such
must check their code line by breaking it down into smaller and smaller sub-
incorrect statements As and problems until you get to a small enough problem
linker is not able to find the function
definition for a given prototype 7. A complex algorithm is often divided into smaller that it can be easily solved.
Linker Errors These errors ocCur when the
units called
Programming means writing computer programs. While An algorithm 1s analysed to measure its performance in 1. An algor+thm solves a problem in a finite numberof steps. 6 Terminal symbol depicts the ffow of control of the
programming, the programmers takes an algorithm and terms of CPU time and memory space required to exe 2. Flowcharts are drawn in the early stages of program
code the instructions in a particular programming lan- cute that algorithm. formulating computer solutions. 7. Labelled connectors are square in shape.
guage, so that it can be executed by a computer. Sequence means that each step of the algorithm is exe 3. The main focus of pseudocodes is on the details of 8 The outputs of each step of an algorithm should be
An algornthm provides a blueprint to writing a program cuted in the specified order. the language syntax. unambiguous. This means that is should be precise.
to solve a particular probiem Decision statements are used when the outcome of the 4. Algorithms are implemented using a programming 9 You can have maximum one function in a algorithm.
In the course of processing, data 1S read irom an input process depends on some condition. language. 10. Pseudocode is written usng the syntax of a particular
device, stored in computer's memory for further pro- Iteration or Repetition involves executing one or more 5. Repetition means that each step of the algorithm is programming language.
cessing and then the result of the processing is written executed in a specified order.
steps for a number of times, can be implemented using
to an
output device constructs such as the while loops and for loops.
The data is stored in the computer's memory in the form Programming languages have
of variables or constants. a vocabulary O syi Multiple Choice Questions
and semantics for
For a complex problem, its instructing a computer to perto
algorithm is often divided specific tasks. 1. Algorithms should be (c) subroutines
intosmaller units called functions (or modules). (6) unambiguous (d) all of these
Pseudocode facilitates designers focus (a) precise
Algonthm validation ensures that the
algorithm will
to on the
loge (c) clear (d) all of these 6. A single operation which when executed convert one
work coectly irespective of the programming of the algorithm without getting bogged down by
lan- details of language 2. To check whether a given number is even or odd, you state to other is called
guage in which it willbe syntax.
implemented. will use which type of control structure? a) instruction (6)program
(a) sequence (b) decision ()software (d)control
Glossaryy (C) repetition (d) all of these 7 Algorithm is validated to
3. Which one of the following is a graphical or symbolic (a) measure its CPU time
Program A set of instructions that tells the representation of a process?
to solve
particular problem.
a computer how Pseudocode A compact and (b) measure the memory consumed
Algorithm A step by step nstructions that informal (a) algorithm (b) flowchart (c)check if it is correct
tells the
description of an algorithm that uses the hign ural
computer how to solve a particular conventions of a struc (c) pseudocode (d)program (d) all of these
Programming The act of wrting problem. programming
Flowchart A diagrammatic language. 4 In a flowchart, which symbol is represented using a 8 Programming languages have a vocabulary of_
Instruction A Sngle operation programs.
which when the representation
sequence of steps that must be that ilust
ates rectangle? tor instructing a computer to perform specific tasks.
convert one state to0 other executed performed to so (a) terminal (b) decision
problem. (a) syntax (6) semantics
Modularization The process of dividing an RecursioD A (C) activity (d) input output ()both of these (d) none of these
into modules/functions. algorithm breaking it downtechnique of solving a proble D 5. Which of the following details are omitted in 9. Syntax and semantics of a language are strictly
into smaller and
Programming language A
until you get to small smaller pseudocodes?
enough problem sub-proD
language checked in
to express
computations that can be specihcally
periormed
designed casily solved.
a
that it (a) variable declaration (a) algorithm (b) flowchart
by a computer. Ca (b) system specitie code (c)pseudocode (d) program
24 Problem Solving and Programming with Python
Review Questions
draw a flowchart that
11. Write algorithm and
an
1. Define an algorithm. How is it useful in the
context the user
an employee. Prompt
calculates salary of
of software development? and DA. Add
to enter the
Basic Salary, HRA, TA,
2. and compare the approaches for designing
Explain calculate the Gross Salary. Also
these components to
an algorithm. from the Gross Salary
to be paid
deduct 10% salary
3. What is modularization? as tax.
repetition, and decision algorithm and a
4. Explain sequence, Draw a flowchart and write an
12.
Also give the keywords used in each type problem statements
psuedocode for the following
statements.
gaahsotpego
obo Soiicogearibotuoo
nelesuoeoorosleu
o blaorletog
Answers
Fill in the Blanks toittaga
iledunge tolaordigg s wolollto
1. Program
2. software reuse
re4, Algorithm 20 0 7. functions or modules 10. Flowcharts
5. Decision
3. Decision, repetition
6. while,
8. Terminal symbols 11. Modularization
do-while, and for 9. Psuedocode 12. Recursion
loops
State True or False
True 2. True 3. True 4. False 5. False 6. False 7. False 8. False 9. False 10. False
Multiple Choice Questions
1. (d) 2. (b) 3. obepo
6) 4. ()5. (d) 6. (a) 7. (c) 8. (c) 9. (d)
stioo r