04-data-types.py
python-for-j277 / getting-started  ·  Lesson 04
Lesson 04 — Getting started

Data Types

The four basic types of value, and how to check them.

Every value in Python has a type. The type tells you what you can do with a value. For GCSE you need four of them.

The four basic data types

TypeWhat it storesExamples
String (str)Text, written in quotes"hello", "B", "14"
Integer (int)A whole number14, -3, 0
Float (float)A number with a decimal point1.62, 3.0, -0.5
Boolean (bool)One of two values onlyTrue, False

Checking a type with type()

types.py
print(type("hello"))
print(type(14))
print(type(1.62))
print(type(True))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
How it works
  • type() tells you the data type of a value.
  • str is text, int is a whole number, float has a decimal point, bool is True or False.

Booleans: True and False

bool.py
is_logged_in = True
has_paid = False
print(is_logged_in)
print(type(has_paid))
Output
True
<class 'bool'>
How it works
  • A boolean stores one of just two values: True or False (note the capital letters).
  • Booleans are how a program records a yes/no fact.
  • They become very important for making decisions — the next topic.
Quotes change the type14 is an integer, but "14" is a string. They look the same on screen but behave differently: "14" + "14" gives '1414', while 14 + 14 gives 28.
Remember the fourstr, int, float, bool. Picking the right type prevents errors and surprises.

What you have learned

  • Every value has a type: str, int, float or bool.
  • Use type() to check a value's type.
  • A boolean is either True or False and records a yes/no fact.
  • Quotes make a value a string, even when it looks like a number.