Data Types in Python

Python Data Types

In programming, a data type defines the kind of value a variable can hold. Every value in Python has a specific data type, and these types help the interpreter understand what operations can be performed on a value.

Python has several built-in data types, and they can be grouped into different categories.

Built-in Data Types

Here are the main data types in Python:

  1. Text Type:
    • str: Represents a sequence of characters (strings).
  2. Numeric Types:
    • int: Represents whole numbers.
    • float: Represents decimal (floating point) numbers.
    • complex: Represents complex numbers (numbers with real and imaginary parts).
  3. Sequence Types:
    • list: Ordered and changeable collection of items.
    • tuple: Ordered but unchangeable (immutable) collection.
    • range: Represents a sequence of numbers.
  4. Mapping Type:
    • dict: Represents key-value pairs (like a dictionary).
  5. Set Types:
    • set: Unordered collection of unique items.
    • frozenset: Unordered and immutable set.
  6. Boolean Type:
    • bool: Represents two values, True or False.
  7. Binary Types:
    • bytes: Immutable sequence of bytes.
    • bytearray: Mutable sequence of bytes.
    • memoryview: Provides a view of the memory of an existing object.
  8. None Type:
    • NoneType: Represents the absence of a value (similar to null in other languages).

Getting the Data Type

To check the type of a value or variable, you can use the type() function.

Example:

python
1
2
3
4
5
6
7
x = 5
y = "Hello"
z = [1, 2, 3]

print(type(x))  # Output: <class 'int'>
print(type(y))  # Output: <class 'str'>
print(type(z))  # Output: <class 'list'>

Frequently Asked Questions