main
No variables
str, int, float, and bool.int or float when a calculation needs a number.type(...) as an investigative tool.In the previous chapter, you used variable names to carry input from one line to another. This chapter builds on that first use: it examines how assignment changes program state and why Python distinguishes different kinds of values.
Consider:
name = "Ada"age = 20After these lines run, the program has two variables. name refers to the value "Ada", and age refers to the value 20.
A variable is useful because a program can refer to the value by name later:
print(name)print(age)The variable name should usually say something about what the value means. Compare:
x = 45with:
study_minutes = 45Python does not care which name you choose. A human reader does.
Python variable names normally use lowercase words separated by underscores. Names can contain letters, digits, and underscores, but cannot begin with a digit. Names are case-sensitive, and reserved words used by Python itself cannot be used as names.
A useful name communicates the value’s role. study_minutes says more than x. Readable names reduce the amount of information a reader has to keep in their head.
0 / 10 points
Rename the three variables without changing what the program does:
a to course_name;b to student_name;c to room.Update every use of the variables. Keep the existing prompts and final output behavior unchanged.
A small program often stores more than one named value. The next exercise asks you to assign a name, a subject, and a number of study minutes, then print each stored value.
0 / 5 points
Drag and drop the natural-language programming steps to the area marked with “Program”. Arrange the steps so the program stores the values “Ada”, “Python”, and 45, then prints each of them.
The three print steps may appear in any order, but each value must be assigned before it is printed.
An assignment statement gives a variable a value:
count = 3You can assign the same variable again:
count = 3count = 4After both lines run, count has the value 4. The previous value is no longer the current value of that variable.
A slightly more interesting example is:
count = 3count = count + 1Python evaluates the right-hand side first. At that moment, count is 3, so count + 1 becomes 4. Then 4 is assigned back to count.
The line is not an equation in the mathematical sense. It is an instruction that changes program state. Following those changes line by line is called tracing program state. The code stepper below demonstrates the process.
Code
Step 1 of 10
State
No variables
Output
No output yet
Now trace the changes yourself. For each assignment, first evaluate the right-hand side using the current values, then update the variable on the left before continuing to the next line.
Python distinguishes different kinds of values. Some common types are:
| Type | Meaning | Examples |
|---|---|---|
str | text | "Ada", "42", "" |
int | whole number | 42, 0, -7 |
float | number with a decimal representation | 3.5, 0.0, -1.25 |
bool | truth value | True, False |
These types support different operations.
For example:
print(10 + 5)prints 15, because both values are numbers.
But:
print("10" + 5)raises a TypeError, because Python does not automatically treat the text "10" as a number. The text must first be converted when numeric addition is intended.
The characters look similar. The values are not the same kind of thing.
type(...)Python can tell you the type of a value:
print(type("Ada"))print(type(42))print(type(3.5))print(type(True))The output resembles:
<class 'str'><class 'int'><class 'float'><class 'bool'>You do not need to worry yet about why Python displays the word class. The useful part is str, int, float, or bool.
type(...) is especially useful while investigating unfamiliar behavior.
For example:
minutes = input("Minutes: ")print(type(minutes))Even if the user types 45, the type is still str. input(...) returns text.
0 / 10 points
Compare these programs:
text_value = "42"print(text_value)number_value = 42print(number_value)In two or three sentences, explain why both programs display 42 even though the variables have different types. Mention the types and what print(...) does.
Sometimes you need to convert a value from one type to another.
The most common conversions at this stage are:
int("42")float("3.5")str(42)This allows a program to turn numeric input text into actual numbers:
age_text = input("Age: ")age = int(age_text)print(age + 1)A shorter version combines the input and conversion:
Both are valid. The two-line version can be easier to debug because it keeps the raw text and converted number separate. The one-line version is more compact.
What happens here if the user types hello?
age = int(input("Age: "))Python cannot convert the text "hello" into an integer. It raises a ValueError.
For now, this is acceptable. In Part 3, you will learn systematic input validation and exception handling. In this part, the instructions for exercises that expect a number can assume that the user enters a sensible numeric value.
Still, it is useful to know what the error means. The program did not fail because int is unreliable. It failed because the provided value did not satisfy the conversion’s requirements.
Unless an exercise specifically asks you to handle invalid input, you may assume users type values in the requested form during Parts 1 and 2.
0 / 10 points
Write a program that first asks for the user’s name and then asks for their age.
Store the name in a variable called name. Convert the entered age to an integer and store it in a variable called age. You may assume that the entered age is a valid whole number.
Finally, print a greeting in exactly this format:
Hello {name}! You are {age} years old.For example, if the name is Ada and the age is 37, the greeting is:
Hello Ada! You are 37 years old.You have now seen bool alongside the other basic value types, but you do not need to operate on boolean values yet. Chapter 5 develops arithmetic with int and float; Chapter 6 introduces comparisons, shows how they produce boolean values, and uses those values to make decisions.
count = count + 1 valid Python even though it is not a mathematical equation?input(...) return?"42" and 42?int(...) do?AI Study Aid
We're looking into to what extent AI-generated diagrams could help with studying.
Use this study aid to generate an AI-generated visual summary of the material you just studied.
Each diagram style emphasizes the content in a different way, so you can choose the focus
that feels most useful.
Using the diagram generator is voluntary and does not affect your course progress. We encourage
you to try it out and see if it helps, but it's totally up to you! Your answers help us understand
how to make better study aids in the future.