Ch-3 Notes and Questions
Ch-3 Notes and Questions
Developing an Algorithm
A set of precise and sequential steps written to solve a problem
The algorithm can be written in natural language
There can be more than one algorithm for a problem among
which we can select the most suitable solution.
Coding
Algorithm written in natural language is not understood by computer
and hence it has to be converted in machine language. And to do so
program based on that algorithm is written using high level
programming language for the computer to get the desired solution.
What is Algorithm?
A set of precise, finite, and sequential set of steps written to solve
a problem and get the desired output.
Algorithm has definite beginning and definite end.
It lead to desired result in finite amount of time of followed
correctly.
Representation of Algorithm
An algorithm can be represented in two ways:
• Flow chart
• Pseudo code
Flow chart
Flow chart is visual representation of an algorithm.
It's a diagram made up of boxes, diamonds and other
shapes, connected by arrows.
Each step represents a step of solution process.
Arrows in the follow chart represents the flow and link
among the steps.
Example 1: Write an algorithm to divide a number by another and
display the quotient.
Algorithm
Step 1: Input a two numbers and store them in num1 and num2
Step 2: Compute num1/num2 and store its quotient in num3
Step 3: Print num3
Flow Chart
Pseudo code
Pseudo code means 'not real code'.
A pseudo code is another way to represent an algorithm.
It is an informal language used by programmer to write
algorithms.
It does not require strict syntax and technological support.
It is a detailed description of what algorithm would do.
It is intended for human reading and cannot be executed directly
by computer.
There is no specific standard for writing a pseudo code exists.
Pseudo Code
INPUT num
COMPUTE square = num*num
PRINT square
Flow chart
Flow of Control
Introduction
Python is an open-source, object-oriented, high-level programming
language developed by Guido Van Rossum in 1990, and released in 1991, at
the National Research Institute for Mathematics, Netherlands.
Presently owned by Python Software Foundation (PSF).
Python is influenced by two programming languages:
ABC language, a teaching language created to replace the
programming language BASIC.
Modula-3 is a language that preserves the power of a systems
programming language
Python is a general-purpose programming language that can be
used to build any kind of program that does not require direct access
to the computer's hardware.
Characteristics of Python
It supports functional and structured programming methods as well
as OOP.
It can be used as a scripting language or can be compiled to byte-
code for building large applications.
It provides very high-level dynamic data types and supports dynamic
type checking.
It supports automatic garbage collection.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
It can be used to develop a large application with small codes.
Python - Advantages
It has become popular because of its salient features -
Easy - loosely typed OOP language with few keywords and simple
English like structure, and is easy to learn
Takes less time to develop the program, typically 3-5 times shorter
than equivalent Java programs. Due to Built-in high-level data types,
and dynamic typing.
Free and Open Source, High-Level, Portable, and Object-Oriented
Extensible & Embeddable
Interpreted - Python is interpreted, interactive, and directly executed
with pre-compiled code. This means that it is processed at runtime by
the interpreter and you need not compile your program before
executing it.
Large Standard Library and support GUI Programming (using MFC,
Tkinter)
Python is used for both scientific and non-scientific programming.
Python is a case-sensitive programming language.
Python - Disadvantages
It has some minus i.e., disadvantages -
Not the fastest Languages - Python is an interpreted language, not a
compiled language. It converts python source code into the internal
bytecode, which is then executed by the Python interpreter. It makes
slower than fully compiled languages.
Not strong as Type Binding - Python does not strongly bind with data
types i.e., type.
Not easy to convert code into other languages - It is not easy to
translate a Python program into other programming languages
because Python does not have strong syntax.
Working with Python:
Python prompt: Python prompt, which indicates that the interpreter is ready
to receive instructions. We can type commands or statements on this
prompt for execution.
Execution Mode
There are two ways to run a program using the Python interpreter:
1. Interactive mode, and
2. Script mode
1. Interactive Mode
In the interactive mode, we can type a Python statement on the >>> prompt
directly. As soon as we press enter, the interpreter executes the statement
and displays the result(s)
In the script mode, we can write a Python program in a file, save it and then
use the interpreter to execute the program from the file.
Python program files have (.py) extension.
Python programs are also known as scripts.
Python has a built-in editor called IDLE which can be used to create
programs/ scripts.
Python IDLE:
To Create a Program
First open the IDLE,
Click File>New File to create a new file,
then write your program on that file and
save it with the desired name.
By default, the Python scripts are saved in the Python installation
folder.
SOLVED QUESTIONS
1. What is Python?
Ans. Python is a high-level, interpreted, dynamic, Object-Oriented Programming Language that supports G
programming.
2. Why is Python interpreted?
Ans. Python is interpreted because the program is processed at run-time by the interpreter., i.e., convertins
source code into machine code line by line, and we do not need to compile the program beroe
executing it.
3. Who developed Python?
Ans. Python was developed by Guido van Rossum in the early nineties at the National Research Institute for
Mathematics and Computer Science in the Netherlands.
4. Write the full form of "IDLE".
Ans. Integrated Development Learning Environment
5. Why is Python easy to learn?
Ans. Python has relatively few keywords, simple structure and a clearly defined syntax. This allows anyone to
understand and work easily in a relatively short period of time.
6. Write any one feature of Python library.
Ans. Python library is very portable and cross-platform compatible with UNIX, Windows and Macintosh.
7. Is Python a compiled or an interpreted language? [HOTS]
Ans. The normalexecution of Python program is interpreted. However, subsets of the language can be compiled.
8. In which mode of Python is the interpreter no longer active?
Ans. Script Mode Programming.
9. Python is a free and open-source language. What do you understand by this feature?
Ans. Python is a free and open-source language signifies that we do not have to pay anything to download it.
Also, being an open-source language, its source code is also available if we wish to modify it.
10. State some salient features of Python.
Ans. Python is amodern, powerful, interpreted language with objects, modules, exceptions (or interrupts) and
automatic memory management. It was introduced to the world by Guido van Rossum in the year 1991.
Salient Features of Python are:
Simple and Easy: Python is a simple language that is easy to learn.
Free/Open Source: Anybody can use Python without the need to purchase a licence.
High-level Language: Being ahigh-level language, it can be easily understood by the user without the
need to be concerned with low-level details.
Portable: Python codes are machine and platform-independent.
Extensible and Embeddable: Python programs support usage of c/C++ codes.
Standard Library: Python standard library contains pre-written tools for programming.
11. Distinguish between other programming languages and Python.
Ans. Python can be distinguished from other programming languages on the following bases:
1. Python programs run slower than other parallelprogramming language codes, but Python saves much
time and space. Python programs are 3 to 5 times smaller.
2. Python is a loosely-typed dynamic language as no time gets wasted in declaring variable types as in other
programming languages.
3 Python is easier to learn as compared to other object oriented programming languages.
12. Describe flow charts and pseudocode.
Ans. Flow charts are diagrams that show the step-by-step solution to agiven problem.
Pseudocode is a simple and informal way of writing programming code in English. There are no standard
rules to write pseudocode.
13. Write a pseudocode to convert temperature in Celsius into Fahrenheit.
Ans. Input Celsius
Set Fahrenheit by multiplying Celsius by 1.8 and then adding 32
Print the Celsius and Fahrenheit.
14. Write a pseudocode to calculate and display Area and Perimeter of a rectangle.
Ans. Input length of the rectangle
Input breadth of the rectangle
Set the area by multiplying length and breadth
Set the perimeter by adding length and breadth and then multiply it by 2.
Display area and perimeter.
15. Write a pseudocode to find the largest number among the three inputted numbers.
Ans. Input 3 numbers A, B, C.
If Ais greater than B and A is greater than C,
then display 'A' is the largest number,
otherwise display 'C is the largest number.
If Bis greater than C, then display 'B' is the largest number,
otherwise display 'C is the largest number.
16. Draw aflow chart to accept a number and display its table.
Ans.
Start
input n
a=1
print a*n
True
a=a+1 is a<=10?>
False
Stop
17. Drawa flow chart to display sum of the
following series:
S=1!+2!+3!+...+10!
Ans.
Start
F=1
sum=0
a=1
Stop
18. What is the difference between Interactive mode and Script mode in Python?
Ans. In Interactive mode, instructions are given in front of command prompt (>>>) in Python Shell. Python carries
out the given instructions and shows the result there itself.
InScript mode, Python instructions are stored in a file, generally with .py extension, and executed together
inone go as a unit. The saved instructions are known as Python script or Python program.
19. Explain how Python is interpreted.
Ans. Python program runs directly from the source code. Each time Python programs are executed, the code
is required. Python converts source code written by the programmer into intermediate language which
is again translated into native language/machine language that is then executed. So, Python is an
interpreted language.
20. Give the output for the following:
(i) print ('apple 'mango', 'orange', 'banana', sep='@')
(ii) print (1,2,3,4,5, end=**** Sep='%)
(ii) print (2,4, 6, 8,10) ;print (3,6,9, 12,15)
Ans. (i) apple@mango@orange @banana
(ii) 1%2%3%45****
(ii) 2 4 6 8 10
3 69 12 15
21. Give the output for the following:
(i) print ("Hello")
(ii) print (5.5)
(ii) print (4+6)
(iv) print (a=10)
Ans. (i) >>>print ("Hello")
Hello
(ii) >>> print (5.5)
5.5
(iii) >>> print (4+6)
10
(iv) >> print (a=10)
Error (Assignment is
not permitted in
print() method
22. Can you come up with an algorithm to make a cup of tea?
Ans. (i) Put water into kettle
(ii) Turn kettle on
(iii) Get a cup
(iv) Put tea bag into cup
(v) When water gets boiled, add to cup
(vi) Add milk and sugar if needed
(vii) Stir with spoon
(viii) Remove tea bag and drink the tea
23. Draw a flow chart to accept a number and find its factorial.
Ans.
Start
Read n
i=1
fact=1
False
Is i<=n?
True
fact=fact*i
i=i+1
Print fact
Stop
24. Complete the following flow chart andfind sum of first 20natural numbers.
Start
A=1
SUM=0
(i) False
Print (iv)
True
SUM= (ü)
A= (ii)
Stop
Ans. (i) Is A<=20?
(iü) sUM=SUM+A
(iii) A=A+1
(iv) print SUM
25. What is Decomposition?
Ans. Decomposition involves breaking down a complex problem or system into smaller parts that are more
manageable and easier to understand. The smaller parts can then be examined and solved, or des
individually, as they are simpler to work with.