What are Python’s built-in data types?

Home » Python » What are Python’s built-in data types?

Table of Contents

Introduction to Python’s built-in data types

Python’s built-in data types refers to the types of values that Python provides. Which is to represent different kinds of data.

They are the fundamental types of values it uses to represent different kinds of data.

These data types are fundamental for performing various operations and they are essential for any Python program.

1.Numeric Types:

  • int (Integer): Used to represent whole numbers, both positive and negative, without any decimal point.
  • float (Floating point): Represents real numbers or numbers with decimal points.
  • complex: Represents complex numbers, which have a real part and an imaginary part.

Examples:


a = 10         # int
b = 3.14       # float
c = 2 + 3j     # complex

2.Sequence Types:

  • list: A mutable (changeable) ordered collection of items. Lists can contain elements of different data types (e.g., integers, strings, etc.).
  • tuple: An immutable (unchangeable) ordered collection of items. Once a tuple is created, its elements cannot be changed.
  • range: Represents a sequence of numbers, typically used in loops.

Examples:


lst = [1, 2, 3, 4]   # list
tpl = (1, 2, 3, 4)   # tuple
r = range(5)         # range (0, 1, 2, 3, 4)

3.Text Type:

  • str: Represents a sequence of characters (a string). Strings are immutable in Python.

name = "Alice"  # str

4.Mapping Type:

  • dict (Dictionary): A dictionary is a collection of key-value pairs. Each key is unique, and it’s associated with a specific value. Consequently, dictionaries are mutable and unordered.

person = {"name": "Alice", "age": 30}  # dict

5.Set Types:

  • set: A set is an unordered collection of unique elements. Consequently, it does not permit duplicate values and is mutable.
  • frozenset: An immutable version of a set. Once created, its contents cannot be modified.

s = {1, 2, 3}        # set
fs = frozenset([1, 2, 3])  # frozenset

6.Boolean Type:

  • bool: Represents one of two values: True or False.Therefore, It is often used in conditional statements and logical operations.

x = True    # bool
y = False   # bool

7.Binary Types:

  • bytes: Immutable sequence of bytes. Typically used to represent binary data.
  • bytearray: Mutable sequence of bytes.
  • memoryview: An ArrayBufferView object provides a view into the underlying binary data of an ArrayBuffer. Consequently, it allows you to access and manipulate the data directly, which is particularly useful for performance-critical operations. You can use it to interact with other buffer objects such as bytes or bytearray.

b = b"hello"         # bytes
ba = bytearray([65, 66, 67])  # bytearray
mv = memoryview(b"hello")  # memoryview

8.None Type:

  • None: None represents the absence of a value or a null value. Therefore, it’s often used to indicate that something lacks a value or that a function doesn’t return anything.

x = None  # None

Conclusion of Python’s built-in data types:

Python offers a diverse range of built-in data types, including numbers, sequences, and collections, to accommodate various data types.

Consequently, these data types form the foundation of Python programming and can be combined or extended to construct intricate data structures and algorithms.

Understanding these types and when to use each one is essential for writing effective Python code.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *