Table of contents

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, and NoneType.
  • 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

  1. Int 
  2. Float
  3. Bool 
  4. 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

  1. String
  2. Bytes
  3. Bytearray
  4. 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

  1. List
  2. 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.

  1. set
  2. 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

  1. 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 as float, not int.
# int data type num = 10 print(num,type(num)) num1 = 0 print(num1,type(num1)) num2 = -23 print(num2,type(num2))

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.
# float data type num1 = 23.5 print(num1,type(num1)) num2 = -22.5 print(num2,type(num2)) num3 = 0.5 print(num3,type(num3))
# Mantisa var = 2e3 print(var, type(var)) var1 = -3e3 print(var1,type(var1)) var2 = 4e-2 print(var2,type(var2))

3. Bool Data Type

  • bool is a pre-defined Python class used to store logical values: True and False.
  • Internally, True is represented as 1 and False as 0, allowing it to integrate seamlessly with numerical operations.
  • Boolean values are commonly used in logical expressions, conditional statements, and control flow structures.
# Bool Data Type var = True print(var,type(var)) var1 = False print(var1,type(var1)) Print(True+False)

4. Complex Data Type

  • mplex is a pre-defined Python class used to store and manipulate complex numbers in the format a + bj or a - bj, where a is the real part and b is the imaginary part with j 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 and complexobj.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.
# Complex Number var = 2+3j print(var,type(var)) print(var.real) # it will give the real part print(var.imag) # it will give the imaginary part)