Introduction to
Algorithms and Python
Preparatory Year Program PYP-002
Objectives
Basics of Algorithms
Representing Algorithms
Introduction to Python Programming
Variables and Data Types
2
Introduction to Algorithms
An algorithm can be
defined in different ways:
Input List
A finite sequence of well-
defined instructions (step
by step procedure), to Algorithm
solve a particular A step-by-step procedure for solving
a problem or completing a task.
problem.
A step-by-step process of
solving any specific Output List
problem or doing a task is
called algorithm.
3
Qualities of a good algorithm
Finite number of steps
The steps are unambiguous. Meaning
that every step leads clearly to the next
one.
The algorithm terminates eventually
with desired result (output)
4
Different ways of representing algorithm
Pseudocode
The pseudocode is a written form
representation of the algorithm.
Flowchart
Flowchart is graphical representation of
algorithms.
5
Pseudocode (Example of Algorithm)
Write an algorithm to find the average of two
numbers entered by user.
Step-1: Start
Step-2: Declare variables num1, num2, Average
Step-3: INPUT num1, num2
Step-4: Average (num1 + num2)/2
Step-5: Display Average
Step-6: Stop
6
Flowchart
Flowchart is a diagrammatic or graphical
interpretation of an algorithm. It is very helpful to
explain any program to others and writing
programs.
A Flowchart
Illustrates the logic of an algorithm
Highlights on each particular step and their
interconnections
It also shows the control flow from one action to the
next
7
Symbols Used In Flowchart
8
Example of flowchart
Draw a flowchart to find average of two numbers entered by user.
Start
Input num1
Input num2
Average (num1 + num2)/2
Output Average
End
9
Python Programming
To implement the algorithm, a program must be
written in a programming language. A program is
a particular set of instructions for the computer to
follow.
Python is a High-Level Programming Language
The programs are written by following a certain
syntax. The syntax indicates how each
instruction should be written
10
Variables
To understand the concept of algorithms in computer
science one needs to understand how variables work.
Variable
A variable is a named piece of computer memory,
containing some information inside.
Think of a variable as a box with a name, where we
can "store" something for retrieval and use later.
A simple variable can hold one value at a time.
When a new value is assigned, the old value is lost
and cannot be recovered.
11
Variables
Simple variables hold the latest value inside
When a new value is assigned, the old is
replaced
12
Variables Types
Variables can hold different types of data
name = 'Ahmed'
name Ahmed
In the notebook, you can show the variables declared in
the program and their types when you click on {x}.
Variable Data type
name= 'Ahmed' String
x=5 Integer
y = 2.5 Floating point (Real
Numbers)
13
Variable Input
Input numbers
input() functions always return string (text) value
To get numbered input we can use the int (convert to
integer) or float (convert to real number with decimal
point) functions
side=input('Enter side of a square:')
side=int(side)
perimeter=4*side
print(perimeter)
Output:
Enter side of a square:4
16
14
Python Arithmetic Operators
Arithmetic operation Python symbol Output
Addition 9+6 15
Subtraction 9–6 3
Multiplication 9*6 54
Division 9/6 1.5
Floor division 9 // 6 1
Remainder 9%6 3
Exponentiation 9 ** 6 531441
Order of evaluation:
1. Exponentiation
2. Division, multiplication, remainder
3. Addition and subtraction
15
Python modules
Math module in python contains multiple functions inside
Math function Python syntax
Sin(x) math.sin(x)
Cos(x) math.cos(x)
Tan(x) math.tan(x)
Radians math.radians(angle)
math.exp(5)
math.ceil(7.1) = ? Ceiling math.ceil(x)
math.floor(7.8) = ? Floor math.floor(x)
math.sqrt(x)
16
Python Programming
Google Colab IDE
Gmail account required
https://colab.research.google.com/
17
Combining text and variable in
print
‘+’ symbol can be used within print function to
concatenate same input types (str + str)
float or int variables need to be converted to string
using str() function to concatenate with text
GPA=3.8
name= ‘Mohammad'
print('Hello ' + name)
print('Your GPA is = ' + str(GPA))
Comma can be used to combine inputs and
variables of different types in print function without
the need of conversion to str
GPA=3.8
name=‘Mohammad'
print('Hello ',name,'\nYour GPA is = ',GPA)
18
Review Questions
Calculate the result of the following expressions
2**3+4/2
4*3+2*8/4**2
13-math.ceil(19 / 4) + (4 ** math.floor(5 * 0.5))
Convert the following mathematical expressions into
Python expressions. x and y are real number inputs
that are entered by the user
19