Skip to main content

Data Types in Python

Learn more about Data Types in Python (with Samples)

Python supports various data types that allow you to work with different kinds of data.

Here's an overview of common data types in Python:

Numeric Data Types

  • int: Represents integers, e.g., 5, -10, 1000.
  • float: Represents floating-point numbers (real numbers), e.g., 3.14, 0.001, 2.0.
  • complex: Represents complex numbers, e.g., 3 + 4j, where 3 is the real part and 4 is the imaginary part.

Text Data Type

  • str: Represents strings of text, e.g., "Hello, World", 'Python', "123".

Boolean Data Type

  • bool: Represents boolean values, either True or False. Used for logical operations and control flow.

Sequence Types

  • list: Ordered, mutable sequences of elements, e.g., [1, 2, 3], ['apple', 'banana', 'cherry'].
  • tuple: Ordered, immutable sequences, e.g., (1, 2, 3), ('red', 'green', 'blue').
  • range: Represents a sequence of numbers, often used in loops, e.g., range(5) produces 0, 1, 2, 3, 4.

Mapping Type

  • dict: Represents dictionaries, which are collections of key-value pairs, e.g., {'name': 'Alice', 'age': 30}.

Set Types

  • set: Represents an unordered collection of unique elements, e.g., {1, 2, 3}.
  • frozenset: Similar to a set but is immutable.

Binary Data Types

  • bytes: Represents sequences of bytes, e.g., b'hello'.
  • bytearray: Mutable sequences of bytes.

None Type

  • None: Represents the absence of a value or a null value.

Custom Classes

You can define your own classes and create custom data types in Python.

These are the fundamental data types in Python. You can perform various operations and manipulations on these data types to process and manipulate data in your Python programs.

Coding Sample

Here's a quick example:

# Numeric data types
age = 30
height = 5.9
complex_num = 3 + 4j

# Text data type
name = "Alice"

# Boolean data type
is_student = True

# Lists
fruits = ['apple', 'banana', 'cherry']

# Dictionary
person = {'name': 'John', 'age': 25}

# Sets
unique_numbers = {1, 2, 3}

# None type
no_value = None

✅ In Summary

Understanding these data types is fundamental to writing Python code and performing various operations on data in Python.

✅ Resources