How Many Data Types in Python with examples

Python offers various data types in Python, for different purposes. Integer (int) represents whole numbers. Floating-point (float) handles decimal numbers. Strings (str) store text. Booleans (bool) manage true or false values. Lists (list) are versatile, ordered collections. Tuples (tuple) are similar to lists but immutable. Dictionaries (dict) store key-value pairs. Sets (set) manage unique elements. None (NoneType) denotes the absence of a value.

These data types serve diverse roles in data manipulation, control flow, and data storage, making Python versatile for tasks ranging from mathematics and text processing to data structures and program logic.

What is a data types in Python with example?

In Python, a data type is a classification that specifies which type of value a variable can hold. Data types are essential because they determine the kind of operations that can be performed on a variable and how that data is stored in memory. Python has several built-in data types, including:
which of the following data types is immutable in Python?
  1. Integer (int): Represents whole numbers, both positive and negative. For example: 42, -7.
  2. Floating-Point (float): represents real numbers (numbers with decimal points). For example: 3.14, -0.5.
  3. String (str): represents text, enclosed in single or double quotes. For example: "Hello, World!", 'Python'.
  4. Boolean (bool): Represents either True or False, often used for logical comparisons.
  5. List (list): represents an ordered collection of values, which can be of different types. For example: [1, 2, 3], ['apple', 'banana', 'cherry'].
  6. Tuple (tuple): Similar to a list but immutable (cannot be changed after creation). For example: (1, 2, 3).
  7. Dictionary (dict): Represents a collection of key-value pairs. For example: {'name': 'John', 'age': 30}.
  8. Set (set): Represents an unordered collection of unique values. For example: {1, 2, 3}.
  9. NoneType (None): Represents the absence of a value or a null value.

Python is dynamically typed, meaning you don’t need to declare the data type of a variable explicitly. The interpreter determines the data type based on the value assigned to the variable. You can check the data type of a variable using the type() function. Data types are important because they affect how you can manipulate and interact with data in your Python programs.

How many data types in Python:

Python has several built-in data types that serve various purposes. Here are the main types in Python:

  1. Numeric Types:
    • int: Represents integers (whole numbers).
    • float: Represents floating-point numbers (real numbers with decimal points).
    • complex: Represents complex numbers.
  2. Text Type:
    • str: Represents text and is enclosed in single or double quotes.
  3. Boolean data types in Python:
    • bool: Represents boolean values, either True or False.
  4. Sequence Types:
    • list: Represents ordered, mutable collections of elements.
    • tuple: Represents ordered, immutable collections of elements.
    • range: Represents sequences of numbers.
  5. Mapping Type:
    • dict: Represents unordered collections of key-value pairs.
  6. Set data type in Python:
    • set: Represents unordered collections of unique elements.
    • frozenset: Represents immutable sets.
  7. Binary Types:
    • bytes: Represents sequences of bytes (immutable).
    • bytearray: Represents sequences of bytes (mutable).
    • memoryview: Represents memory view of objects, used for binary data.
  8. None Type:
    • NoneType: Represents the absence of a value or a null value (usually denoted as None).
  9. Custom Data Types:
    • You can create custom data types using classes and objects.

These built-in data types cover a wide range of data manipulation needs in Python. Understanding the different data types and when to use them is crucial for writing effective Python code.

Built-in Data Types:

The concept of data type is crucial in programming. There are various sorts of data that can be stored in variables, and various types can perform different functions. The following data types are pre-installed in Python and fall into these categories:

Text Type: str
Numeric Types: intfloatcomplex
Sequence Types: listtuplerange
Mapping Type: dict
Set Types: setfrozenset
Boolean Type: bool
Binary Types: bytesbytearraymemoryview
None Type: NoneType


Getting the data type:

Using the type() method, you may determine the data type of any object:


Print the data type of the variable x:

x = 5
Example Data Type
x = “Hello World” str
x = 20 int
x = 20.5 float
x = 1j complex
x = [“apple”, “banana”, “cherry”] list
x = (“apple”, “banana”, “cherry”) tuple
x = range(6) range
x = {“name” : “John”, “age” : 36} dict
x = {“apple”, “banana”, “cherry”} set
x = frozenset({“apple”, “banana”, “cherry”}) frozenset
x = True bool
x = b”Hello” bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType


Setting the Specific Data Types in Python:

The following constructor functions can be used to indicate the data type:

Example Data Type
x = str(“Hello World”) str
x = int(20) int
x = float(20.5) float
x = complex(1j) complex
x = list((“apple”, “banana”, “cherry”)) list
x = tuple((“apple”, “banana”, “cherry”)) tuple
x = range(6) range
x = dict(name=”John”, age=36) dict
x = set((“apple”, “banana”, “cherry”)) set
x = frozenset((“apple”, “banana”, “cherry”)) frozenset
x = bool(5) bool
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5))


Python Numbers:

Python has three different sorts of numbers:

  • int
  • float
  • complex

Numerical variables are generated when values are assigned to them.


x = 1    # int
y = 2.8  # float
z = 1j   # complex

Use Python’s type() method to confirm an object’s type.



Int: An integer, or int, is a whole number of any length that can be either positive or negative and is devoid of digits.



x = 1
y = 35656222554887711
z = –3255522

Float data types in Python A positive or negative number with one or more decimals is called a “floating point number” or “float”.



x = 1.10
y = 1.0
z = –35.59

Scientific numbers denoted with a “e” to represent the power of ten can also represent floats.



x = 35e3
y = 12E4
z = –87.7e100

Complex data types in Python: For complex numbers, the imaginary portion is represented by a “j”:



x = 3+5j
y = 5j
z = -5j

Type Conversion:

To convert between different types, use the int(), float(), and complex() methods:


Convert from one type to another:

x = 1    # int
y = 2.8  # float
z = 1j   # complex

#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)

Note: Complex numbers cannot be changed from one number type to another.

Random Number:

There is no random() function in Python. To create a random number, import the random module and show a number between 1 and 9; alternatively, you can use the random module that comes with Python to create random numbers:


After importing the random module, a random number between 1 and 9 will be displayed:

import random

Mutable and Immutable data types in Python:

In Python, data types can be classified into two main categories: mutable and immutable. This classification is based on whether the data type’s value can be changed (mutated) after it is created.

Mutable Data Types of data:

1. List (list): Lists are mutable, meaning you can change their contents, add or remove elements, and modify individual elements.

my_list = [1, 2, 3]
my_list[0] = 0 # Modifying an element
my_list.append(4) # Adding an element
my_list.remove(2) # Removing an element

2. Dictionary (dict): Dictionaries are mutable, allowing you to add, update, or delete key-value pairs

my_dict = {‘name’: ‘Alice’, ‘age’: 30}
my_dict[‘age’] = 31 # Modifying a value
my_dict[‘city’] = ‘New York’ # Adding a new key-value pair
del my_dict[‘age’] # Deleting a key-value pair

Immutable Data Types:

Basic types in Python – Python has several basic built-in data types for handling different kinds of data. The basic data types in Python include:

1. Integer (int): Integers are immutable. When you perform operations on integers, you create new integer objects instead of modifying the original ones.

x = 5
x = x + 1 # This creates a new integer object

2. Floating-Point (float): Floating-point numbers are also immutable.

y = 3.14
y = y * 2 # This creates a new float object

3. String (str): String data types in Python are immutable, meaning you can’t change the characters within a string after it’s created. Operations on strings return new string objects.

text = “Hello”
new_text = text + “, World!” # This creates a new string

4. Tuple (tuple): Tuples are immutable sequences, and their elements cannot be changed after creation.

my_tuple = (1, 2, 3)
# You can’t modify my_tuple directly

5. Boolean (bool): Booleans data types in Python are also immutable, and you can’t change True to False or vice versa.

is_valid = True
# You can’t change is_valid to False directly

Immutable data types are useful for ensuring data integrity, as they cannot be accidentally modified. Mutable data types are valuable when you need to modify data frequently. Understanding the mutability of data types is important when working with Python, as it impacts how you design and manipulate your data structures.

Leave a Comment