Variables are containers that can hold data values. Variables in Python are dynamically typed, Meaning the datatype is inferred frm 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 by using classes. so there are classes called int, str, float,complex and more. when we create a variable of a type, what we effectively are doing is 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'>
'''