Python values and types

Beginner25 min

Name Python's basic types, check them with type and isinstance, and convert between them on purpose.

What you will learn
  • Identify int, float, str, bool and None values
  • Inspect a value with type() and isinstance()
  • Convert between types with int(), float(), str() and bool()
On this page

Every value in a Python program has a type, and the type decides what you can do with it. You can add two numbers, repeat a string, but not subtract one string from another.

The general ideas of types, and the difference between static and dynamic typing, are in the fundamentals lesson. This lesson is the Python version: which types you get and how to inspect them.

The five basic types

Type Example Holds
int 42, -7 Whole numbers of any size
float 3.14, 2.0 Numbers with a fractional part
str "hello", 'hi' Text
bool True, False Yes or no, usually the answer to a comparison such as 7 > 2
NoneType None “No value”

None is Python’s way to say “nothing here”. It is the only value of its own type.

type() and isinstance()

type(value) tells you the exact type of a value.

type_of_values.py
print(type(42))
print(type(3.14))
print(type("hello"))
print(type(True))
print(type(None))
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>

If you only want the short name, add .__name__ after the call: type(42).__name__ is int. The exercises below use that form.

isinstance(value, type) asks whether a value is of a given type, which is usually the better question.

isinstance_basics.py
print(isinstance(42, int))
print(isinstance(3, float))
print(isinstance(True, int))
Output
True
False
True

The last line surprises people. In Python, bool is a special kind of int, where True behaves like 1 and False like 0. Remember it, because it comes back in a later lesson.

Conversion

You convert between types by calling the type like a function: int("42"), float("2.5"), str(7), bool(0).

conversions.py
print(int("42") + 1)
print(float("2.5") * 2)
print(str(7) + "!")
print(int(3.9))
Output
43
5.0
7!
3

Conversion is explicit, and int(3.9) drops the fractional part rather than rounding. Python will not guess for you, as the next section shows.

bool() turns any value into True or False:

bool_conversion.py
print(bool(0), bool(1))
print(bool(""), bool("0"))
Output
False True
False True

bool(0) is False but bool("0") is True: any non-empty string counts as true, even the string "0". A later lesson covers truthiness in full.

Dynamic and strong typing

Python is dynamically typed: a name can be bound to a value of any type, and types are checked while the program runs, not before. It is also strongly typed: it does not quietly turn one type into another to make an operation work.

The same + joins strings and adds numbers, depending on the types:

strong_typing.py
print("3" + "4")
print(3 + 4)
print(int("3") + 4)
print("3" * 3)
Output
34
7
7
333

What + will not do is mix the two:

print("3" + 4)
Output
TypeError: can only concatenate str (not "int") to str

That error is useful. It tells you that a number and some text met, and you must decide which one to convert.

Common pitfalls

  • Comparing types with ==. Write isinstance(x, int), not type(x) == int. It handles subclasses, such as bool, correctly. Remember that isinstance(True, int) is True, so check for bool first if the difference matters.
  • Assuming int() rounds. int(3.9) is 3. It drops the fractional part.
  • Treating input() results as numbers. They are always str.
  • Dividing and expecting an int. 7 / 2 is 3.5, a float. Use 7 // 2 for whole-number division.
ExerciseType detective

Predict what each line prints, then run the file to check. Input: none. Expected output: six short type names, one per line.

type_detective.py
print(type(7 / 2).__name__)
print(type(7 // 2).__name__)
print(type("7").__name__)
print(type(7 > 2).__name__)
print(type(int("7")).__name__)
print(type(str(7.0)).__name__)
Show solution
float
int
str
bool
int
str

/ always gives a float and // of two ints gives an int. A comparison produces a bool. int("7") is an int, and str(7.0) is the text "7.0", so it is a str.

ExercisePredict the conversions

Predict the output of this file, then run it. Input: none. Expected output: four lines.

predict_conversions.py
print(int("12") + int("30"))
print("12" + "30")
print(float(4))
print(bool(""), bool("False"))
Show solution
42
1230
4.0
False True

The first line adds two numbers. The second joins two strings. float(4) is 4.0. On the last line bool("") is False, but bool("False") is True, because any non-empty string is true, whatever it says.

Summary

  • The core types are int, float, str, bool and None.
  • Use type() to look and isinstance() to test. Convert explicitly with int(), float(), str(), bool().
  • Python is dynamically but strongly typed: it will not silently mix text and numbers.

Next: what a variable actually is in Python.