Data Types
- Data types classify data items based on their values, helping to define the rules for managing and processing data.
- A variable's data type is determined by the type of value it holds, and in Python, it doesn't need to be explicitly declared.
- Data types in Python are represented as classes, and variables are instances of these classes.
-
Python has built-in data types, including
int
,float
,complex
,str
,bool
,list
,tuple
,set
,frozenset
,dict
, andNoneType
.
- The main purpose of data types is to allocate sufficient memory space for storing the program's input and processing it effectively.
In Python, the different types can be categorized into five major categories based on their usage and functionality. Here's how the 14 common types can be divided:
1. Fundamental category Data Type
The purpose of Fundamental Category Data Types is "To store Single Values".It contains 4 data types. They are
- Int
- Float
- Bool
- Complex
2. Sequence Category Data Type
The purpose of Sequence Category Data Type is "To Store Sequence of Values". We have 4 data types in the Sequence Category. They are
- String
- Bytes
- Bytearray
- range
3. List Category Data Type
The purpose of List Category Data Types is "To store multiple values either of the same type or different type or both types with unique and duplicates in single objects." We have 2 types in List Category. They are
- List
- tuple
4. Set Category Data Types
The purpose of set Category Data Types is To store multiple values either of the same type or different types or both types with unique Values in a single object.
- set
- frozen set
5. Mapping Data Type
In Python, mapping refers to data structures that store data in key-value pairs, where each key maps to a corresponding value. The primary mapping data type in Python is dict
. Here’s how mapping works in a dict
- dict
1. Int Data Type:
-
int
is a built-in Python class for storing and manipulating whole numbers (without decimal places), including positive, negative, and zero values.
- Python integers have no size limit, allowing them to grow as large as memory permits, and support various operations like arithmetic, comparisons, and bitwise manipulations.
-
Strings representing integers can be converted using
int()
, and numbers with decimal points are treated asfloat
, notint
.
2. Float Data Type
-
float
is a pre-defined Python class used to store and manipulate numbers with decimal places, also known as floating-point or real numbers (e.g., 23.45, -24.56, 0.99, -0.8).
- A float value consists of two parts: the integer part and the decimal part, such as 23 and 0.45 in the number 23.45.
-
Floats can also represent scientific notation, where a number is expressed as
mantissa × 10^exponent
(e.g.,2.5e3
equals 2500.0).
- Scientific notation in floats saves memory space when representing very large or very small numbers efficiently.
- The float data type cannot store number system data (e.g., binary, octal, hexadecimal) directly.
- Floats are useful for computations that require precision beyond whole numbers, such as calculations involving fractions or continuous measurements.
3. Bool Data Type
-
bool
is a pre-defined Python class used to store logical values:True
andFalse
.
-
Internally,
True
is represented as1
andFalse
as0
, allowing it to integrate seamlessly with numerical operations.
- Boolean values are commonly used in logical expressions, conditional statements, and control flow structures.
4. Complex Data Type
-
mplex
is a pre-defined Python class used to store and manipulate complex numbers in the formata + bj
ora - bj
, wherea
is the real part andb
is the imaginary part withj
representing the square root of-1
. -
The real and imaginary parts of a complex number can be accessed using the attributes
.real
and.imag
, respectively (e.g.,complexobj.real
andcomplexobj.imag
). - By default, both the real and imaginary parts of a complex number are treated as floating-point values.
- Arithmetic operations such as addition, subtraction, and multiplication are supported for complex numbers.
- In complex number representation, the real part can use any number system (e.g., decimal, binary), but the imaginary part must be represented using the decimal number system only.