Values, Variables, and Types

core-python
beginner
Understand values, expressions, assignment, names, and type conversion together.
  • Level: Beginner
  • Estimated time: 25–40 minutes
  • You will learn: Understand values, expressions, assignment, names, and type conversion together.
  • Practice in: Google Colab, Jupyter, or VS Code

Questions

  • What problem does Values, Variables, and Types help us solve in a small Python program?
  • What should we predict before running the example?
  • What value, output, or error should we inspect after changing one line?

Objectives

  • Run a complete example for values, variables, expressions, and types in Colab.
  • Explain the example line by line using plain language.
  • Change one part of the code and predict the result before running it.
  • Recognize one common mistake and use the error message as evidence.

Hands-on episode: Values, Variables, and Types

A value is information, a variable is a name that points to a value, an expression produces a value, and a type tells Python what operations make sense.

We will learn this by running code, not by memorizing a definition first. Open the Colab notebook from the button above, find this section, and run each cell in order. Keep a small note beside the notebook with three columns: prediction, actual result, and what changed.

Example 1.1

Before running this, predict the value of total and the type of each named value.

price = 12.50
quantity = 2
total = price * quantity
print(total)
print(type(total))

Run the cell once without editing it. If the result is different from your prediction, leave the prediction visible and write one sentence about the difference. That sentence is more useful than a perfect first guess.

Explain Example 1.1

  • price = 12.50 attaches the name price to a floating-point number.
  • quantity = 2 attaches the name quantity to an integer.
  • price * quantity looks up both names, multiplies their values, and produces a new value.
  • print(type(total)) lets you inspect the kind of value Python produced.

Now explain the example out loud or in a Markdown cell. Use short sentences: “this line creates…”, “this name stores…”, “this output appears because…”. If you cannot explain a line yet, run only the lines above it and inspect the values that exist at that moment.

Challenge 1.1

NoteChallenge

Change price = 12.50 to price = "12.50". Now the value is text, not a number, and multiplication or addition may behave differently. This is why type() is a powerful inspection tool for beginners.

Show a safe way to approach the challenge
  1. Copy Example 1.1 into a new Colab cell.
  2. Change exactly one value, name, condition, or line.
  3. Write the expected output before running the cell.
  4. Run the cell and compare the actual result with your prediction.
  5. If the result surprises you, undo the change and try a smaller one.

Suggested first move: Change price = 12.50 to price = "12.50".

Debugging checkpoint 1.1

WarningDebugging checkpoint

Do not read score = score + 1 as a math equation. Read it as a three-step action: look up the old score, add one, and attach the name score to the new value. If Python says NameError, it means the name had no value yet at the moment Python tried to look it up.

Do not debug by rewriting the whole example. Read the error type or surprising output, inspect the closest value with print(...) or type(...), then change one thing. This is the same routine you will use in larger projects.

Apply it

In Colab, build a tiny receipt with price, quantity, subtotal, tax, and total. Print both the value and the type after each calculation. Then quote one number on purpose and explain the error or changed behavior.

Finish by adding a Markdown cell that answers: What did this example teach me that I can reuse in a project?

Key points

  • Learn the concept by running a complete, small example first.
  • Predict before execution so your thinking becomes visible.
  • Change one thing at a time so cause and effect stay clear.
  • Treat errors as clues about the exact line or value Python could not handle.

The problem this lesson solves

A program is useful because it can remember information and combine it in new ways. For example, a store program may need to remember a price, remember a quantity, multiply them, and print the total.

Before you can write larger programs, you need four tiny but important ideas:

  • a value is a piece of information, like 10 or "Ada";
  • an expression is code Python can evaluate to produce a value, like 2 + 3;
  • a variable is a name that helps you reuse a value later, like price;
  • a type tells Python what kind of value something is, like int or str.

These ideas are small, but almost every Python program uses them.

How names point to values

The diagram separates the line of code, the name, the value, and the type.

flowchart LR
  line["Python line<br/>price = 10"] --> assign["assignment<br/>="]
  assign --> name["name<br/>price"]
  name --> object["value object<br/>10"]
  object --> type["type<br/>int"]
  textline["Python line<br/>price = '10'"] --> textname["name<br/>price"]
  textname --> textobject["value object<br/>'10'"]
  textobject --> texttype["type<br/>str"]

The name price is not the value itself. It is more like a label attached to a value. Python also knows the value’s type.

1. Values and expressions

A value is data Python can work with. These are values:

42
3.14
"hello"
True

An expression is a small piece of code that Python can turn into one value. Try reading the examples below like calculator entries.

2 + 3
10 / 4
"Py" + "thon"

Step-by-step explanation

  • 2 + 3 asks Python to add two whole numbers. The result is 5.
  • 10 / 4 asks Python to divide. The result is 2.5, which is a decimal number.
  • "Py" + "thon" asks Python to join two pieces of text. The result is "Python".

The same symbol can mean different things depending on the values around it. With numbers, + means addition. With strings, + means joining text.

Check your understanding

2. Variables and assignment

A variable lets you give a name to a value so you can use it later.

price = 10
quantity = 3
total = price * quantity
print(total)

What Python does, line by line

  1. price = 10
    • Python creates the value 10.
    • Python attaches the name price to that value.
  2. quantity = 3
    • Python creates the value 3.
    • Python attaches the name quantity to that value.
  3. total = price * quantity
    • Python looks up price and finds 10.
    • Python looks up quantity and finds 3.
    • Python multiplies 10 * 3 and gets 30.
    • Python attaches the name total to 30.
  4. print(total)
    • Python looks up total and finds 30.
    • Python displays 30.

A common beginner misunderstanding

= in Python does not mean “these are already equal”. It means “store this value under this name”.

score = 8
score = score + 1
print(score)

The second line means:

  1. look up the old value of score, which is 8;
  2. add 1, producing 9;
  3. attach the name score to the new value 9.

Check your understanding

3. Types and conversion

A type tells Python what kind of value it is working with. That matters because Python treats different kinds of values differently.

print(type(42))
print(type("42"))

The first value, 42, is an integer. The second value, "42", is a string because it has quotation marks.

If you ask a user for input, Python gives you text:

age_text = input("How old are you? ")
age = int(age_text)
print(age + 1)

What Python does, line by line

  1. input(...) waits for the user to type something.
  2. Whatever the user types is stored as text. If the user types 12, Python stores "12", not the number 12.
  3. int(age_text) tries to convert the text into an integer.
  4. After conversion, age + 1 works as arithmetic.

Common mistake: text that looks like a number

Broken code:

age = "12"
print(age + 1)

Python cannot add text and a number. The fix is to convert the text first:

age = int("12")
print(age + 1)

Check your understanding

Practice

Open https://colab.new and try this small receipt calculator:

notebook_price = 4
pen_price = 2
notebooks = 3
pens = 5

total = notebook_price * notebooks + pen_price * pens
print(total)

Then change exactly one number and predict the new total before running the code.

Key points

TipKey points
  • Values are pieces of information, such as numbers, text, and booleans.
  • Expressions are evaluated by Python to produce values.
  • Assignment attaches a name to a value.
  • Types matter because Python uses them to decide which operations make sense.
  • Convert text to numbers before doing arithmetic with user input.

References

  • Python Tutorial: https://docs.python.org/3/tutorial/introduction.html
  • Built-in types: https://docs.python.org/3/library/stdtypes.html
  • Quarto OJS documentation: https://quarto.org/docs/interactive/ojs/
Back to top