v What is Algorithm
v Variables and operators
v How to write an algorithm -
Sequence
Algorithm
Algorithm
¯ a description of a solution to a problem step by step
¯ The word “algorithm” is derived from the ninth-
century Arab mathematician, Al-Khwarizmi who
worked on “written processes to achieve some goal”
Algorithm
Algorithms in our daily lives
Algorithm is a sequence of operations
¯ Recipe 1 2
¯ Manual
¯ Direction
¯ Schedule 3 4
¯ Study plan
¯ Travel plan
5 6
Furniture Assembly Instruction
Algorithm
Exact Instructions Challenge Peanut Butter Jelly Sandwich
When giving instructions to the computer,
the instructions need to be
¯ Clear
¯ Specific
¯ Complete
¯ Consistent
Source at [Link]
Algorithm Structure
An Algorithm Structure
Input Processing Output
Data Information
¯ Input: raw data
(e.g., numbers, text, sound, image, animations, video, and etc.)
¯ Processing converts it into meaningful information
¯ Output: the processed input
Algorithm Structure
Processing part is important
Processing
Input Output
ㆍ Variables
ㆍ Operators
Problem ㆍ Control Structure Solution
¯ Data representation: data types, variables
¯ Operators: arithmetic, relational, logical operators
¯ Control structure: sequence, conditional, iteration
Algorithm Structure
Processing part is important
Processing
Input Output
ㆍ Variables
ㆍ Operators
Problem ㆍ Control Structure Solution
¯ Data representation: data types, variables
¯ Operators: arithmetic, relational, logical operators
¯ Control structure: sequence, conditional, iteration
Variables and Assignment
Variables
A variable is a name associated with a value which can change
A variable is very useful when we need to use it over and over again
health name
68 Zelda
For instance, a variable “health" is a variable “name” is associated
associated with the number 68 with the text string “Zelda”.
Variables and Assignment
Assignment
Assignment is to assign a value to a variable using ‘=’ sign
A variable can store number, string, Boolean data types in Python
age = 20
Use ‘ ’ or “ ” when assigning text to a variable
name = ‘Charles’
Variables and Assignment
Assignment is different from equal in math
In mathematics, age = age + 1 is wrong
In computer science, age = age + 1 is correct
STEP 1 STEP 2 STEP 3
‘age’ gets the value of 20 Perform the operation 20 + 1 ‘age’ gets the value of 21
21
20 20 + 1 21
age age
Variables and Assignment
Variable Name
A variable in programming is often given a long name to help human
programmers comprehend its meaning
A variable can use alphabets, numbers, and _
Do not begin with a number
Example
populationOfKorea = 50000000
myAddress = ‘220 41w street New York City’
Operators
Mathematical Operators
¯ An operator is a symbol that tells the computer to perform a task
Operator Function Expression Description
+ addition a=b+c a gets the value of (b+c)
- subtraction a=b-c a gets the value of (b-c)
* multiplication a=b*c a gets the value of (b*c)
/ division a=b/c a gets the value of (b/c)
% remainder a=b%c a gets the value of (b%c)
// quotient a = b // c a gets the value of (b//c)
Operators
Mathematical Operator Precedence
¯ An operator is a symbol that tells the computer to perform a task
Precedence Operator Sign Expression
Highest () Parentheses
** Exponentiation
*, /, //, % Multiplication, division, quotient, remainder
Lowest +, - Addition, subtraction
a=2
b=3
a=a+b
b=a*b
a=2
b=5
a = 10 + a * b
b = 30 % (a - b)
Summary
Algorithm is a step by step description of solution to a problem
Basic components of algorithm are variables and operators
Operator Precedence