0% found this document useful (0 votes)
56 views18 pages

Ch-3 Notes and Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views18 pages

Ch-3 Notes and Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

CHAPTER- 3rd

GETTING STARTED WITH PYTHON

Introduction to Problem Solving


In computer programming, selection structures run sections of code
based on logical conditions. Problem solving is the process of
identifying a problem, analyse the problem, developing program.

Steps for problem solving


An algorithm for the identified problem and finally implementing the
algorithm to develop.
There are 4 basic steps involved in problem solving:
 Analyze the problem
 Developing an algorithm
 Coding
 Testing and debugging

Analyze the problem


Analyzing the problem is basically understanding a problem very
clearly before finding its solution. Analyzing a problem involves:
 List the principal components of the problem
 List the core functionality of the problem
 Figure out inputs to be accepted and output to be produced

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.

Testing and Debugging


After writing program it has to be tested on various parameters to
ensure that program is producing correct output within expected time
and meeting the user requirement.
There are many standard software testing methods used in IT industry
such as:
 Component testing
 Integration testing
 System testing
 Acceptance testing

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.

Why do we need Algorithm?


 Algorithm helps programmer to visualize the instructions to be
written clearly.
 Algorithm enhances the reliability, accuracy and efficiency of
obtaining solution.
 Algorithm is the easiest way to describe problem without going
into too much details.
 Algorithm lets programmer understand flow of problem
concisely.
Characteristics of a good algorithm

 Precision - the steps are precisely stated or defined.


 Uniqueness - results of each step are uniquely defined and
only depend on the input and the result of the preceding
steps.
 Finiteness - the algorithm always stops after a finite
number of steps.
 Input - the algorithm receives some input.
 Output - the algorithm produces some output.

What are the points that should be clearly identified while


writing Algorithm?
 The input to be taken from the user
 Processing or computation to be performed to get the
desired result
 The output desired by the user

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.

Input: Two Numbers to be divided


Process: Divide number1 by number2 to get the quotient
Output: Quotient of division

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.

Keywords used in writing pseudo code


 INPUT
 COMPUTE
 PRINT
 INCREMENT
 DECREMENT
 IF/ELSE
 WHILE
 TRUE/FALSE

Pseudo Code Example

Example: Write an algorithm to display the square of a given


number.

Input, Process and Output Identification

Input: Number whose square is required


Process: Multiply the number by itself to get its square
Output: Square of the number
Algorithm

Step 1: Input a number and store it to num.


Step 2: Compute num* num and store it in square.
Step 3: Print square.

Pseudo Code

INPUT num
COMPUTE square = num*num
PRINT square

Flow chart

Flow of Control

An algorithm is considered as finite set of steps that are executed in a


sequence. But sometimes the algorithm may require executing some
steps conditionally or repeatedly. In such situations algorithm can be
written using
• Sequence
• Selection
• Repetition
Decomposition

 Decomposition means breaking down a complex problem into


smaller sub problems to solve them conveniently and easily.
 Breaking down complex problem into sub problem also means
analyzing each sub problem in detail.
 Decomposition also helps in reducing time and effort as different
subprograms can be assigned to different experts in solving
such problems.
 To get the complete solution, it is necessary to integrate the
solution of all the sub problems once done.

Familiarization with the basics of Python Programming:

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 Interpreter: Python interpreter must be installed on your computer,


to write and execute a Python program. It is also called a Python shell.
>>>: Symbol >>> is called Python prompt.

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)

Advantages of using interactive mode:


 It is convenient for testing a single line code for instant execution.

The disadvantage of using Interactive mode:


 In the interactive mode, we cannot save the statements for future use
and we have to retype the statements to run them again.
2. Script Mode

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:

 IDLE: Integrated Development and Learning Environment

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.

To run/execute a program in script mode:


 Open the program using an IDLE editor
 In IDLE, go to [Run]->[Run Module] to execute the program
 The output appears on the shell.
Example- Print “Hello Students” in Python Program using command line
prompt-
MEMORY BYTES
Algorithm refers to the rules to follow to solve a problem.
º An algorithm is defined as a step-by-step procedure designed to perform an operation which will lead to the desired
result, if followed correctly.
Algorithms have a definite beginning and a definite end, and a finite number of steps.
º Agood algorithm,which is precise, unique and finite, receives input and produces an output.
º Aflow chart is a type of diagram that represents the algorithm graphically using boxes of various kinds, in an order
connected by arrows.
An algorithm where all the steps are executed one after the other is said to execute in sequence.
Decision-making involves selection of one of the alternatives based on outcome of acondition.
There can be more than one approach to solve a problem and hence we can have more than one algorithm for a
particular problem.
The choice of algorithm should be made on the basis of time and space complexity.
Acomplete set of instructions written using a programming language is termed as a Program/Code/Program Code.
Python is a powerful and flexible programming language. It uses concise and easy-to-learn syntax which enables
programmers to write more codes and develop more complex programs in a much shorter time.
Python is a platform-independent programming language.
º Python interpreter executes one statement (command) at a time.
º Python provides two different ways to work withInteractive mode and Script mode.
Interactive mode does not save commands in the form of a program and the output is placed between
commands
as it is displayed as soon as we press the Enter key atter typing one or more commands.
Interactive mode is suitable for testing code.
º Pvthon is acase-sensitive language. Thus, Ram and ram are two different names in Python.
Python is an interpreted language.
º Python's interactive interpreter is also called Python Shell.
The programs written in Python are easily readable and understandable.
print() statement outputs an entire (complete) line and then goes to the next line for subsequent output(s).
Script mode is useful for creating programs and then running them later and getting complete output.
º Allthe programs in Python are saved with the .py or pyw extension.
OBJECTIVE TYPE QUESTIONS
1. Fill in the blanks.
(a) are aset of instructions given to the computer to solve a problem.
(b) A/An is defined as a finite sequence of steps required to get the desired output.
(c) A is a type of diagram that represents the algorithm graphically using boxes of
various types, connected by arrows.
(d) Pythonwas created by
(e) Python is called
language.
(f) Python syntax is
(e) In Python, asyntaxerror is detected bythe
(h) Python is independent language.
(i) To start Python from the command prompt, use the command
extension.
() Python programs are saved using the or

(k) Python programming can be done in .. and modes.


(|) There are no standard rules to write
(m) An is the process to write step-wise solution of agiven problem in English-like language.
(n) is the process of breaking down acomplex problem into smaller problems so that they
can be managed easily.
(o) is defined as anoutput statement todisplay the result of a code on the output screen.
2. State whether the following statements are True or False.
(a) Breaking down a complex problem into smaller parts is called decomposition.
(b) Pseudocode is the pictorial representation of an algorithm.
(c) Python is a dynamically typed language.
(d) Alt+R is used to repeat the last typed command in Python shell.
(e) Print and print are the same in Python.
(() sep argument is used with print() method.
(g) Python is a compiled high-level language.
(h) Python is a cross platform language.
(i) The default extension of Python program file is *.pyt.
(i) Python Editor refers to Script mode in Python.
(k) Ctrl+Q shortcut key combination is used to exit Python.
(0) Python code can run on a variety of platforms.
diagram.
(m) Arectangle represents a process in a flow chart
3. Multiple Choice Questions (MCQs):
(a) What is an algorithm?
() Aset of steps to solve a problem
(iü) Software that analyzes data
(iii) Ahardware device that stores data (iv) Allof these
flowchart?
(b) What shape represents adecision in a
(i) Diamond (ii) Rectangle
(ii) Oval (iv) Parallelogram
(c) Python was developed by
(i) Charles Babbage (ii) Guido van Rossum
(ii) Tim Berners Lee (iv) Robert E. Kahn
its source code too. It means Python is
(d) You don't have to pay for Python and you can view (ii) Free and Open source
(i) Freeware
(iii) Open source
(iv) Shareware

(e) ldentify the correct print() statement:


(i) print(Hello) (ii) print("Hello")
(ii) print('Hello") (iv) print("Hello')
(f) Python is a/an language.
(i) Compiled (ii) Interpreted
(ii) Compiled &Interpreted (iv) None of these
(g) The interactive interpreter of Python is termed as
(i) Python shell (iüi) Python Script mode
(iii) Python Editor mode (iv) Python command line
(h) The three greater than signs (>>>) are called the Python
(i) Cursor (ii) Command prompt (ii) Pointer (iv) Blinking cursor
(i) Which of the following codes is correct?
()) print ("Programming is fun")
print ("Python ")
print ("Computer Science")
(i) print ("Programming is fun)
print ("Python")
print ("Computer Science)
(ii) Print ("Programming is fun ")
print ("Python")
print ("Computer Science")
(iv) Print ("Programing is fun")
Print ("Python")
Print ("Computer Science")
G) Python is a case-sensitive language. This means
(i) Capital and smallletters are same for Python.
(ii) Python doesn't care about the case of alphabets.
(ii) Python treats capital and small letters differently.
(iv) Python automatically capitalizes the small letters.
(k) For which set of values will the Python code (s=(a**4) +5*5**(b+b)) give the output as:
141?
(i) a=1, b=2 (ii) a=3, b=2
(iii) a=2, b=1 (iv) a=3, b=1
(1) mode of Python gives instant result of typed statement.
(i) Interactive mode (ii) Script mode
(ii) Both interactive and script mode (iv) None of these

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

<is a<=10? False print sum


True
F=F*a
Sum=Sum+F
a=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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy