Each variable must be categorized into a type of value.
Based on the type, the variable will be used for the mathematical or logical operations.
For example, we can use mathematical operations such as addition, multiplication can only be applied on numbers. It can either be integer or decimal numbers. These mathematical operations cannot be applied to text value.
By identifying the data type, the corresponding operations only be performed on the variables.
If the datatype is known then the errors that occur from inappropriate operations on variables can be avoided.
Major datatypes in Python are,
- Int: Used to store positive and negative whole numbers.
- Float: Used to store positive and negative decimal numbers.
- String: Used to store text/string values. Allows alpha numeric.
- Boolean: Only store true or false values.
NOTE WHILE USING BOOLEAN: True must start with a capital letter, or it will produce an error. It’s the same for False. (True and False)
a = 10 b = "A string" c = 10.5 d = True e = """This is a multiline string. Second line.""" print(type(a)) print(type(b)) print(type(c)) print(type(d)) print(type(e))
<class 'int'> <class 'str'> <class 'float'> <class 'bool'> <class 'str'>
Type() function: Helps us to know the data type of the variable.