Python Data Types: Master the 7 Data Types

A data type defines the kind of value a variable holds. It tells Python how to store and manipulate data in memory. Think of data types like containers some hold numbers, some have text and others hold collections of items.

Example: int (Integer), str (String), Sequence Data Types.

Let’s take a look at all the data types one by one.

Numeric: int. float, complex.

Boolean: bool

Sequence: list, tuple, range, string

Set: Set, Frozenset

Binary: bytes, bytearray, memoryview

None: None

Data Types in Python

1. Numeric Data Types

  • Int (Integer)

Integer data type is mainly used for whole numbers, positive or negative numbers without decimals.

Example: Counting objects, iterations and mathematical operations.

				
					age = 20
print(type(age))                         
                      # Output: <class 'int'>
				
			
  • float (Floating Point)

Floating point is used for numbers with decimal points.

Example: Calculating percentages, averages or financial values.

				
					pi = 3.14159
temperature = -12.5

				
			
  • complex

Used for complex numbers with real and imaginary parts.

Example use: Electrical engineering, signal processing, AI simulations.

				
					z = 3 + 5j
print(z.real, z.imag)
                    # Output: 3.0 5.0

				
			

In the above example the code creates a complex number z=3+5j and then uses the built-in attributes (.real and .imag) to print its corresponding parts. The output shows the real component as 3.0 and the imaginary component as 5.0.

2. Boolean Data Type (bool)

Boolean values should be either True or False.

Use case: Conditional statements, loops and logical operations.

				
					is_logged_in = True
is_admin = False
print(5 > 3) 
                         # True
				
			

3. Sequence Data Types

  • list

Lists are used to store mixed data types and are mutable (can be changed).

Example: Storing dynamic collections like user data or API responses.

				
					fruits = ["apple", "banana", "cherry"]
print(fruits)
             # output ['apple', 'banana', 'cherry']

				
			
  • tuple

Tuples are similar to lists but they are immutable  once created, they can’t be changed.

Example: Fixed data such as database records or GPS coordinates.

				
					coordinates = (10, 20, 30)
print(coordinates)
                # output    (10, 20, 30)

				
			
  • range

Represents a sequence of numbers.

Example use: Loops, counters and iteration control.

				
					for i in range(5):
     print(i)
       # output 0
       #        1
       #        2
       #        3
       #        4

				
			
  • str (String)

A string stores text enclosed in single, double quotes. Strings are immutable means once created, they cannot be changed.

				
					name = "Sam"
message = 'Hello, Python!'

				
			

4. Set Data Type

  • set

A set is an unordered collection of unique items.

Use case: Removing duplicates, performing mathematical set operations.

  • frozenset

It is an unordered collection of unique elements, just like a regular set. It is immutable; once created, you cannot add, remove, or modify any elements within it. 

				
					numbers = {1, 2, 3}
print(numbers) 
          # output     {1, 2, 3}


my_set = {"apple", "banana", "cherry"}
immutable_set = frozenset(my_set)

print(f"Frozenset: {immutable_set}")
print(f"Type: {type(immutable_set)}")

try:
    print("\nAttempting to modify the frozenset is prevented.")
except AttributeError as e:
    print(f"\nError on modification: {e}")

#       Frozenset: frozenset({'banana', 'apple', 'cherry'})
#       Type: <class 'frozenset'>

#       Attempting to modify the frozenset is prevented.

				
			

5. Mapping Data Type

  • dict

A dictionary is used to store data as key-value pairs.

Example: JSON data, APIs, configuration files and data mapping.

				
					student = {
  "name": "Rakshath",
    "course": "AI"
}
          # output  {'name': 'Rakshath', 'course': 'AI'}

				
			

6. NoneType

Represents the absence of a value.

Use case: Placeholders for missing or optional data.

				
					x = None
print(type(x)) 
          # output     <class 'NoneType'>

				
			

7. Binary Data Types

Python has special data types to handle binary data, commonly used in files, images and network communication.

  •  bytes

It is an immutable sequence of bytes.

Use case: Reading binary files, encryption or network data transfer.

				
					data = b"Hello"
print(type(data))  # <class 'bytes'>
print(data[0])     # 72 (ASCII value of 'H')

				
			
  • bytearray

It is a mutable version of bytes.

Use case: When you need to modify binary data in memory.

				
					arr = bytearray(b"Python")
arr[0] = 66  # Changes 'P' to 'B'
print(arr)   # bytearray(b'Bython')

				
			
  • memoryview

A memory-efficient view of binary data — allows access without copying.

Use case: High-performance computing and data streaming.

				
					data = bytearray(b"AI Tools")
view = memoryview(data)
print(view[0])  # 65

				
			

Check the Data Type

Use type() to check the data type.

				
					print(type(3.14))  # output <class 'float'>
				
			

Convert between data types easily:

To convert between data types we must uses the desired data type as a function.

				
					int("5")      # 5
float(3)      # 3.0
str(100)      # "100"
				
			

Mutable and Immutable Data Types

1. Mutable Data Types

Mutable objects can be altered in-place after creation. You can modify their contents without creating a new object in memory. The modification operation is fast in mutable data types.

Example: List, Dictionary, Sets

 

2. Immutable Data Types

Immutable objects cannot be changed after creation. Any operation that appears to modify them (like concatenation) actually creates an entirely new object in memory. They are faster for simple access.

Example: String, Tuple, Integer

FAQ (Frequently Asked Questions)

 

1. What is a data type in Python?

A data type in Python defines the kind of value a variable can hold and what operations can be performed on it. It tells Python how to store and interpret the data in memory

x = 10       # int

y = “Hello”  # str

z = 3.14     # float

 

2. What are mutable and immutable data types in Python?

Mutable Data Types(list, set, dict) mean the variable’s value can be changed after its creation whereas immutable datatypes(int, str, float) are those whose variable values cannot be changed.

 

3. How to check data type in Python?

You can check the data type of any variable using the built-in type() function.

x = 42

y = “AI”

print(type(x))  # <class ‘int’>

print(type(y))  # <class ‘str’>

 

4. What is a sequence in Python?

A sequence in Python is an ordered collection of items that can be accessed using an index.
Sequences allow slicing, iteration and membership testing.

Common sequence types:

  • str → sequence of characters
  • list → sequence of elements (mutable)
  • tuple → sequence of elements (immutable)
  • range → sequence of numbers

 

5. Is set mutable in Python?

Yes sets are mutable in Python.
You can add or remove elements after a set is created.

 

6. How to convert a bit to an integer in Python?

In Python, you can convert a bit (binary string) to an integer using the built-in int() function with base 2.

Example 1:

bit_value = “1010”

integer_value = int(bit_value, 2)

print(integer_value)  # Output: 10

 

7. What is a bool data type in Python?

The bool (Boolean) data type in Python represents two truth values: either True or False.
It is commonly used in conditional statements and logical operations.

Example:

is_active = True

is_admin = False

print(type(is_active))  # <class ‘bool’>