Python Data Types
Published May 6, 2021Updated May 11, 2023
Contribute to Docs
Python is a strongly typed language, in the sense that at runtime it prevents typing errors and it engages in little implicit type conversion or casting, i.e. converting one type to another without a specific call to a conversion function.
codecademy = 575codecademy = "575 broadway"
After line 1, codecademy is an int. After line 2, codecademy is a str.
Python includes the following categories of built-in data types:
- String type:
str - Boolean type:
bool - Binary types:
bytes,bytearray,memoryview - Number types:
int,float,complex - Sequence Types:
list,range,tuple - Set types:
set,frozenset - Dictionary type:
dict
type()
The type() function can be used to retrieve the data type of an object:
message = "Hello, world!"print(type(message))# Output: <class 'str'>
isinstance()
The isinstance() function can be used to test if an object is an instance of a specified type. This will print a boolean value for each function call, indicating if the object is an instance of the given type:
word = "purple"languages = ("Python", "JavaScript", "Go")print(isinstance(word, str)) # Output: Trueprint(isinstance(languages, list)) # Output: Falseprint(isinstance(languages, tuple)) # Output: True
All contributors
- AndrewBarbour
- Anonymous contributor
- Anonymous contributor
renerepina
Christine_Yang- StevenSwiniarski
christian.dinh
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours