Making Decisions

Values, Variables, and Types


Learning Objectives

  • You understand a variable as a name referring to a value in a running program.
  • You can assign and reassign variables and trace how the program state changes.
  • You can distinguish common Python value types: str, int, float, and bool.
  • You can convert numeric input text to an int or float when a calculation needs a number.
  • You can use type(...) as an investigative tool.
  • You can choose variable names that communicate intent.

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.

Variables and Readable Names

Consider:

name = "Ada"
age = 20

After 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 = 45

with:

study_minutes = 45

Python 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.

Improve the variable names

0 / 10 points

Rename the three variables without changing what the program does:

  • rename a to course_name;
  • rename b to student_name;
  • rename 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.

Variables and natural-language programming

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.

Assignment Changes State

An assignment statement gives a variable a value:

count = 3

You can assign the same variable again:

count = 3
count = 4

After 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 = 3
count = count + 1

Python 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

1 / 10
Formatting...

State

main

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.

Values Have Types

Python distinguishes different kinds of values. Some common types are:

TypeMeaningExamples
strtext"Ada", "42", ""
intwhole number42, 0, -7
floatnumber with a decimal representation3.5, 0.0, -1.25
booltruth valueTrue, 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.

Investigating with 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.

Explain identical-looking output

0 / 10 points

Compare these programs:

text_value = "42"
print(text_value)
number_value = 42
print(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.

Converting Between Types

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:

No files opened Select a file to edit

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.

Conversion Can Fail

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.

Name and age

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.

Check Your Understanding

  1. What does assignment do?
  2. Why is count = count + 1 valid Python even though it is not a mathematical equation?
  3. What type does input(...) return?
  4. What is the difference between "42" and 42?
  5. What does int(...) do?
  6. What are the two possible boolean values?
  7. What makes a variable name useful to a human reader?

AI Study Aid

Create a chapter diagram

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.

Diagrams tried: 0