0% found this document useful (0 votes)
3 views15 pages

Problem-Solving and Python Introduction

The document introduces problem solving in computer science, emphasizing the importance of defining problems, developing algorithms, and implementing solutions through programming. It outlines the steps of problem solving, characteristics of good algorithms, and methods of representing algorithms, such as flowcharts and pseudocode. Additionally, it provides an overview of Python, its features, advantages, limitations, and basic usage instructions for writing and executing Python programs.

Uploaded by

aaravgarg.del
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)
3 views15 pages

Problem-Solving and Python Introduction

The document introduces problem solving in computer science, emphasizing the importance of defining problems, developing algorithms, and implementing solutions through programming. It outlines the steps of problem solving, characteristics of good algorithms, and methods of representing algorithms, such as flowcharts and pseudocode. Additionally, it provides an overview of Python, its features, advantages, limitations, and basic usage instructions for writing and executing Python programs.

Uploaded by

aaravgarg.del
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/ 15

Unit II: Computational Thinking and Programming – 1

Introduction to Problem Solving

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.

Steps for Problem Solving

Analyzing the problem: It is important to clearly understand a


problem before we begin to find the solution for it. If we are not
clear as to what is to be solved, we may end up developing a
program which may not solve our purpose. Thus, we need to
read and analyze the problem statement carefully in order to list
the principal components of the problem and decide the core
functionalities that our solution should have.

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’:

1) Remove the bicycle from the stand,


2) Sit on the seat of the bicycle,
3) Start peddling,
4) Use breaks whenever needed and
5) Stop on reaching the destination.
Characteristics of a good algorithm

1. Precision – the steps are precisely stated (defined).

2. Uniqueness – results of each step are uniquely defined and only depend on the input and the result of
the preceding steps.

3. Finiteness – the algorithm stops after a finite number of steps.

4. Input – the algorithm receives input.

5. Output – the algorithm produces output.

2
6. Generality – the algorithm applies to a set of inputs.

** While writing an algorithm, it is required to clearly identify the following:

• The input to be taken from the user.


• Processing to be performed to get the desired result.
• The output desired by the user.

Representation of Algorithms: There are two common methods of representing an algorithm —


flowchart and pseudo code. Either of the methods can be used to represent an algorithm while
keeping in mind the following:

• It showcases the logic of the problem solution, excluding any implementation details.
• It clearly reveals the flow of control during execution of the program.

Flowchart — Visual Representation of Algorithms: A flowchart is a visual representation of an


algorithm. A flowchart is a diagram made up of boxes, diamonds and other shapes, connected by
arrows. Each shape represents a step of the solution process and the arrow represents the order or
link among the steps.

3
Example 1.

4
Example 2.

Example 3.

5
Pseudo-code

A non-formal language that helps programmers to write algorithm. It is a detailed


description of instructions that a computer must follow in a particular order. It is intended
for human reading and cannot be executed directly by the computer. No specific standard
for writing a pseudocode exists. The word “pseudo” means “not real,” so “pseudocode”
means “not real code”. Following are some of the frequently used keywords while writing
pseudocode:

• INPUT

• COMPUTE

• PRINT

• INCREMENT

• DECREMENT

• IF/ELSE

• WHILE

• TRUE/FALSE

Example:
Pseudocode for calculating area and perimeter of a rectangle.

INPUT length

INPUT breadth

COMPUTE Area = length * breadth

PRINT Area

COMPUTE Perim = 2 * (length + breadth)

PRINT Perim

6
Benefits of Pseudocode:

Before writing codes in a high level language, a pseudocode of a program helps in


representing the basic functionality of the intended program. By writing the code first in a
human readable language, the programmer safeguards against leaving out any important
step.

What is Computational Thinking?


It refers to the collective thought processes involved in formulating
problems and solutions so that they are represented in a form that can be effectively
carried out by a computer. It is an approach to solving problems using concepts and ideas
from computer science.

Characteristics of computational thinking (4 pillars of Computational Thinking):


It is a problem solving process that includes the following characteristics:
(A) Decomposition: Breaking down a complex problem data or process into smaller
parts to make it in simple form.
(B) Pattern recognition: It refers to the looking for patterns and trends in data.
(C) Abstraction: Showing the important information and hiding background details.
(D) Algorithm: Developing step by step instructions to solve the problem.

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.

EX: Scientist and engineers look for pattern in data

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 used in a variety of fields, including software development, web development, scientific


computing, big data and Artificial Intelligence.

Some of the features which make Python so popular are as follows:

❖ 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.

C Language Python Code


Code for for swapping
swapping values values
int a = 2, b = 3, a, b = 2, 3
temp; a, b = b, a
temp = a;
a = b;
b = temp;
3. Interpreted Language: Python interprets and executes the code line by line at a time. It
makes Python an easy–to–debug language and thus suitable for beginners and advanced users.
4. Completeness: Python Standard Library provides various modules for different
functionalities. For example, it can be used for different functionalities like email, web–pages,
databases, GUI development, network connections and many more. Hence no additional
libraries to be installed.
5. Cross–Platform Language (Platform Independent/Portable): Python can run on different
platforms like Windows, Linux / Unix, Macintosh, Super Computers, Smart Phones etc. Hence
it portable language.
6. Free and Open Source: Python is freely available at free of cost and its source code is
available to every body for further improvement.
7. Syntax Highlighting: It allows to distinguish between input, output and error messages by
different color codes.

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.

IDLE comprises of:


(i) Interactive Mode (Python Shell)
(ii) Script Mode (Python Editor)

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>)

>>>x = input ("Enter a Number")


>>>Enter a Number10

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

>>> a=int(input("Enter a Number"))


Enter a Number10

>>> print(a)
10

Understanding print( ):
To print or display output, Python provides print( ) statement. It can be used as
follows:

14
Syntax:

print (value,….., sep=’ ‘,end=’ ‘)

sep(separator): String inserted between values, default is space.

end: String appended after the last value, default is new line or \n.

Ex: print ("Hello World")

** In Python, a string may be provided in either double quotes or single quotes.

15

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