A5. Python Data Types
On this page
This tutorial will guide you through the various data types available in Python, and provide examples to help you understand their usage.
Understanding data types is crucial in programming, as they determine the kind of information that can be stored in a variable. For instance, in the following example, the variable “num” is assigned the integer value of 24:
num = 24
As a result, the data type of “num” is of the int class.
Python Data Types
Data Types | Classes | Description |
---|---|---|
Numeric | int, float, complex | holds numeric values |
String | str | holds sequence of characters |
Sequence | list, tuple, range | holds collection of items |
Mapping | dict | holds data in key-value pair form |
Boolean | bool | holds either True or False |
Set | set, frozeenset | hold collection of unique items |
In Python programming, all entities are represented as objects, with data types being classes and variables being instances of these classes.
Python Numeric Data type
In Python, the numeric data type is utilized to store numerical values. This includes integers, floating-point numbers, and complex numbers, which are defined as int, float, and complex classes in Python.
The int class is used to hold signed integers of unlimited length, while the float class is used to hold numbers with decimal points and has a level of accuracy of up to 15 decimal places. The complex class is used to hold complex numbers.
We can easily determine the class a variable or value belongs to by using the type() function. For example, if we want to know the class of the variable x, we can simply use the type(x) function.