Review 2/8
IDLE , Variables , String ←→ Integer , Storing Inputs
Published in
2 min readFeb 8, 2018
→ Lab Assignment: “How old will I be?” on repl.it
[[ REVIEW 0 ]]
IDLE: shell vs. editor
[[ REVIEW 1 ]]
Variables
- Variables are used to label and store data. This lets us use the same data in many places, and change the value over time.
- Variables can be named whatever you want, as long as it does NOT: have any spaces, start with a number, and/or contain any symbols except
_
(underscore) - To create a variable, we write the variable’s name followed by a single equal sign
=
. Everything to the right of the equal sign=
is assigned to the variable and gets stored.
[[ REVIEW 2 ]]
Converting Integer → String
When we do this:
>>> a = 124
>>> b = 816>>> str(a) + str(b)
This is what Python is actually doing:
[[ REVIEW 3 ]]
Storing inputs as variables
The input()
function lets you ask the user for some data. We can store a user’s input as a variable, and do stuff with it later.
Why did the last print statement fail?
Inputs are always strings!!! Therefore, age
is a string.
Since age
is a string, it can’t be added to 10
(an integer).
We must convert it to an integer. And there are actually two places this could happen:
You could convert it at…
- the final print statement (Line 7), or
- the actual variable assignment (Line 1)