Study Guide Instructor view

Writing Your First Python Programs

Teaching notes for Lesson 5.18, with controls for presenting it.

Keep this page on your own screen. Share only the lesson window. The lesson window holds no teaching notes, so sharing it cannot show them.

Presenting

  1. Open the lesson in its own window and share that window, not this one.
  2. Use Next and Previous here, or the lesson’s own, to move through the lesson. Next reveals one thing at a time: the next idea, or the answer to the question on screen, then the next step. Previous takes the last reveal back. The line below says what is on screen and what Next will show.
  3. Reveal hints and answers when the class is ready, and reset a demonstration before using it again.

The controls need JavaScript and a browser that supports BroadcastChannel. Without them, open the lesson in presentation mode and use its own controls.

The two windows talk to each other only inside this browser, on this device. Nothing is sent to a server, and no account is needed. A second device (a phone or another tablet) cannot follow the lesson window yet.

Lesson plan

Level
Beginner
Time
About 75 minutes; the steps below add up to 93

Learners move from a block program, described in words, to the same ideas in Python: print, input, variables, int(), arithmetic, if, elif and else, and error messages. Every question can be answered by predicting on paper; running the code is optional and used to check predictions.

Objectives

  • Match a block-based program to the same program written in Python
  • Predict what print statements, arithmetic and an if, elif, else statement will show
  • Explain why input() gives text, and use int() to convert it to a whole number
  • Use indentation and colons correctly in a Python program
  • Read a Python error message and use it to find and fix a mistake
  • Write a small Python program that asks for a number and makes a decision

Before the lesson

  • If possible, a computer or tablet with a free Python environment for checking predictions; the lesson works without one.
  • Mini whiteboards for predicting output.
  • Optional: the three-block Scratch program on the board.

Steps

  1. Introduction Opening

    On screen: the lesson’s title, module and number; Next adds its objectives and key terms, as the lesson page lists them; Next again begins the first step.

    Objectives on screen

    • Match a block-based program to the same program written in Python
    • Predict what print statements, arithmetic and an if, elif, else statement will show
    • Explain why input() gives text, and use int() to convert it to a whole number
    • Use indentation and colons correctly in a Python program
    • Read a Python error message and use it to find and fix a mistake
    • Write a small Python program that asks for a number and makes a decision

    Purpose: Learners move from a block program, described in words, to the same ideas in Python: print, input, variables, int(), arithmetic, if, elif and else, and error messages. Every question can be answered by predicting on paper; running the code is optional and used to check predictions.

  2. 1. A program made of blocks Think 6 min

    Purpose: Start from a block program learners may know and make them state what it does and what it keeps.

    Ask

    • What does the sprite say if the player types Mina?
    • What must you add to ask for the age as well?

    Responses to expect

    • Hello, Mina
    • Another ask block, and keep the name in a variable first

    Misconceptions to challenge

    • A learner may believe: “Answer keeps every typed value.” Respond: Answer is replaced each time somebody types.

    Checking understanding: Pupils show on a whiteboard the exact words the sprite says, including the comma and space.

    Depending on the answers

    • If pupils have not used a block language: treat the three blocks as three instructions on a card and run them aloud

    English as an additional language

    • Check the words ask, wait, join and sprite.

    In the lesson window

    • Think it through: A player types Mina. What does the sprite say? Then say how you would change the program so that it also asks for the player’s age and says it back.
  3. 2. The same program in words and symbols Discover 8 min

    Purpose: Map each block idea to its Python line and state how the lesson can be used with or without a computer.

    Ask

    • Which line matches the ask block?
    • What is name for?

    Responses to expect

    • The input line
    • A place chosen by the programmer to keep the typed text

    Misconceptions to challenge

    • A learner may believe: “print waits for typing.” Respond: print only shows; input shows and waits.
    • A learner may believe: “The built-in answer is the same as a Python variable.” Respond: In Python the programmer names the variable.

    On the board or with the pen

    • Draw the block and the Python line side by side with a line joining ask to input.

    Checking understanding: The blocks-mapping check: the print options show who confuses showing with asking.

    In the lesson window

    • Think it through: Look at the two Python lines beside the three blocks. Which line do you think matches the ask block, and which matches the say block? What do you think the name on the left of the = sign is for?
    • Check your understanding: Which line of Python does the same job as the ask “What is your name?” and wait block, including keeping what was typed?
  4. 3. print, and what quotation marks do Predict 7 min

    Purpose: Pupils predict three print lines before the meaning of quotation marks is stated.

    Ask

    • What does each of the three lines show?
    • What do the quotation marks change?

    Responses to expect

    • Hello, 7, 3 + 4
    • They make it text that is shown as it is

    Misconceptions to challenge

    • A learner may believe: “print(3 + 4) shows 3 + 4.” Respond: Without quotation marks Python works the calculation out.

    Checking understanding: Whiteboards: pupils write the three outputs, then the class compares.

    Depending on the answers

    • If many write 3 + 4 twice: ask which line has quotation marks and what the program does with 3 + 4 without them

    In the lesson window

    • Think it through: Without running it, predict what the program shows on the screen, line by line. Are all three lines different?
    • Check your understanding: Which line makes Python show exactly the characters 2 + 5 on the screen?
  5. 4. Variables: storing a value Explain 7 min

    Purpose: Variables are made by storing, and = means store, not equal.

    Ask

    • What does the second line of the program do?
    • How is this different from algebra?

    Responses to expect

    • Works out score + 5 from the old value, then stores it
    • Here = stores; it is not a claim that both sides are equal

    Misconceptions to challenge

    • A learner may believe: “score = score + 5 is impossible.” Respond: Read it as: work out the right side first, then store.
    • A learner may believe: “A variable keeps its first value.” Respond: It changes whenever something new is stored.

    Checking understanding: The reassign check, with an option for each misconception.

    Depending on the answers

    • If pupils choose the error option: compare with score ← score + 5 in pseudocode from Lesson 3

    In the lesson window

    • Check your understanding: What does this program show?
  6. 5. What input() gives back Predict 10 min

    Purpose: A predicted crash introduces the idea that input gives text, and int() as the fix.

    Ask

    • What happens when the visitor types 12?
    • What does “5” + “3” show?

    Responses to expect

    • An error: a string cannot be added to a whole number
    • 53, and 5 + 3 is 8

    Misconceptions to challenge

    • A learner may believe: “input gives a number if the user types digits.” Respond: input always gives text. int() converts it.
    • A learner may believe: “The error is in the + sign.” Respond: The + is fine; its two values are of different kinds.

    Checking understanding: The fix-conversion check: the order of input and int is the usual slip.

    Depending on the answers

    • If pupils write input(int(…)): ask which happens first: asking or converting the answer
    • If pupils are secure: ask for the output of print("5" * 3)

    To go further

    • What does print("5" * 3) show, and why?

    In the lesson window

    • Think it through: A visitor types 12. Predict what the program shows. Does it show 13, or something else, and why might that be?
    • Think it through: What does print(“5” + “3”) show, and what does print(5 + 3) show? Why are they different?
    • Check your understanding: The program above crashed. Which version of its first line fixes it, so that age + 1 works?
  7. 6. Arithmetic Predict 8 min

    Purpose: Link Python’s symbols to DIV and MOD, and note that / gives a decimal.

    Ask

    • Does 7 / 2 give 3?
    • Which symbols match DIV and MOD?

    Responses to expect

    • 3.5
    • // and %

    Misconceptions to challenge

    • A learner may believe: “20 / 4 shows 5.” Respond: It shows 5.0, because / always gives a decimal.
    • A learner may believe: “Subtraction comes before multiplication.” Respond: Multiplication is done first, as in maths.

    Checking understanding: The expressions table: any wrong cell points to a particular operator.

    Depending on the answers

    • If pupils get // and % the wrong way round: recall DIV(7, 2) and MOD(7, 2) and which of the two keeps the remainder

    In the lesson window

    • Think it through: You know from pseudocode that DIV(7, 2) is 3 and MOD(7, 2) is 1. What do you think Python shows for print(7 / 2): 3, 3.5 or something else?
    • Check your understanding: What does print(20 / 4) show?
  8. 7. Choosing: if, elif and else Predict 10 min

    Purpose: Predict an if, elif, else program and learn how Python marks structure with colons and indents.

    Ask

    • What does the program show for 7?
    • Which lines were not carried out, and why?

    Responses to expect

    • Silver
    • The print under if, and the else, because a branch runs only when the ones above are false

    Misconceptions to challenge

    • A learner may believe: “All true branches run.” Respond: Only the first true branch runs.
    • A learner may believe: “= can compare two values.” Respond: Use == to compare; = stores.

    Checking understanding: The badge table, especially the boundary scores 9 and 6.

    Depending on the answers

    • If pupils are wrong at 9 or 6: ask them to say the condition aloud and whether 9 >= 9 is true
    • If pupils confuse = and ==: return to the DoNotConfuse panel and give three conditions to classify

    Support

    • Underline the colon and the indent on a printed copy of the program.

    In the lesson window

    • Think it through: A player scores 7. Predict what the program shows. Then say which lines were not carried out at all, and why.
  9. 8. Reading an error message Discover 10 min

    Purpose: Treat an error message as information: the kind of error, the description and the line.

    Ask

    • Which message goes with which cause?
    • Which appears first if a program has two mistakes?

    Responses to expect

    • Match by the first word and its description
    • The syntax error, before the program runs

    Misconceptions to challenge

    • A learner may believe: “An error message means the pupil has failed.” Respond: It is information that points to a line.
    • A learner may believe: “Python reports every mistake together.” Respond: It reports the first problem it meets; fix it and run again.

    Checking understanding: The two checks: the NameError cause and the order of errors.

    Depending on the answers

    • If pupils guess at causes: practise reading the last line first, then the line number, with one message at a time

    English as an additional language

    • Teach the words defined, expected, indented, concatenate with a gloss.

    In the lesson window

    • Think it through: Without being told, match each error message to its cause: a missing colon, a variable name that was never given a value, an indent missing under an if, and a number added to a string. What in each message helped you?
    • Check your understanding: Python says: NameError: name 'scor' is not defined. What is the most likely cause?
    • Check your understanding: A program has a missing colon on line 3 and a misspelt variable on line 6. Python is asked to run it. What happens?
  10. 9. Mend a broken program Apply 10 min

    Purpose: Independent mending of a broken program, then a diagnosis of 53.

    Ask

    • Which mistake would Python report first?
    • Why does 5 and 3 give 53?

    Responses to expect

    • The missing colon, then the misspelt name
    • input gives text, so + joins characters

    Misconceptions to challenge

    • A learner may believe: “The misspelt variable would appear first because it is on line 1.” Respond: A syntax error is found before the program runs.
    • A learner may believe: “Quotation marks make something a number.” Respond: They make it text. int() converts text.

    Checking understanding: Pupils write the corrected program on paper before the model answer is shown.

    Depending on the answers

    • If pupils fix only one mistake: ask them to run the corrected program in their head and report any further error

    Stretch

    • Add a message for under-13s using else.

    In the lesson window

    • Think it through: Find the mistakes, say which error message each would cause and in which order Python would show them, and write the corrected program. Check by predicting what it shows for Ali, aged 14.
    • Apply it: What is the best change to make the program show 8?
  11. 10. Write a program of your own Apply 10 min

    Purpose: Write a short program from a description, then diagnose a condition in the wrong order.

    Ask

    • Which ages are Free, Child and Adult?
    • Why does 75 show Pass?

    Responses to expect

    • 4 Free; 5 and 15 Child; 16 Adult
    • The first condition is true for 75, so the elif is never checked

    Misconceptions to challenge

    • A learner may believe: “The elif must say at least 5.” Respond: It is reached only when the first condition was false.
    • A learner may believe: “Python checks every branch.” Respond: It stops at the first true one.

    Checking understanding: Check each pupil’s program against ages 4, 5, 15 and 16.

    Depending on the answers

    • If pupils use <= 5 for Free: test age 5 and see what the program shows against the description

    Support

    • Give the structure with blanks for the conditions.

    In the lesson window

    • Think it through: Write a Python program that asks for the visitor’s age, as a whole number, and shows Free, Child ticket or Adult ticket. Predict what it shows for the ages 4, 5, 15 and 16.
    • Apply it: Why does 75 show Pass, and not Merit?
  12. 11. Show what you know Consolidate 7 min

    Purpose: Explain how Python compares with blocks and translate a block program with three details to get right.

    Ask

    • How is Python like a block program, and how is it different?
    • Which details must be right when translating?

    Responses to expect

    • The same ideas, but spelling, colons and indents must be exact
    • int(), ==, and print

    Misconceptions to challenge

    • A learner may believe: “answer = 7 would be true if the player types 7.” Respond: The typed value is text, so int() is needed before comparing with 7.

    Checking understanding: Collect the “least sure” questions; they decide how Lesson 19 begins.

    Depending on the answers

    • If several pupils choose the == with no int(): revisit the age + 1 crash and ask what “7” == 7 is

    In the lesson window

    • Think it through: Explain to a friend who only knows Scratch how a small program in Python is like a block program and how it is different. Mention input, a variable and what a colon and an indent do.
    • Apply it: Which Python program does the same as the blocks?
    • Think it through: Which part of this lesson are you least sure about? Write one question you would ask your teacher.
  13. Lesson recap Closing

    On screen: the lesson’s objectives again; Next adds the closing question, which asks the class to explain each one in their own words without looking back. It says nothing about what anyone has learnt: use the answers to decide what to revisit.

    Points to listen for

    • print shows; input asks and waits and always gives text; int() turns text into a whole number.
    • = stores a value; == compares two values.
    • if, elif and else use colons and indents; the first true branch runs.
    • An error message names the kind of mistake and the line: read the last line first.

Consolidation

  • print shows; input asks and waits and always gives text; int() turns text into a whole number.
  • = stores a value; == compares two values.
  • if, elif and else use colons and indents; the first true branch runs.
  • An error message names the kind of mistake and the line: read the last line first.