Dr Alexander Fletcher
A program is a sequence of instructions for the computer to follow.
Let's think about telling someone how to cross the road.
|Variables||to store data and information, can be of various types||
|Mathematical operations||add, multiply, etc.||
|Input and output operations||communicate with the user and the World||
|Loops||for repeating a set of steps||
|Conditional statements||for checking if you want to do one thing or another||
|Functions / procedures||to encapsulate a sequence of steps you perform often||
Writing in Python does not come until step 3!
Often you will go back to previous steps.
Often you will program a little bit at a time.
(Break the task down.)
Task. For each number from 1 to 50,000 find its factors by trial division.
[Trial division means that to find if x is a factor of n you try to divide n by x and see if you have any remainder.]
1 2 3 4 5 6 7 8 9
n = 1 while n <= 50000: print("The factors of", n, "are:") i = 1 while i <= n: if n % i == 0: print(i) i = i + 1 n = n + 1
Line 2 starts a loop that will repeat lines 3–9 from
Line 5 starts a loop that will repeat lines 6–8 from
Line 7 prints
i to screen if
i is a factor of
(We will discuss refactoring and debugging in the next two lectures.)
if (boolean_condition): do this followed by this carry on here
password = input("Enter password: ") if password == "parrot": print("Correct") print("Thank you")