0% found this document useful (0 votes)
4 views

PseudocodetoPythonintroduction examples

The document explains pseudocode, which is a simplified way to express algorithms independent of programming languages. It provides examples of how pseudocode can be translated into Python code for various programming constructs such as sequence, assignment, selection, and repetition. Additionally, it includes exercises for practicing the conversion of pseudocode to Python.

Uploaded by

73956
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PseudocodetoPythonintroduction examples

The document explains pseudocode, which is a simplified way to express algorithms independent of programming languages. It provides examples of how pseudocode can be translated into Python code for various programming constructs such as sequence, assignment, selection, and repetition. Additionally, it includes exercises for practicing the conversion of pseudocode to Python.

Uploaded by

73956
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Pseudocode

“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

Input Name Name = input("Your name: ")


Input Age Age = int(input("Your age: "))
Print Name, " in dog years you print(Name, "in dog years you
are" are", Age * 7)
Output Age * 7

Assignment
Pseudocode Python

Age ← 17 Age = 17

City ← "Manchester" City = "Manchester"

Names ← ["Bob", "Baz", "Ann"] Names = ["Bob", "Baz", "Ann"]

Selection
Pseudocode Python

IF BobMood = "happy" THEN if BobMood == "happy":


BobEmotion = ":)" BobEmotion = ":)"
ENDIF

IF Lives > 0 THEN if Lives > 0:


Print ‘Carry on!’ print("Carry on!")
ELSE else:
Print ‘Game over.’ print("Game over.")
ENDIF
IF Order < 50 THEN if Order < 50:
Discount ← 5 Discount = 5
ELSEIF Order < 100 THEN elif Order < 100:
Discount ← 10 Discount = 10
ELSEIF Order < 200 THEN elif Order < 200:
Discount ← 15 Discount = 15
ELSE else:
Discount = 20
Discount ← 20
ENDIF
Repetition
Pseudocode Python

FOR Num ← 1 To 10 DO for Num in range(1, 11):


Print Num print(Num)
ENDFOR

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.

Write your answers in Python column next to the pseudocode.

Fill this document in electronically please.

Pseudocode Python

1 Discount ← 20 Discount = 20

2 Nickname ← "Jolly Green Giant" Nickname = “Jolly Green Giant”

3 Input Name Name = Input(‘name: ’)

4 Ptr ← Ptr + 1 Ptr = ptr + 1

5 Num ← 17 Num = 17
Remainder ← Num MOD 7 Remainder = num mod 7

6 Numbers ← [20, 13, 16, 34] Numbers = [20, 13, 16, 34]

7 Print Numbers[3] Print(numbers[3])

8 Index ← 1 Index = 1
Print Numbers[Index + 1] Print(numbers[Index + 1])

9 Input Number1 Number1 = Input(‘number 1:’)


Input Number2 Number2 = Input(‘number 2: ‘)
Sum ← Number1 + Number2 Sum = number1 + number2
Average ← Sum / 2 Average = sum / 2
Output Sum, Average Return sum, average

You might also like