PseudocodetoPythonintroduction examples
PseudocodetoPythonintroduction examples
“Code that resembles a programming language but that uses a less strict syntax to express an algorithm and is
independent of any real programming language.”
Below are some examples of how the given pseudocode can be implemented using Python.
Examples
Sequence
Pseudocode Python
Assignment
Pseudocode Python
Age ← 17 Age = 17
Selection
Pseudocode Python
Count ← 10 Count = 10
WHILE Count <> 5 DO while Count != 5:
Count ← Count - 1 Count = Count - 1
ENDWHILE
FOR EACH x IN [2, 4, 6, 8] DO for x in range(2, 9, 2):
Print x print(x)
ENDFOR
x takes on the each value in turn as Tip: to check what values a range produces.
the loop repeats. In your console type
>>> list(range(2, 9, 2))
Exercises (assignment and sequence)
Have a go at writing the Python equivalents for the provided pseudocode.
Pseudocode Python
1 Discount ← 20 Discount = 20
5 Num ← 17 Num = 17
Remainder ← Num MOD 7 Remainder = num mod 7
6 Numbers ← [20, 13, 16, 34] Numbers = [20, 13, 16, 34]
8 Index ← 1 Index = 1
Print Numbers[Index + 1] Print(numbers[Index + 1])