Python - Datatypes

In Python, a datatype is a classification of data items based on their nature and characteristics. It determines the operations that can be performed on the data and the way in which the data is stored in memory.

Python supports several built-in datatypes, including:

  • Numeric Datatypes:
    • int: used to store integers (positive or negative)
    • float: used to store decimal or floating-point values
    • complex: used to store complex numbers with real and imaginary parts
  • Boolean Datatype:
    • bool: used to store True or False values
  • Sequence Datatypes:
    • str: used to store strings of characters
    • list: used to store a collection of items (mutable)
    • tuple: used to store a collection of items (immutable)
    • range: used to store a sequence of numbers with a specific pattern
  • Mapping Datatype:
    • dict: used to store key-value pairs
  • Set Datatype:
    • set: used to store a collection of unique items (mutable)
    • frozenset: used to store a collection of unique items (immutable)

In Python, the type of a variable can be determined using the type() function. For example:

x = 5
print(type(x))  # output: <class 'int'>

y = 5.0
print(type(y))  # output: <class 'float'>

z = "Hello"
print(type(z))  # output: <class 'str'>

It is important to choose the appropriate datatype for a variable based on the type of data it will be storing and the operations that will be performed on it. Using the wrong datatype can result in errors or unexpected behavior.