Python · Beginner

Data Types in Python

8 min readUpdated September 24, 2026Every example verified

In short: Every value in Python has a type. The core built-in types are int (whole numbers), float (numbers with a decimal point), str (text), bool (True or False) and NoneType (the single value None). type() tells you a value's type, and int(), float() and str() convert between types explicitly, because Python never silently mixes text with numbers.

Why types matter

A type describes what kind of value something is and which operations make sense on it. Adding two integers gives an integer; adding two strings joins them; adding an integer to a string is an error, because Python cannot tell whether you wanted arithmetic or text. Knowing the type of each value in your program is therefore the difference between code that works and code that raises TypeError at the worst moment.

Python is dynamically typed (a variable can refer to a value of any type and you never declare types) but strongly typed (values are never converted behind your back). "3" + 4 does not become 7 or "34"; it fails, and you must say which you meant with int("3") + 4 or "3" + str(4). Among these types, the one conversion Python does perform on its own is int to float in arithmetic: 3 + 0.5 gives the float 3.5, because a whole number can always be represented as a float without changing its meaning.

Five types appear in nearly every program. int holds whole numbers of any size; Python integers never overflow. float holds numbers with a fractional part, stored in binary floating point with roughly 15 to 16 significant decimal digits, which is why some decimals cannot be stored exactly. str holds text and is always written in quotes. bool holds True or False, the results of comparisons. None is a special single value meaning "nothing here", used for missing results and unset options. Lists, tuples, sets and dictionaries are types too; they are collections and get their own lessons, starting with lists.

Syntax

 Python · syntax
type(value)          # the type object, e.g. <class 'int'>
int("42")            # str -> int   (the text must be a whole number)
float("3.25")        # str -> float
str(7)               # number -> str
bool(value)          # truthiness: 0, 0.0, "", None -> False
isinstance(x, int)   # True if x is an int

int() and float() accept surrounding spaces and a leading sign, such as " -42 ", but reject anything that is not a plain number: int("42 kg") and int("3.5") both raise ValueError.

The core built-in types

TypeExample valuesTypical use
int0, 42, -7, 10_000counts, indexes, money in whole cents
float3.14, -0.5, 2.0measurements, averages, percentages
str"hello", 'x', "" (empty)names, messages, anything read by input()
boolTrue, Falseresults of comparisons, on/off flags
NoneTypeNoneno value yet, nothing found

Checking types with type()

Five readings from a home sensor, each of a different type.

 Python
reading = 21
voltage = 3.7
room = "hallway"
active = True
last_error = None
print(type(reading))
print(type(voltage))
print(type(room))
print(type(active))
print(type(last_error))
print(type(reading + voltage))

Output

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>
<class 'float'>

Each literal has a type decided by how it is written: no decimal point gives int, a decimal point gives float, quotes give str, the words True and False give bool, and None is its own type. Mixing int and float in arithmetic produces a float, the only conversion between these types that Python performs without being asked.

Input is text until you convert it

The program is run with two input lines, 18 and 2.5.

 Python
boxes = input()
weight = input()
print(boxes + weight)
boxes = int(boxes)
weight = float(weight)
print(boxes * weight)
print(type(boxes * weight))
print(str(boxes) + " boxes")

Input given to the program: 182.5

Output

182.5
45.0
<class 'float'>
18 boxes

Both input() calls return strings, so the first + joins "18" and "2.5" into "182.5": no error, just a wrong answer, which is why this mistake is so easy to miss. After conversion the multiplication is numeric. Going the other way, str(boxes) is required before joining to text; boxes + " boxes" with an int would raise TypeError.

Floats, division and bool arithmetic

Small facts about numbers that surprise people the first time.

 Python
print(7 / 2)
print(7 // 2)
print(type(7 // 2), type(8 / 2))
print(0.7 + 0.1)
print(round(0.7 + 0.1, 2) == 0.8)
print(True + True)
print(bool(0), bool(""), bool("0"), bool(None))

Output

3.5
3
<class 'int'> <class 'float'>
0.7999999999999999
True
2
False False True False

/ always produces a float, even when the division is exact, so 8 / 2 is 4.0. // keeps integers as integers. 0.7 + 0.1 shows binary floating point at work: neither 0.7 nor 0.1 has an exact binary representation, and the tiny errors add up to a visible one, so compare rounded values rather than raw floats. bool is a subclass of int with True equal to 1, which is why True + True is 2. The last line shows truthiness: zero, empty text and None are False, but the non-empty text "0" is True.

Common mistakes

  • Adding a number to text

    Why it goes wrong: "Total: " + 5 raises TypeError: can only concatenate str (not "int") to str. Python will not guess that you wanted the number turned into text.

    Fix: Convert with str(), pass the values to print() as separate arguments, or use an f-string.

     Python · fix
    total = 5
    print("Total: " + str(total))
    print("Total:", total)
    print(f"Total: {total}")
  • Calling int() on text that holds a decimal

    Why it goes wrong: int("3.5") raises ValueError because the text is not an integer literal; int() only parses whole numbers.

    Fix: Use float("3.5"), and then int() on the result if you really need to drop the fraction.

  • Comparing a number with the text of a number

    Why it goes wrong: input() gives "10", and "10" == 10 is False, so a check such as if choice == 1: never matches typed input and no error tells you why.

    Fix: Convert the input before comparing: if int(choice) == 1:.

  • Testing floats for exact equality

    Why it goes wrong: Values such as 0.1 cannot be stored exactly, so sums that should be equal often differ in the last digit and == reports False.

    Fix: Compare rounded values, or use math.isclose().

     Python · fix
    import math
    print(math.isclose(0.7 + 0.1, 0.8))

Where you use this

Any program that takes data from the outside, whether typed at a prompt, read from a file or received over a network, starts with text. A form asks for an age, a quantity and an amount; all three arrive as strings, and each must be converted to the type its meaning requires before you can compare or add. Deciding that money is stored as an integer number of cents rather than a float avoids rounding surprises in totals. Returning None from a lookup that found nothing lets the caller write if result is None: instead of guessing from a magic value such as -1.

 Python · in practice
age = int(input())
price_cents = round(float(input()) * 100)
if age >= 18 and price_cents > 0:
    print("ok")

Key points

  • int, float, str, bool and None cover most everyday values.
  • type(x) shows the type; isinstance(x, int) tests for it.
  • input() always returns str; convert with int() or float() before arithmetic.
  • Python never converts between str and numbers on its own, but int mixed with float gives float.
  • / always gives a float; // gives an int when both sides are int.
  • Floats are approximations: compare with rounding or math.isclose, not with ==.

Try it yourself

Read the number of litres (a decimal) and the price per litre (a decimal) and print Cost: followed by the total rounded to 2 decimal places with round(). The starter reads the two lines but leaves them as text.

Your program
litres = input()
price = input()
# convert both, then print the cost
Input the program receives: 12.5 ↵ 1.38
Expected output: Cost: 17.25

Practise this

Exercises for this lesson are in the Python practice set.

Open the Python playground

Frequently asked questions

Why does input() always return a string?

Because a line of typed text could mean anything: the characters "12" may be a quantity, a bus route number or part of a postcode. Python hands you the raw text and lets you decide what it means by converting it with int() or float(), or leaving it as str. Converting explicitly also lets you catch bad input where it happens rather than deep inside a calculation.

Is bool a separate type from int in Python?

bool is a subclass of int: True behaves as 1 and False as 0 in arithmetic, so sum([True, False, True]) is 2. type(True) still reports <class 'bool'>, and isinstance(True, int) is True. A practical use is counting how many conditions hold by summing bools.

What is None used for?

None is the single value of type NoneType that means "no value". Functions without a return statement return it, optional settings default to it, and a search that finds nothing often returns it. Test for it with is None or is not None rather than ==, because there is exactly one None object and identity is the clearest check.

Progress is stored only in this browser.

How this page was checked. Every program on it was run with CPython 3.11 at build time; runs in your browser on CPython 3.14 (Pyodide) by the publishing checks, and the output shown is what it printed. The Run buttons execute the same code on your device; nothing you type is sent anywhere.