Python Data Types
Beginner Python Basics

Python Data Types

1Python Data Types: overview

A data type tells Python what kind of value something is — a whole number, a decimal, text, True/False or nothing at all — and therefore what you can do with it.

GoalBy the end you can name Python's basic data types, check a value's type with type(), and convert values with int(), float(), str() and bool().

Before this lesson: Python Variables

2Python Data Types: diagram

Python Data Typestype(value)valuetype42int3.14float"hello"strTrueboolNoneNoneType[1, 2, 3]listconvert: int("7") -> 7, float(7) -> 7.0, str(7) -> "7"
Each value on the left belongs to the type on the right — that's what type() reports. Calling a type's name, like int() or str(), converts a value into a new value of that type.

3Python Data Types: explained step by step

Every value in Python has a type. The type tells Python what kind of thing the value is, and therefore what you can do with it: you can divide two numbers but not two words, and you can make text uppercase but not a number.

The basic types

  • int — whole numbers, positive or negative: 7, -3, 2026. They can be as big as you like, and you may write 1_000_000 with underscores to make it easier to read.
  • float — numbers with a decimal point: 3.14, -0.5, 2.0. Dividing with / always gives a float.
  • str — text, called a string, in single or double quotes: "hello", '42'.
  • bool — a truth value: True or False, always with a capital letter. Comparisons such as 5 > 3 produce a bool.
  • NoneType — the single value None, meaning "nothing here yet" or "no result".

Python also has collection types such as list, tuple, dict and set, which hold many values at once. Each gets its own lesson.

Checking a type with type()

When you're not sure what kind of value you have, ask Python:

Python
print(type(42))
print(type(3.5))
print(type("42"))
print(type(True))
print(type(None))

Notice that "42" is a str, not an int. The quotes make it text that just happens to contain digits.

Why the type matters

The same operator can do different jobs depending on the types involved. + adds numbers but joins strings, and * multiplies numbers but repeats strings:

Python
print(5 + 5)
print("5" + "5")
print("5" * 3)

Mixing types carelessly leads to errors: "5" + 5 raises a TypeError, because Python refuses to guess whether you meant adding or joining.

Converting between types

To turn a value into another type, call the type's name like a function. Conversion creates a new value; the original is left unchanged.

Python
age_text = "29"
age = int(age_text)
print(age + 1)
print(float("2.5") * 2)
print(str(99) + " bottles")
print(int(3.99))
print(bool(0), bool(""), bool("hi"))

A few things to remember:

  • int() cuts off the decimal part instead of rounding, so int(3.99) is 3. Use round() when you want rounding.
  • int("3.5") fails with a ValueError — the text must look like a whole number. Use float() for text with a decimal point.
  • bool() gives False for "empty" values — 0, 0.0, "" and None — and True for almost everything else.

Floats are approximate

Computers store floats in binary, and some decimals can't be stored exactly, so 0.1 + 0.2 comes out as 0.30000000000000004. Every programming language behaves like this. Round the result when you display it, and avoid comparing floats with ==.

Quick reference: Python Data Types

4Python Data Types: syntax

type(value)          # the type of a value, e.g. <class 'int'>
int("42")            # text -> whole number      42
float("2.5")         # text -> decimal number    2.5
str(99)              # number -> text            "99"
bool(0)              # -> False ("empty" values are False)
isinstance(x, int)   # True if x is an int

5Python Data Types: run the example

Press Run (or Ctrl + Enter). The output (step 6) appears directly below the code. Change anything and run it again.

Interactive example
product = "Notebook"
price = 3.5
quantity = 4
in_stock = True
discount = None

print(type(product), type(price))
print(type(quantity), type(in_stock), type(discount))

typed_amount = "6"
quantity = quantity + int(typed_amount)
total = price * quantity
print("Quantity:", quantity)
print("Total:", total)
print("Label: " + product + " x" + str(quantity))
print(type(total), type(str(total)))
Expected output<class 'str'> <class 'float'>
<class 'int'> <class 'bool'> <class 'NoneType'>
Quantity: 10
Total: 35.0
Label: Notebook x10
<class 'float'> <class 'str'>

7Python Data Types: how the example works

Hover or tap a step to highlight its lines in the example above.

  1. Line 1-5Five variables, five types: text (str), a decimal (float), a whole number (int), a truth value (bool) and None, meaning "no discount set".
  2. Line 7-8type() reports the type of each value, and print() shows it as <class '...'>.
  3. Line 10"6" is in quotes, so it's text — exactly the kind of value you get when a user types a number — not an int.
  4. Line 11int("6") converts the text into the number 6, so it can be added: 4 + 6 = 10.
  5. Line 12A float times an int gives a float: 3.5 × 10 = 35.0. The .0 shows it is a float even though it's a whole amount.
  6. Line 13-14The new quantity and the total are printed.
  7. Line 15+ between strings joins them. quantity is an int, so str(quantity) turns it into the text "10" first.
  8. Line 16total is still a float: str(total) made a new string value and left total itself unchanged.

8Python Data Types: common mistakes

Adding a number to text

✗ Wrong

age = "30"
print(age + 1)
TypeError: can only concatenate str (not "int") to str

✓ Right

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

age holds text, and Python won't add a number to text. Convert it with int() first.

Using int() on text with a decimal point

✗ Wrong

price = int("4.99")
ValueError: invalid literal for int() with base 10: '4.99'

✓ Right

price = float("4.99")
print(price)

int() only accepts text that looks like a whole number. Use float() for decimals — and int(float(...)) if you really want to drop the decimal part.

Writing true, false or none in lowercase

✗ Wrong

is_open = true
NameError: name 'true' is not defined. Did you mean: 'True'?

✓ Right

is_open = True
print(is_open)

True, False and None are special values that always start with a capital letter. In lowercase, Python treats them as unknown variable names.

9Python Data Types: exercises

Write your answer, then press Check. The checks run in your browser.

1. Fix the fruit total

Fix the bug Easy

The fruit counts arrive as text. This code should print Total fruit: 20, but it prints Total fruit: 128. Fix it so that total is the number 20.

Your code
apples = "12"
oranges = "8"
total = apples + oranges
print("Total fruit:", total)

2. Predict the types

Predict the output Medium

Read the code and type exactly what it prints. Then press Check to run it and compare.

Read this code
print(type(7 / 2))
print(int(7.9))
print("7" * 2)
print(type("False"))
print(bool(0.0), bool(" "))

2 more exercises → 4 more examples

Finished this lesson?
Progress is saved in this browser — create a free account to keep it across devices.

Questions learners ask

What is the difference between int and float in Python?

An int is a whole number with no decimal point, like 7. A float has a decimal point, like 7.0 or 7.5. Dividing with / always gives a float — even 8 / 2 is 4.0. Use int for counting things and float for measurements and averages.

Why does type() print <class 'int'> instead of just int?

In Python every type is a class, and type() hands you that class object, which prints as <class 'int'>. If you only want the name, use type(x).name, which gives 'int'.

What does None mean in Python?

None is a special value meaning "no value here". It's often used as a placeholder before the real value is known, and it's what a function gives back when it doesn't return anything. Check for it with x is None.

Further reading

10Next lesson

Up nextPython NumbersPython has three built-in number types, int for whole numbers, float for decimal numbers and complex for numbers with a…

Related:
Python OperatorsPython StringsPython Input and OutputPython TypeError: What It Means and How to Fix It

← Python Variables · All Python Basics lessons · Practise in the IDE