Problem-Solving and Python Introduction
Problem-Solving and Python Introduction
Introduction
We usually use the term computerization to indicate the use of computer to develop software in
order to automate any routine human task efficiently. Computers are used for solving various day-
to-day problems and thus problem solving is an essential skill that a computer science student
should know. It is pertinent to mention that computers themselves cannot solve a problem. Precise
step-by-step instructions should be given by us to solve the problem. Thus, the success of a
computer in solving a problem depends on how correctly and precisely we define the problem,
design a solution (algorithm) and implement the solution (program) using a programming
language. Thus, problem solving is the process of identifying a problem, developing an algorithm
for the identified problem and finally implementing the algorithm to develop a computer program.
GIGO (Garbage In Garbage Out): The correctness of the output that a computer gives depends
upon the correctness of input provided.
1
2- Developing an Algorithm: It is essential to device a solution before writing a program code for
a given problem. The solution is represented in natural language and is called an algorithm. We can
imagine an algorithm like a very well-written recipe for a dish, with clearly defined steps that, if
followed, one will end up preparing the dish.
3. Coding: After finalizing the algorithm, we need to convert the algorithm into the format which
can be understood by the computer to generate the desired solution. Different high level
programming languages can be used for writing a program.
4. Testing and Debugging: The program created should be tested on various parameters. The
program should meet the requirements of the user. It must respond within the expected time. It
should generate correct output for all possible inputs. In the presence of syntactical errors, no
output will be obtained. In case the output generated is incorrect, then the program should be
checked for logical errors, if any.
Algorithm: An algorithm is a set of step-by-step procedures, or a set of rules to follow, for completing
a specific task.
** The origin of the term Algorithm is traced to Persian astronomer and mathematician, Abu Abdullah
Muhammad ibn Musa Al-Khwarizmi (c. 850 AD) as the Latin translation of Al-Khwarizmi was called
‘Algorithmi’.
In our day-to-day life we perform activities by following certain sequence of steps. Examples of
activities include getting ready for school, making breakfast, riding a bicycle, wearing a tie,
solving a puzzle and so on. To complete each activity, we follow a sequence of steps. Suppose
following are the steps required for an activity ‘riding a bicycle’:
2. Uniqueness – results of each step are uniquely defined and only depend on the input and the result of
the preceding steps.
2
6. Generality – the algorithm applies to a set of inputs.
• It showcases the logic of the problem solution, excluding any implementation details.
• It clearly reveals the flow of control during execution of the program.
3
Example 1.
4
Example 2.
Example 3.
5
Pseudo-code
• INPUT
• COMPUTE
• INCREMENT
• DECREMENT
• IF/ELSE
• WHILE
• TRUE/FALSE
Example:
Pseudocode for calculating area and perimeter of a rectangle.
INPUT length
INPUT breadth
PRINT Area
PRINT Perim
6
Benefits of Pseudocode:
7
DECOMPOSITION:
It is the process of breaking down a big or complex problem into a set of smaller sub-
processes to allow us to understand and execute the problem better.
Decomposition is done by:
1) Dividing a task into a sequence of subtasks.
2) Identify elements or parts of a complex system.
Ex: while writing a program divide it into functions.
8
PATTERN RECOGNITION
It refers to observing or looking for similarities or pattern among and within small,
decomposed problems. The identified pattern helps to solve more complex problems more
efficiently.
Pattern recognition is done by:
1) Identify similarities or common differences that lead us to shortcuts
2) Using identified shortcut, mapping problems to possible solution.
ABSTRACTION
It refers to focusing on information relevant to a context or problem and hiding the
background details. It is done by:
1) Recognizing the context.
2) Identifying the information relevant to the context.
Ex: A calculator that shows only numbers and operators.
ALGORITHM
An algorithm is a sequence of steps that solves a problem by working on some input data
and producing a desired outcome. Algorithm design involves both creation and execution
of an algorithm. Algorithm Design is done by:
1) What steps are needed to solve?
2) How can the steps best be organized?
9
MEMORY MAP
10
PYTHON INTRODUCTION:
Python was created by Guido Van Rossum when he was working at CWI (Centrum Wiskunde &
Informatica) which is a National Research Institute for Mathematics and Computer Science in
Netherlands. The language was released in 1991. Python got its name from a BBC comedy series
from seventies called “Monty Python’s Flying Circus”. It is based on ABC programming languages.
Presently it is owned by Python Software Foundation (PSF).
❖ It is a general-purpose programming language which can be used for both scientific and non-
scientific programming.
11
❖ It is free, open source and portable language.
❖ It is a very simple, high level, loosely typed language with vast library of add–on modules.
❖ It is excellent for beginners as the language is interpreted (It is processed at run-time by the
interpreter), hence gives immediate results.
❖ The programs written in Python are easily readable and understandable.
❖ It is easily compatible with other languages like C, C++ etc.
❖ It is easy to learn and use.
❖ It is case sensitive. i.e. Uppercase and Lowercase alphabets are different.
ADVANTAGES OF PYTHON:
1. Easy to Use: Python is compact and very easy to use Object Oriented language with very
simple syntax rules. It is programmer–friendly.
2. Expressive Language/Higher Productivity: Because of simple syntax and fewer lines of
code, it is more capable to express code's purpose than many other languages and hence
program development time is less.
Limitations of Python:
1. Not the Fastest Language: Fully compiled languages are faster than interpreted languages.
12
Python is an interpreted language. Hence Python is not faster compared to fully compiled
languages.
2. Lesser Libraries: Python offers library support for almost all computing programs, but its
library is still not competent with languages like C, Java, Perl as they have larger collection of
libraries.
3. Not Strong on Type–Binding: Python is not strong on catching 'Type Mismatch'. For
example, a String value can be stored in a variable declared as an integer, where Python never
complain about it.
4. Not Easily Convertible: The syntax of Python language is simple but different compared to
other programming languages. Hence it is not easy to convert a program which is in Python
into other language.
Installing Python:
https://www.python.org/downloads/
Python distribution comes with:
• Python IDLE
• Python Interpreter
• PIP (Python Package Installer/Manager)
WORKING IN PYTHON:
To write and run Python program, install Python interpreter in computer. IDLE (GUI
integrated) is the standard, most popular Python development environment. IDLE is an acronym of
Integrated Development and Learning Environment. It lets edit, run, and browse and debug Python
Programs from a single interface. This environment makes it easy to write programs.
Interactive Mode: As the name suggests, this mode allows to interact with OS and as soon as you type
command, you can see the output in the same window. This mode does not save commands in form of
a program. Some points to remember while working in interactive mode are:
✓ This mode is suitable when running a few lines of code.
✓ The symbol '>>>' is called Python command prompt, indicates that interpreter is ready to accept
command.
✓ At the prompt, either a command or statement or expression can be given.
✓ Alt+P and Alt+N are used to invoke and repeat prior commands provided interactive window.
✓ The quit( ), exit(), Alt+F4, Ctrl+D or Ctrl+Q is used to exit Python command prompt.
13
✓ Ctrl+F6 will restarts the shell.
✓ Type credits at the prompt to get information about the organization involved in Python
development.
✓ The commands copyright and license( ) can be used to know more about Python.
✓ The command help( ), with empty parenthesis, will provides an interactive help.
✓ The command help( ), with a command inside parenthesis, will provide an exclusive
help about required command.
✓ To leave help and return to interactive mode, quit command can be used.
Script Mode: In script mode, Python program is to be typed in a file and then interpreter to be used to
execute the content of the file. Working in interactive mode is convenient for beginners and for testing
small pieces of code. But for coding more than few lines, code is to be saved so that it can be modified
and reused.
To create and run a Python script, the following steps to be used in IDLE:
1. File->Open OR File->New Window (for creating a new script file)
2. Write the Python code as function i.e. script(extension .py or .pyw)
3. Save it (Ctrl+S)
4. Execute it in interactive mode, by using RUN option (F5)
Understanding input():
The input( ) function is used to input values during runtime of a program. But, this function always
returns a value of string type. i.e. even a number inputted using input( ) method is not a number and is
a string.
Syntax: variable = input(<Message to be displayed>)
In the above case the value 10 inputted is assumed as string. Hence type of x is string.
To input as a number it is to be appropriately converted into desired data type, like below
>>> print(a)
10
Understanding print( ):
To print or display output, Python provides print( ) statement. It can be used as
follows:
14
Syntax:
end: String appended after the last value, default is new line or \n.
15