Lesson 2 of 30
In progress
Variables and the four basic types
Store values in variables, name them well, and tell the difference between text, whole numbers, decimals and true or false values.
A program that can only print fixed text is a poster, not a program. The moment you store a value and use it later, the program can respond to real data. Variables are how Python remembers.
What you will learn
- Create variables and change their values.
- Tell
str,int,floatandboolapart withtype(). - Name variables so your future self can read the code.
A variable is a label, not a box
Picture a school bag with a name tag. The tag is not the bag and it is not the contents. It is a label that tells you where to look. In Python, score = 10 creates the label score and points it at the value 10.
That picture explains something surprising: a variable can point at a different value later, and two labels can point at the same value. You do not need to announce a type in advance. Python works it out from the value.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
print(name)
print(score)
print(average)
print(passed)
Press Run to see the output.
The four values above show the types you will use constantly.
| Type | Meaning | Example |
|---|---|---|
str |
text | "Aisha", "42" |
int |
whole number | 10, -3, 0 |
float |
decimal number | 87.5, 0.1 |
bool |
true or false | True, False |
Notice that "42" is text, not a number. Quotes change the type, and the type changes what operations make sense.
Check a type instead of guessing
type() tells you what Python thinks a value is. This is the fastest way to settle an argument with yourself about whether something is text or a number.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
The output names the class: <class 'str'>, <class 'int'>, <class 'float'>, <class 'bool'>. In Python, True and False start with capital letters. true in lowercase is a NameError.
Change a value without changing the name
Reassignment is normal and expected. The old value is simply no longer referenced by that name.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
points = points + 5
print(points)
points += 5
print(points)
Press Run to see the output.
points += 5 is shorthand for points = points + 5. Both lines do the same thing. The += form is common when counting.
Names that help you later
A variable name may contain letters, digits and underscores, and cannot start with a digit. total_score is valid. total-score is not, because Python reads the hyphen as subtraction. 2nd_try is not valid either.
Use snake_case for ordinary variables: lowercase words separated by underscores. Use short names in tiny examples and descriptive names in real code. s tells a reader nothing; student_count does.
Avoid names that shadow built-ins such as list, str or sum. Python allows it, then behaves strangely when you later try to call the built-in.
Constants are written in capitals by convention: MAX_ATTEMPTS = 5. Python does not enforce constancy, but the capitals warn a reader not to reassign it. When a value appears twice in a program, give it a name once and use the name; the second copy is where the bug hides.
Beginners often write age = input("Age: ") and then age + 1. input() returns text, so this raises TypeError: can only concatenate str (not "int") to str. Convert at the edge: age = int(input("Age: ")), and handle the case where the learner types something that is not a number, which you will learn in the module on errors.
Create three variables: a city name, a temperature as a decimal, and a boolean for whether it is raining. Print each value and its type on the same line using two print calls or a comma.
Show solution
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
print(city, type(city))
print(temperature, type(temperature))
print(raining, type(raining))
Press Run to see the output.
The exact values do not matter. What matters is that the city is a str, the temperature is a float even though it is warm, and the boolean is capitalised.
Recap
- A variable is a name pointing at a value; reassignment is normal.
str,int,floatandboolcover most beginner work, andtype()settles doubts.snake_casenames and conversion at the point of input prevent a large share of early bugs.
Next, you will combine text and variables with f-strings, and read input from the person using your program.
Does Python care about uppercase and lowercase variable names?
Can a variable hold more than one value?
What does a leading underscore in a name mean?
Progress is saved in this browser only. No account, no email.