Introduction to
Algorithms and Python
Preparatory Year Program PYP-002
Announcement
➢Presentation 01
▪ Next Week (23 – 26 February).
▪ Deadline to submit the files in blackboard.
➢Quiz 02
▪ Week 09 (08 – 12 March) (During class time)
▪ Chapter 02 and 03 from LAB Manual
▪ Computer Networks and Cybersecurity
▪ Blockchain Networks, IoT and Immersive
Technologies
▪ MCQ’s
2
Objectives
➢Basics of Algorithms
➢Representing Algorithms
➢Introduction to Python Programming
➢Variables and Data Types
3
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.
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
Symbol Name Description
Represents start / end of
Oval
algorithm
Used for input or output
Parallelogram
operation
8
Used for processing
Rectangle
expressions
Can be used for decision
Diamond
making (Branching / Loops)
Indicates flow of logic in the
Flow Line
algorithm
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.
▪ 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.
➢An assignment statement assigns a variable with
a value, such as
▪ x=15
11
Variables
➢Simple variables hold the latest value inside
➢When a new value is assigned, the old is
replaced
x y
7
10 83
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
Print function (Output)
➢Print Hello World
▪ The first line is a comment. Comments start with a # and are ignored by
the interpreter
▪ The second line displays a line of text i.e., Hello, World! using the print
function
▪ A function is a collection of programming instructions that carry out a
particular task.
➢Comma can be used to combine variables of
different types in print function.
GPA=3.8
name=‘Mohammad'
print('Hello ',name,'\nYour GPA is = ',GPA)
14
Variable Input
➢Input numbers
▪ input() function always returns 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
15
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. Parenthesis ().
2. Exponentiation
3. Division, floor division, multiplication, remainder
4. Addition and subtraction
16
➢Compound Operators
▪ compound operators provide a shorthand way to
update a variable
Compound Operator Expression in Equivalent
python expression
Addition assignment age += 5 age = age + 5
Subtraction assignment age -= 5 age = age – 5
17
Python modules
➢Math module in python contains multiple functions inside
Math function Python syntax
Sin(x) [Link](x)
Cos(x) [Link](x)
Tan(x) [Link](x)
Radians [Link](angle)
𝑒5 [Link](5)
Ceiling 𝑥 [Link](x)
➢[Link](7.1) = ?
Floor 𝑥 [Link](x)
➢[Link](7.8) = ?
𝑥 [Link](x)
18
Python Programming
➢Google Colab IDE
▪ Gmail account required
▪ [Link]
19
Review Questions
➢Calculate the result of the following expressions
▪ 2**3+4/2
▪ 4*3+2*8/4**2
▪ [Link](19 / 4) + (4 ** [Link](5 * 0.5))
➢Convert the following mathematical expressions into
Python expressions. x and y are real number inputs
that are entered by the user
sin(𝑥)
1. 8 + 𝑦−5
∗9
13+𝑥 5
2.
𝑦+𝑐𝑜𝑠(𝑥)
20