To make a computer execute an algorithm, it must be written in a programming language. In this lesson, using Python-style notation, we will learn basic elements common to every language: variables, conditional branches, repetition, arrays, and functions.
Variables and Assignment
A variable is a "named box" in which a value is stored. Putting a value into a variable is called assignment and is represented by "=". Unlike the equals sign in mathematics, the programming "=" means "put the value on the right into the variable on the left."
x = 10 (put 10 into x)x = x + 5 (add 5 to the current value of x, 10, and put the resulting 15 into x again)After execution, the value of x is 15. "x = x + 5" is not an equation; it is an instruction to "increase x by 5."
Conditional Branching (if Statements)
Use an if statement to switch processing according to a condition. When the condition is true, the statements inside if are executed; when it is false, the statements inside else are executed.
score = 72
if score >= 60:
print("Pass")
else:
print("Fail")
In this example, score is 72 and "score >= 60" is true, so "Pass" is displayed. Conditions use comparison operators such as == (equal), != (not equal), <, >, <=, and >=.
Repetition (for and while Statements)
Use repetition to perform the same process many times. A for statement repeats a process for a specified number of times or range, while a while statement repeats it as long as a condition is true.
total = 0
for i in range(1, 11):
total = total + i
print(total)
range(1, 11) means "from 1 through 10" (note that 11 is not included). As i changes through 1, 2, 3, ..., 10, it is added to total, and finally 1 + 2 + ... + 10 = 55 is displayed.
Arrays (Lists) and Functions
Use an array (called a list in Python) to handle multiple values together. Write it as scores = [80, 65, 92], for example, and use scores[0] to retrieve the first element (80). Remember that index numbers start at 0.
A function is a named sequence of processes that can be reused. If you define one as def average(a, b): return (a + b) / 2, you can obtain 70 simply by calling average(80, 60). Functions organize programs and eliminate the need to write the same process repeatedly.
- "=" means assignment. "x = x + 5" means "increase x by 5."
- Use if–else for conditional branching. Conditions use comparison operators (==, >=, and so on).
- A for statement repeats a fixed number of times; a while statement repeats while a condition is true. range(1, 11) means 1–10.
- Array indexes start at 0. scores[0] is the first element.
- A function is a named group of processes. It improves program reusability.
Practice Questions
- After executing "a = 5" and then "a = a + 3," what is the value of a?
- When writing "for i in range(5):", how many times is the loop executed? What values does i take?
- In the program for the sum from 1 to 10, what is displayed if range(1, 11) is changed to range(1, 101)?
Answers and Explanations
- Answer: 8
Explanation: a = a + 3 assigns 8, obtained by adding 3 to the current value 5, back to a. - Answer: 5 times. i changes from 0 to 4.
Explanation: range(5) means 0, 1, 2, 3, and 4. When the starting value is omitted, it starts at 0, and the ending value itself is not included. - Answer: 5050
Explanation: This is the sum from 1 to 100. It can also be checked with the formula for the sum of an arithmetic sequence: 100×(1 + 100)÷2 = 5050.