Year 8 · Spring 2

Python I · Lesson 1 of 5

print, input and sequence

PracticalTopic 11 AlgorithmsTopic 13 Programming basics

Year 8 · Python I · Lesson 1 of 5

You will learn to

  • Use print() to show text on the screen.
  • Use input() to ask a question and store the answer in a variable.
  • Explain that Python runs one line at a time, from top to bottom.
  • Predict what a short program will show before you run it.

Year 8 · Python I · Lesson 1 of 5

Success criteria

You have got it when you can say:

  • I can write a program that asks for my name and says hello to me by name.
  • I can predict the output of a program, then run it to check.
  • I can save my programs with the agreed names.

Year 8 · Python I · Lesson 1 of 5

Retrieval starter

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

  1. Last unit

    5 KB into bytes (kilo = 1000). You should…

    A Divide by 1000B Multiply by 8C Multiply by 1000

    C. Multiply by 1000

  2. Last unit

    Hex F is…

    A 15B 16C 10

    A. 15

  3. Earlier this year

    A box has width 100px, padding 10px, border 2px, margin 5px. How wide is it on screen, from border edge to border edge?

    A 124pxB 112pxC 134px

    A. 124px

Year 8 · Python I · Lesson 1 of 5

Key words (1 of 4)

casting
Changing a value from one data type to another, like int("13").
Algorithm
Precise, ordered, finite steps to solve a problem.
Decomposition
Breaking a problem into smaller ones.
Abstraction
Ignoring detail that doesn't matter for the problem.

Year 8 · Python I · Lesson 1 of 5

Key words (2 of 4)

Sequence · selection · iteration
Steps one after another. First this, then this.
Pseudocode
Code-like writing for algorithms, without strict rules.
Trace table
Records variable values step by step to check logic.
Variable
A named place in memory that holds a value.

Year 8 · Python I · Lesson 1 of 5

Key words (3 of 4)

Assignment
Storing a value in a variable. = in Python, ← in pseudocode.
Concatenation
Joining strings together: "7" + "7" = "77".
Selection
Choosing a path with if / else.
Iteration
Repeating steps: a loop.

Year 8 · Python I · Lesson 1 of 5

Key words (4 of 4)

Boolean
A value that is only True or False.
Infinite loop
A loop whose condition never becomes false.

The explanation · step 1 of 2

Inputs, process, outputs

★Remember

Before writing an algorithm ask: what goes in? What must happen? What comes out?

The explanation · step 2 of 2

Input and casting

name = input("What is your name? ")
age = int(input("How old are you? "))
print("Hi " + name + ", next year you'll be " + str(age + 1))
  1. input() always gives back a string, even if you type digits.
  2. int() turns "13" into 13 so you can do maths. This is casting.
  3. str() turns 14 back into text so it can join other text.

Year 8 · Python I · Lesson 1 of 5

Quick quiz · 1 of 3

What does print("3" + "4") show?

  1. A 7
  2. B 34
  3. C An error

B. 34

They're strings, so + joins them: 34.

Year 8 · Python I · Lesson 1 of 5

Quick quiz · 2 of 3

What does 17 % 5 give?

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

A. 2

17 ÷ 5 = 3 remainder 2. % gives the remainder.

Year 8 · Python I · Lesson 1 of 5

Quick quiz · 3 of 3

range(3) gives…

  1. A 0, 1, 2, 3
  2. B 0, 1, 2
  3. C 1, 2, 3

B. 0, 1, 2

Starts at 0, stops before 3.

Year 8 · Python I · Lesson 1 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. x = input("Number: ") and the user types 5. What type is x?

    A intB floatC str

    C. str input() always gives a string: "5". Cast it with int() to do maths.

  2. How many times does for i in range(2, 10, 2): run?

    A 8B 4C 5

    B. 4 2, 4, 6, 8. The next would be 10, which is the stop, so it isn't used. 4 times.