Students learn how to use the input() command to assign strings to variables.
This topic adds the input command to the prior learning about print and variable assignment.
Students will learn to interpret, trace, identify and create python code that uses:
Concept | Explanation | Python Code | Examples |
---|---|---|---|
Strings | The text data type. All string data is treated as text, even if it is numerical characters. | “string goes here” | “This is a string” |
Variable assignment | Putting data into a variable. | = | num1 = 10 name = “Andy” |
Concatenation | Joining string literals (text in speech marks that prints exactly as it appears in the code) to the contents of variables in output, or joining the contents of variables together. There are several ways of doing this, but I’ve used the plus symbol in my exercises. | + | print(“Hello ” + name) fullName = firstName + lastName |
Input | Collecting data for processing. In these examples we collect input that the user types on the keyboard but computers collect input in lots of ways – as sound, mouse movements, with sensors etc. ** Input must ALWAYS be assigned to a variable** or it will not be stored. All input is treated as strings. Any input that needs to be treated as a number must be cast into the new format. | variableName = input() | print(“What’s your name?”) name = input() print(“Guess the secret number” guess = int(input()) |
[The slide deck for this topic can be found here] (https://docs.google.com/presentation/d/1iQtjLPF7ZCsytrZSkp3I_v9uE4SvYNA8x5TQLKQETJU/edit?usp=sharing)
Each skill has 3-4 individual projects associated with it that take students through the PRIMM pedagogical framework. They are numbered to try and make the order obvious. You can assign these one at a time or drip feed as students get to them.
https://www.w3schools.com/python/ref_func_print.asp
https://www.w3schools.com/python/python_strings.asp
https://www.w3schools.com/python/python_syntax.asp
https://www.w3schools.com/python/python_variables.asp
https://www.w3schools.com/python/gloss_python_assignment_operators.asp
There are lots of similar tasks available online that you can use to supplement the ones in these resources. I can particularly recommend:
https://www.w3schools.com/python/python_exercises.asp
https://www.practicepython.org/
The input/process/output diagram on the lesson slides will be referred back to in future sessions.
It’s important to ensure that students understand the difference between algorithm and program.
Once you have explained this we move on to coding output using the print statement. This is one of the simplest commands in Python. It offers instant feedback (users can see if it works or not straight away). When students have had a chance to predict & test, start to explore how to break the statement. This will give you a chance to highlight the importance of precision and correct syntax in programming. I find it really helps if students appreciate this very early on.
At first, you start with printing set text (called strings). In later tasks you use variables to store text and refer back to it. Variables are used in a very similar way to letters in algebra – they represent the text. Students can call variables anything they like, but there are some good practice principles:
Make sure to look out for the following things:
print("What's your name?")
name = input("Andy")