Section 3.3 ~ 3.4
Vocab
Algorithm: a finite set of instruction that accomplish a task, it can be expressed by natural language, diagrams, and various other ways
- Three parts of Algorithm: selection, sequence, and iteration
- Sequence: the order of how to do something to achieve a result, similarly to how you follow the instructions from a teacher
num1 = 5
num2 = num1*6 - 20
num3 = num1*num2 - 2*6
result = num1* 6 + num2/2 - (num3/2)
print(result)
- selection: it allows an algorithm to make a decision based on if a condition is met
x = 3
y = 2
if x > y:
print("{} is bigger than {}".format(x,y))
elif x < y:
print("{} is smaller than {}".format(x,y))
else:
print("{} and {} are same".format(x,y))
- Iteration: loop and doing something again until a condition is met
x = 0
a = 0
while x < 5:
a += x
print(a)
x +=1
- Addition: a + b
- Subtraction: a - b
- Multiplication: a * b
- Division: a/b
MO = 102 % 5
ad = 3 + MO
s = 6 - 7
m = 564*349
d = 1024/ 16
print(MO,ad, s, m, d)
Note
- Algorithms are a finite set of instructions that accomplish a task. it has three parts, sequence, selection, and iteration
- A sequence is the order of how to do something to achieve a result, similarly to how you follow the instructions from a teacher.
- A selection allows an algorithm to make a decision based on if a condition is met, an example of this is when your car is out of fuel, you go to the gas station to fill your car, but if your car is full you wouldn't go to the gas station.
- An iteration is a loop and doing something again until a condition is met, like you put away your computer when you are finished with your work.
- Arithmetic uses addition, subtraction, division, multiplication, and modulus operator
- Addition: a+b
- Subtraction: a-b
- Multiplication: a*b
- Division: a/b
- Modulus: a MOD b
- (a and b can be string or number)
- A string concatenation connects two or more string end-to-end to make a new string
- Len() gives the character number
- strings are variables and can be joined together through the print() command to make a statement
num1 = 5
num2 = num1 * 3
num3 = num2 / num1 * (9 % 2) * 4
result = (num3 % num1 + num2) % num3 * 3 / 5
- Iteration
- Selection
- Sequence