Variables are containers that can hold data values. Variables in Python are dynamically typed, meaning the datatype is inferred from their assigned value.
Python has the following data types built-in by default, in these categories:
- Text Type: str
- Numeric Types: int, float, complex
- Sequence Types: list, tuple, range
- Mapping Type: dict
- Set Types: set, frozenset
- Boolean Type: bool
- Binary Types: bytes, bytearray, memoryview
- None Type: NoneType
There is a function to check the datatype of a variable or data structure. The command is:
type(variable_name)
'''
output:
<class 'datatype'>
'''
It returns the class the variable belongs to.
Why does this return a class?
In Python, datatypes are implemented using classes. There are classes called int, str, float, complex, and more. When we create a variable of a type, we are effectively creating an object of the class of the datatype.
Simple exercise:
a = 12 # int
b = 12.42 # float
c = "Django tutorial" # str
print("a: ", type(a))
print("b: ", type(b))
print("c: ", type(c))
'''
output:
a: <class 'int'>
b: <class 'float'>
c: <class 'str'>
'''