Code of the Day

Data types — integers, floats, strings, booleans

Values come in different kinds. Knowing what kind of value you have tells you what you can do with it.

First Steps9 min read
By the end of this lesson you will be able to:
  • Identify the four basic Python types — int, float, str, bool
  • Use type() to inspect the type of any value
  • Explain why mixing incompatible types causes a TypeError

Every value in Python has a type — a classification that determines what the value represents and what operations make sense on it. You would not try to multiply someone's name by two, and Python applies the same logic: operations that do not make sense for a type produce an error rather than a silent, wrong answer.

Understanding types early prevents a whole class of beginner bugs and makes error messages much easier to read.

The four basic types

int — whole numbers, positive or negative, of any size:

year = 2024
temperature = -5
population = 8_100_000_000

float — numbers with a decimal point (short for "floating-point"):

price = 9.99
pi = 3.14159
half = 0.5

str — text, always wrapped in single or double quotes:

greeting = "hello"
city = 'London'
empty = ""

bool — exactly two possible values: True or False. Note the capital letter; true (lowercase) is not a keyword in Python.

is_logged_in = True
has_error = False

type() as a diagnostic tool

Python's built-in type() function tells you the type of any value. This is invaluable when something unexpected happens:

Python — editable, runs in your browser

Notice that "42" is a str, not an int. The quotes make it text. You cannot do arithmetic with it directly.

Why type mismatches cause errors

Python refuses to guess what you mean when you combine incompatible types:

Python — editable, runs in your browser

Run this and you will see a TypeError. Python will not silently convert 30 to "30" — that decision belongs to you. Fix it by converting explicitly:

message = "I am " + str(age) + " years old"

Or use an f-string, which handles conversion automatically:

message = f"I am {age} years old"

int and float mix freely because the promotion rule is unambiguous: Python always widens to float when one operand is a float. So 1 + 2.0 gives 3.0. The error only appears at the less obvious boundary — like adding a number to a string — where there is no single sensible answer.

bool is a subtype of int in Python: True == 1 and False == 0. This means True + True gives 2, which looks like a bug if you are not expecting it. When you need a proper yes/no flag, use a bool variable and treat it as boolean — do not do arithmetic with it.

Exercise

One of each typePython

Create four variables: my_int (any integer), my_float (any float), my_str (any string), my_bool (any boolean). Each must be the correct type.

type(my_int)<class 'int'>type(my_float)<class 'float'>type(my_str)<class 'str'>type(my_bool)<class 'bool'>

Where to go next

Next: conditionals — using boolean values to make decisions, so different situations lead to different code paths.

Finished reading? Mark it complete to track your progress.

On this page