Python values and types
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.
print(type(42))print(type(3.14))print(type("hello"))print(type(True))print(type(None))<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.
print(isinstance(42, int))print(isinstance(3, float))print(isinstance(True, int))TrueFalseTrueThe 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).
print(int("42") + 1)print(float("2.5") * 2)print(str(7) + "!")print(int(3.9))435.07!3Conversion 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:
print(bool(0), bool(1))print(bool(""), bool("0"))False TrueFalse Truebool(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:
print("3" + "4")print(3 + 4)print(int("3") + 4)print("3" * 3)3477333What + will not do is mix the two:
print("3" + 4)TypeError: can only concatenate str (not "int") to strThat 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
==. Writeisinstance(x, int), nottype(x) == int. It handles subclasses, such asbool, correctly. Remember thatisinstance(True, int)isTrue, so check forboolfirst if the difference matters. - Assuming
int()rounds.int(3.9)is3. It drops the fractional part. - Treating
input()results as numbers. They are alwaysstr. - Dividing and expecting an int.
7 / 2is3.5, afloat. Use7 // 2for whole-number division.
Predict what each line prints, then run the file to check. Input: none. Expected output: six short type names, one per line.
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
floatintstrboolintstr/ 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.
Predict the output of this file, then run it. Input: none. Expected output: four lines.
print(int("12") + int("30"))print("12" + "30")print(float(4))print(bool(""), bool("False"))Show solution
4212304.0False TrueThe 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,boolandNone. - Use
type()to look andisinstance()to test. Convert explicitly withint(),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.