Grade by Grade
A. Programming Concepts and Skills · Programming Basics: Data, Decisions and Loops

Data Types: Integers, Floats, Strings and Booleans

A1.1 A1.3 A4.1About 65 minutes
Learning goals
  • I can identify int, float, str and bool values and check a value's type with type().
  • I can choose a suitable data type for a piece of information.
  • I can convert between types with int(), float(), str() and bool().
  • I can explain and fix errors caused by mixing types.

Words to know

data type
The kind of value something is. It decides what values are allowed and what operations make sense.
integer (int)
A whole number with no decimal point, such as -3, 0 or 2026.
floating-point number (float)
A number with a decimal point, such as 1.72 or 3.0. Stored to about 15 to 17 significant digits.
string (str)
A sequence of characters (text) in quotes, such as "Sudbury" or "16".
Boolean (bool)
A value that is either True or False. Named after the mathematician George Boole.
type conversion (casting)
Making a value of one type from a value of another type, for example int("16") gives the integer 16.
dynamic typing
In Python a variable has no fixed type; the type belongs to the value it currently refers to.

Four kinds of data

At a skating rink, a sign-up form asks for your name, your age, your height in metres and whether you have a membership. Those are four different kinds of information:

  • a name is text,
  • an age is a whole number,
  • a height like 1.72 is a decimal number,
  • a membership is yes or no.

A computer stores and handles each kind differently, so every value in a program has a data type. Python's four basic types are int, float, str and bool.

intwhole numbers16-402026floatdecimal numbers1.72-12.53.0strtext in quotes"Ava""16""K1A 0A9"booltrue or falseTrueFalse
The four basic data types. Notice "16" is a string (text), not a number, and 3.0 is a float even though it is a whole number, because it has a decimal point.

The four types in detail

int (integer). Whole numbers, positive, negative or zero: 16, -40, 0. Python integers can be as large as your computer's memory allows. Do not type commas: 1,000,000 is not one number in Python. You may use underscores to make big numbers readable: 1_000_000.

float (floating-point number). Any number written with a decimal point: 1.72, -12.5, 3.0. Very large or small values can use e for "times ten to the power": 2.5e3 means 2.5 × 10³ = 2500.0. Floats are stored to about 15 to 17 significant digits, so they are sometimes very slightly off (more in Lessons 4 and 5).

str (string). Text in single or double quotes: "Sudbury", 'K1A 0A9'. A string can hold digits, but "16" is still text; you cannot do arithmetic with it. "" is the empty string (no characters).

bool (Boolean). Only two values: True and False, with capital letters and no quotes. Booleans are the answers to yes/no questions such as "is the temperature below zero?" They control decisions and loops (Lessons 8 to 13).

print(type(16))
print(type(1.72))
print(type("16"))
print(type(True))
print(type(3.0))
print(2.5e3)

type() reports the type of any value. Use it in the shell whenever you are unsure. Output, line by line: <class 'int'> | <class 'float'> | <class 'str'> | <class 'bool'> | <class 'float'> | 2500.0

Python decides the type for you

In languages such as Java or C++, you declare a variable's type before you use it, for example int age = 16;. Python uses dynamic typing: you just assign, and the type comes from the value. The same name can even refer to a different type later:

  • x = 5 makes x an int.
  • x = "five" now makes x a str.

This is legal, but it is poor style. A variable should hold one kind of thing for its whole life, so readers are not surprised.

Converting between types

Often data arrives as one type and you need another. Text typed by a user is always a string (Lesson 7), so you will convert all the time.

  • int(x) makes an integer. int("42") is 42. int(7.9) is 7: it cuts off the decimal part (it does not round). int(-7.9) is -7.
  • float(x) makes a float. float("2.5") is 2.5; float(3) is 3.0.
  • str(x) makes a string. str(42) is "42".
  • bool(x) makes a Boolean. Zero, 0.0 and the empty string "" give False. Any other number or string gives True, even "False" and "0".

Conversions can fail. int("3.5") and int("sixteen") both stop the program with a ValueError, because those strings are not written like whole numbers. (int(float("3.5")) works: first 3.5, then 3.)

str"42"int42float42.0int("42")str(42)float("42")str(42.0)float(42)int(42.0) (int cuts off any decimals)
Converting between types with int(), float() and str(). Each function makes a new value of the new type; the original is unchanged.
Worked example

Example 1: Choose the type

Choose the best data type for each: (a) the number of students in a class, (b) a postal code, (c) the price of a toque, (d) whether a student has handed in a form, (e) a phone number.

  1. (a) Students are counted in whole numbers: int.
  2. (b) A postal code like P3E 2C6 has letters and a space: str.
  3. (c) A price like 24.99 has cents: float. (Banks often store prices as whole cents in an int to avoid rounding errors, which is also a good answer if you explain it.)
  4. (d) Yes or no: bool.
  5. (e) A phone number is made of digits, but you never add or multiply phone numbers, and it may contain dashes or start with a 0 or a +: str.

int, str, float, bool, str.

Worked example

Example 2: Fixing a type error

This program crashes. Explain the error and fix it.

  • age = 16
  • print("Next year you will be " + age + 1)
  1. Read the error. Python says: TypeError: can only concatenate str (not "int") to str.
  2. + between two strings joins them, and + between two numbers adds them. Python will not guess what + should mean between a string and an int.
  3. Do the arithmetic first, then convert the result to a string: str(age + 1).
  4. Fixed line: print("Next year you will be " + str(age + 1)). Another fix: let print handle the different types with commas: print("Next year you will be", age + 1).

Both fixes print Next year you will be 17.

age = 16
print("Next year you will be " + age + 1)

Running this gives TypeError: can only concatenate str (not "int") to str. The error message names both types involved.

Just for fun

What do you call a number wearing quotation marks?

A string in disguise. Try adding 1 to "16" and Python sees right through it.

Hands-on activity

Data type scavenger hunt

You need: a cereal box, a grocery receipt, a bus or train schedule, or any package from your kitchen, plus Python.

  1. Find eight pieces of information on your item, for example the product name, the mass in grams, the price, "gluten free: yes", the best-before date, the barcode number.
  2. For each, write a variable name (snake_case), a value, and the data type you would choose. Give a one-line reason for each choice.
  3. Type each assignment into Python and print type() of each one to check.
  4. Pick one item where two types could work (like price as float or as whole cents in an int) and explain the trade-off.

What to look for: names like product_name = "Oat Rings" (str), mass_g = 525 (int), price = 4.79 (float), gluten_free = True (bool). A barcode should be a str: it is an identifier, not a quantity, and it can start with 0. A date is best stored as a str for now.

Your turn

1

Give the type of each value: -8, 8.0, "8", False, "False".

2

What is int(9.99)?

3

What is float("12")? What is str(12.5)?

4

Choose a data type for (a) the number of goals in a game, (b) a city name, (c) a temperature in °C to one decimal place, (d) whether it is a holiday.

5

Which of these crash with a ValueError? int("25"), int("2.5"), float("2.5"), int("twenty")

6

Fix this line so it prints Total: 30:

  • print("Total: " + 30)
7

What does print(type(7 / 7)) show? (Hint: / always gives a float.)

More practice: warm-up

1

What are the two Boolean values in Python?

2

Is "2026" an int or a str?

3

What does type(3.14) show?

4

What is int("7") + 3?

5

What is str(7) + "3"?

More practice: on your own

1

What is int(-4.7)? Explain.

2

What are bool(0), bool(-3), bool("") and bool(" ")?

3

Why should a Canadian postal code such as M5V 3L9 be stored as a string, not a number?

4

Predict the output.

  • x = "5"
  • y = 5
  • print(x * 2, y * 2)
5

A student stores a price as price = "4.99" and later writes price * 2. What goes wrong, and how should it be fixed?

Challenge

1

What is bool("False")? Why might this surprise a beginner?

2

Give one expression that converts the string "7.8" to the integer 7, and explain why int("7.8") does not work.

3

Java requires int count = 0; but Python just uses count = 0. Give one advantage and one disadvantage of Python's dynamic typing.

Check your understanding

Quiz

1.

What is the type of "3.5"?

2.

What is int(5.99)?

3.

Which conversion causes a ValueError?

4.

Which is the best type for whether a library book is overdue?

5.

What does print("Score: " + str(8 + 2)) show?

6.

Which value is False when converted with bool()?

For parents and teachers

Big idea: expectation A1.1 names the four types students must use: integers, floating points, strings and Boolean values. Conversions support A1.3 (assignment with expressions), and fixing type errors begins A4.1.

How to help: the scavenger hunt works with anything in the kitchen. Ask "is that something you would ever add or multiply?" That question sorts numbers from labels like phone numbers and postal codes.

Common difficulties:

  • Thinking "16" and 16 are the same. Quotes make text.
  • Expecting int(7.9) to be 8. It cuts off; rounding comes in Lesson 4.
  • Writing true instead of True.
  • Joining text and numbers with +. Convert with str(), or use commas in print.