Year 10 · Computer Science · Summer 1

Programming · Lesson 1 of 5

Variables, types, input and output in Python and pseudocode

Practical8.1 Programming concepts

Year 10 · Computer Science · Programming · Lesson 1 of 5

You will learn to

  • Declare variables and constants with the right data type: INTEGER, REAL, CHAR, STRING or BOOLEAN.
  • Use input and output in Python and in Cambridge pseudocode.
  • Compare a program in Python with the same program in pseudocode, line by line.

Year 10 · Computer Science · Programming · Lesson 1 of 5

Success criteria

You have got it when you can say:

  • I can choose a data type for a name, an age, a price and a yes/no answer.
  • I can write the same input–process–output program in both languages.
  • I can explain why a value that never changes should be a constant.

Year 10 · Computer Science · Programming · Lesson 1 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last unit

    The four stages of the program development life cycle are…

    A Analysis, design, coding, testingB Fetch, decode, execute, storeC Input, process, output, storage

    A. Analysis, design, coding, testing

  2. Last unit

    Bubble sort stops when…

    A Half the list is sortedB A whole pass makes no swapsC The first swap happens

    B. A whole pass makes no swaps

  3. Earlier this year

    Parity can…

    A Detect one flipped bit but not fix itB Detect two flipped bitsC Detect and fix any error

    A. Detect one flipped bit but not fix it

Year 10 · Computer Science · Programming · Lesson 1 of 5

Key words (1 of 5)

Variable
A named place in memory whose value can change while the program runs.
Constant
A named value that is set once and cannot change while the program runs.
Identifier
The name given to a variable, constant, array or subroutine.
Data type
The kind of value stored: INTEGER, REAL, CHAR, STRING or BOOLEAN.

Year 10 · Computer Science · Programming · Lesson 1 of 5

Key words (2 of 5)

Assignment
Storing a value in a variable, written with ← in pseudocode.
Sequence
Statements run one after another in the order they are written.
Selection
Choosing which statements to run using a condition (IF or CASE).
Iteration
Repeating a block of statements in a loop.

Year 10 · Computer Science · Programming · Lesson 1 of 5

Key words (3 of 5)

Count-controlled loop
A FOR loop that repeats a set number of times.
Pre-condition loop
A WHILE loop that checks its condition before each pass, so it may run zero times.
Post-condition loop
A REPEAT UNTIL loop that checks its condition after each pass, so it runs at least once.
Totalling
Keeping a running total by adding each value to a total variable.

Year 10 · Computer Science · Programming · Lesson 1 of 5

Key words (4 of 5)

Counting
Adding 1 to a counter variable each time something happens.
Procedure
A named subroutine that does a task and is run with CALL; it does not return a value.
Function
A named subroutine that returns a single value to where it was called.
Parameter
A variable in a subroutine heading that receives a value when the subroutine is called.

Year 10 · Computer Science · Programming · Lesson 1 of 5

Key words (5 of 5)

Local variable
A variable declared inside a subroutine that can only be used inside it.
Global variable
A variable declared in the main program that can be used anywhere in the program.
Library routine
A ready-made, tested subroutine provided with the language, such as ROUND or RANDOM.
Array
A data structure holding many values of the same data type under one name, each found by its index.

The explanation · step 1 of 23 · part 1 of 3

Variables: named boxes in memory

A variable is a named place in memory that holds one value. The value can change while the program runs.

The name is called an identifier. Cambridge pseudocode writes identifiers in PascalCase, such as `TotalScore`.

`DECLARE` gives the name and the data type before the variable is used.

The arrow `←` means assignment: work out the value on the right, then store it in the variable on the left.

The explanation · step 1 of 23 · part 2 of 3

Variables: named boxes in memory (continued)

e.g.Example
Score ← 0 stores 0. Score ← Score + 5 reads the old value (0), adds 5, and stores 5. Output: 5
DECLARE Score : INTEGER
Score ← 0
Score ← Score + 5
OUTPUT Score
score = 0
score = score + 5
print(score)

The explanation · step 1 of 23 · part 3 of 3

Variables: named boxes in memory (continued)

?Why?

Python does not need a declaration. It works out the type from the value. Cambridge pseudocode declares every variable so the type is clear to the reader.

!Watch out

Read `Score ← Score + 5` from right to left. It is not a maths equation. It means: work out Score + 5, then put the answer back in Score.

The explanation · step 2 of 23 · part 1 of 3

Constants: values that never change

A constant is a named value that is set once. It cannot change while the program runs.

In pseudocode write `CONSTANT`, the name, `←` and the value. No data type is given: the value shows it.

Python has no true constants. Programmers write the name in CAPITALS to mean: do not change this.

The explanation · step 2 of 23 · part 2 of 3

Constants: values that never change (continued)

e.g.Example
Spain's standard VAT (IVA) is 21%, so the rate is 0.21. 50.0 × (1 + 0.21) = 50.0 × 1.21 = 60.5 Output: Price with VAT: 60.5
CONSTANT VATRate ← 0.21
DECLARE Price : REAL
Price ← 50.0
OUTPUT "Price with VAT: ", Price * (1 + VATRate)
VAT_RATE = 0.21
price = 50.0
print("Price with VAT:", price * (1 + VAT_RATE))

The explanation · step 2 of 23 · part 3 of 3

Constants: values that never change (continued)

?Why?

If the VAT rate changes, you edit one line instead of every line that uses 0.21. The name also tells the reader what 0.21 means. The program cannot change it by mistake.

!Watch out

Use a constant only for a value that is fixed for the whole run, like a tax rate or the size of an array. A value that the program updates, like a score, must be a variable.

The explanation · step 3 of 23 · part 1 of 4

The five data types

A data type says what kind of value a variable holds. It decides how the value is stored and what you can do with it.

Cambridge uses five data types. A CHAR goes in single quotes. A STRING goes in double quotes.

The explanation · step 3 of 23 · part 2 of 4

The five data types (continued)

Data typeWhat it holdsExamplesPython
INTEGERA whole number: positive, negative or zero25, -3, 0int
REALA number with a decimal part3.142, -0.5, 19.99float
CHARExactly one character'A', '7', '?'str of length 1 (no separate char type)
STRINGZero or more characters (text)"Madrid", "28013", ""str
BOOLEANOnly TRUE or FALSETRUE, FALSETrue, False

The explanation · step 3 of 23 · part 3 of 4

The five data types (continued)

DECLARE Age : INTEGER
DECLARE Height : REAL
DECLARE Grade : CHAR
DECLARE FirstName : STRING
DECLARE IsMember : BOOLEAN
Age ← 15
Height ← 1.68
Grade ← 'A'
FirstName ← "Lucia"
IsMember ← TRUE

The explanation · step 3 of 23 · part 4 of 4

The five data types (continued)

age = 15
height = 1.68
grade = "A"
first_name = "Lucia"
is_member = True
print(type(age), type(height), type(grade), type(first_name), type(is_member))
?Why?

The type decides which operations make sense. You can add two INTEGERs. You cannot multiply two names.

The explanation · step 15 of 23 · part 1 of 2

Choosing the right loop

First ask: do I know how many times? If yes, use FOR.

If not, ask: must it run at least once? If yes, use REPEAT UNTIL. If it might need to run zero times, use WHILE.

LoopCondition checkedRuns at least once?Use it whenExample
FOR (count-controlled)No condition: it countsRuns a fixed number of timesYou know how many timesEnter 30 marks
WHILE (pre-condition)Before each passNo, it can run 0 timesUnknown number of times, maybe noneAdd sales until the target is reached
REPEAT UNTIL (post-condition)After each passYes, alwaysUnknown number, but at least oneAsk for a PIN until it is correct

The explanation · step 15 of 23 · part 2 of 2

Choosing the right loop (continued)

!Watch out

If a question says 'use a pre-condition loop', you must use WHILE. Using a different loop loses marks, even if the program works.

The explanation · 19 more steps

More on the lesson page

The lesson page has the rest of the explanation, step by step:

  • Choosing the right data type
  • Input and output
  • Sequence: one step after another
  • Selection with IF
  • Nested IF: up to three levels
  • Selection with CASE
  • Relational and logical operators
  • Arithmetic operators, DIV and MOD
  • Iteration: the count-controlled FOR loop
  • The pre-condition WHILE loop
  • The post-condition REPEAT UNTIL loop
  • Totalling and counting
  • String handling: LENGTH, UCASE, LCASE
  • SUBSTRING and the first position: 1 or 0
  • Procedures and parameters
  • Functions: subroutines that return a value
  • Local and global variables
  • Library routines: MOD, DIV, ROUND, RANDOM
  • Writing maintainable programs

Open the lesson page

Year 10 · Computer Science · Programming · Lesson 1 of 5

Quick quiz · 1 of 3

FOR i ← 1 TO 10 … NEXT i runs the loop…

  1. A 11 times
  2. B 10 times
  3. C 9 times

B. 10 times

1 to 10 inclusive. Python's range(1, 11) does the same.

Year 10 · Computer Science · Programming · Lesson 1 of 5

Quick quiz · 2 of 3

A REPEAT … UNTIL loop…

  1. A Runs for ever
  2. B May run zero times
  3. C Always runs at least once

C. Always runs at least once

The test is at the end, so the body runs first. WHILE tests first and may run zero times.

Year 10 · Computer Science · Programming · Lesson 1 of 5

Quick quiz · 3 of 3

17 MOD 5 is…

  1. A 3
  2. B 2
  3. C 3.4

B. 2

MOD is the remainder: 17 = 3 × 5 + 2. DIV would give 3.

Year 10 · Computer Science · Programming · Lesson 1 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. A function differs from a procedure because it…

    A Has a nameB Can have parametersC Returns a value

    C. Returns a value Both have names and parameters. Only a function gives a value back.

  2. A local variable…

    A Exists only inside its subroutineB Can be used anywhereC Never changes

    A. Exists only inside its subroutine It is created when the subroutine starts and gone when it ends.