Skip to main content
Learning Center
Python Programming

Module 1: Values, Variables, and Expressions

Values and Types: Text, Numbers, True/False, and Nothing

The five kinds of value you meet in the first hour of Python, how to ask what you are holding, and why published data usually arrives as text.

Lesson 4 of 46 in the recommended order · About 25 min (estimate)

On this page

Outcome

By the end of this lesson you can look at any simple Python value and say what type it is, what you are allowed to do with it, and how to convert it to another type on purpose rather than by accident.

Why it matters

Almost every early Python bug is a type confusion wearing a disguise. A published amount looks like a number on screen but is text in memory; a total comes out as 185000.0 when a whole number was wanted; a check for "did we get a value" quietly succeeds on an empty string.

Data that arrives from outside your program, from a file, a form, or a public API, is almost always text. Deciding when to convert it, and being able to see when you have not, is a skill you will use in every remaining module.

Concept

Every value in Python has a type, and the type decides what the value can do. Five of them cover nearly everything you meet early on.

  • str is text: "SPE-2026-0412", "Closed", even "185000". Written in quotes. Can be joined, sliced, and searched, but not added to a number.
  • int is a whole number: 185000, 0, -4. No quotes, no decimal point. Exact.
  • float is a number with a fractional part: 0.35, 185000.0. Fast, but only approximate; 0.1 + 0.2 is famously not exactly 0.3.
  • bool is one of exactly two values, True or False. It is what a comparison produces.
  • None is the single value meaning "nothing here". It is not zero, and it is not an empty string. It is how Python says a field was never filled in.

The built-in type(value) tells you which one you are holding. The built-ins int(...), float(...), str(...), and bool(...) convert between them, and they are deliberate: Python will not quietly turn "185000" into a number for you, because guessing is how data gets silently corrupted.

Conversion can fail, and failing is the useful behaviour. int("185000") gives 185000. int("not a number") raises a ValueError immediately, at the line where the bad data appeared, instead of producing something wrong three functions later.

Read the code

notice_id = "GSA-2026-0731"
raw_amount = "96500"
share_reserved = 0.25
is_active = True
award_date = None

print(type(notice_id))
print(type(raw_amount))
print(type(share_reserved))
print(type(is_active))
print(type(award_date))

Five names, five different types. The pair worth staring at is notice_id and raw_amount: both are str, even though one of them is obviously an identifier and the other obviously an amount. Python does not care what a value means, only what it is. share_reserved is a float because it was written with a decimal point. award_date is None because this opportunity has not been awarded, which is a genuine fact about it, not a missing line of code.

Predict the output

Predict the five lines this prints. You are predicting the type names, not the values.

Check your prediction
<class 'str'>
<class 'str'>
<class 'float'>
<class 'bool'>
<class 'NoneType'>

The second line is the one that catches people. "96500" is quoted, so it is text, no matter how numeric it looks. None's type is spelled NoneType, which you rarely need to write but will see in error messages.

Modify the code

Change one line: replace share_reserved = 0.25 with share_reserved = 25. Predict which of the five printed lines changes, and to what.

What changes, and why

Only the third line changes, from <class 'float'> to <class 'int'>. Removing the decimal point changes the type, and the type changes what happens later: 25 and 0.25 behave very differently the first time you multiply an amount by them. This is why a percentage stored as 25 instead of 0.25 produces answers that are wrong by a factor of a hundred and never raise an error.

Debug the bug

An assistant was asked for a program that adds a handling fee to a published amount. It produced this and said the result would be 97000.

raw_amount = "96500"
handling_fee = 500

total = raw_amount + handling_fee
print(f"Total: {total}")
What's actually wrong

This is a runtime error, not a wrong answer. The file parses, the first two lines run, and the third raises:

TypeError: can only concatenate str (not "int") to str

+ means "join" for text and "add" for numbers, and Python refuses to guess which one you meant when the two sides disagree. The fix is to convert on purpose: total = int(raw_amount) + handling_fee.

Notice what the error message tells you: it names both types and the operation. Read those two facts before changing anything; they usually identify the exact line and the exact misunderstanding.

Try it yourself

The starter code holds an amount exactly as a public listing would publish it, as text, plus a numeric threshold a reviewer cares about. Print the amount as a number, then print whether it is greater than the threshold.

Loading this exercise…

Practical challenge (optional)

Optional: extend your program so it also prints the amount as a float with two decimal places, and prints the type of each of the three values you now hold. Then answer, in one sentence, why an amount of money is usually safer stored as an integer number of cents than as a float. This is a real design question that Module 6 returns to when the data starts arriving from files.

Sign in to track your progress on this exercise.

AI collaboration

Checkpoint

For each expression, say what type the result is, or say that it raises an error and which one:

  1. "96500" + "500"
  2. int("96500") + 500
  3. 96500 > 500
  4. float("96500")
  5. int("96,500")
Answers
  1. str. Both sides are text, so + joins them: "96500500". It runs, and the answer is nonsense, which is the "wrong output" case from Module 0.
  2. int, value 97000.
  3. bool, value True.
  4. float, value 96500.0.
  5. ValueError. The comma is not a digit, and int refuses to guess that it means thousands. Stripping separators before converting is a real step you will write in Module 6.

Sign in to track your progress on this exercise.

Summary and next step

You can now name a value's type, convert deliberately between text and numbers, and read the two error messages that show up when you forget. None is a real value meaning "no value", and a quoted number is text. Next: how names get bound, rebound, and combined, and what an expression actually evaluates to.

learning.goultergroup.com

The interactive parts of this page have not loaded. Reading and links still work; reload the page to try again.