GoalUse input to store what the user types, and sort data into the types string, integer and real.
New words
input · a command that shows a question, waits for the user to type, and gives back what was typedstring · text: letters, digits and symbols, written in quotation marksinteger · a whole number, such as 24 or −10real (float) · a number with a decimal point, such as 2.75; Python calls it float
Explanation
The input command lets the user give the program data while it runs. The words in the brackets are the question, and the answer is stored in a variable. Data comes in types: a string is text, an integer is a whole number and a real number has a decimal point. Python calls the real type float. A phone number is a string, because you never add phone numbers together. Remember: input always gives back a string, even when the user types digits.
Python
1name = input("What is your name? ")
2print("Hello", name)
Output
Hello Aisha
Worked examples
This Scratch program and the Python program above do the same job. The answer block in Scratch matches the variable in Python. Aisha types her name when the question appears.
Python
1pet = input("Name your pet: ")
2animal = input("What animal is it? ")
3print(pet, "is a", animal)
Output
Biscuit is a hamster
A program can ask several questions. Each answer needs its own variable. The user types Biscuit and then hamster.
Value
Data type
Why
"Nairobi"
string
text in quotation marks
24
integer
a whole number
2.75
real (float)
it has a decimal point
"24"
string
quotation marks make it text
−8
integer
a whole number can be negative
Sort the data by type. A value in quotation marks is always a string, even when it looks like a number.
Class activity
Question cards (no computer): write three questions on cards, for example ‘Favourite fruit?’, ‘How many brothers and sisters?’ and ‘How tall are you in metres?’. Put three paper cups next to the cards and label them fruit, family and height. A partner is the program: they read a card, you answer, and they write your answer on a slip and drop it in the right cup. Then sort the slips into three piles: string, integer, real. Which pile did each slip go in? If you have a computer or tablet, type the program for one question into IDLE with an adult.
Practice
1
Give the data type of each value in the table, and a reason.
Value
Data type
"Lima"
?
14
?
3.75
?
"42"
?
−8
?
"Lima" is a string (text). 14 is an integer (a whole number). 3.75 is a real number (it has a decimal point). "42" is a string because it is in quotation marks. −8 is an integer.
2
The user types Lima and then Friday. What does the program show?