📝

Python Data Types Overview

Sep 6, 2025

Overview

This lecture introduces Python's built-in data types, how to determine an object's data type, and how to assign or specify types using assignment and constructors.

Python Built-in Data Types

  • Python has built-in data types: str, int, float, complex, list, tuple, range, dict, set, frozenset, bool, bytes, bytearray, memoryview, and NoneType.
  • Types are grouped as: Text (str), Numeric (int, float, complex), Sequence (list, tuple, range), Mapping (dict), Set (set, frozenset), Boolean (bool), Binary (bytes, bytearray, memoryview), None (NoneType).

Determining Data Types

  • Use the type() function to get the data type of any object.
  • Example: type(x) returns the type of variable x.

Assigning Data Types

  • Python sets the variable's data type automatically when a value is assigned.
  • Examples:
    • x = "Hello World" sets x as str.
    • x = 20 sets x as int.
    • x = 20.5 sets x as float.
    • x = 1j sets x as complex.
    • x = ["apple", "banana", "cherry"] is a list.
    • x = ("apple", "banana", "cherry") is a tuple.
    • x = range(6) is a range.
    • x = {"name" : "John", "age" : 36} is a dict.
    • x = {"apple", "banana", "cherry"} is a set.
    • x = frozenset({"apple", "banana", "cherry"}) is a frozenset.
    • x = True is a bool.
    • x = b"Hello" is bytes.
    • x = bytearray(5) is a bytearray.
    • x = memoryview(bytes(5)) is a memoryview.
    • x = None is NoneType.

Specifying Data Types with Constructors

  • Use constructors to specify a type, e.g., x = str("Hello World").
  • Examples:
    • int(20), float(20.5), complex(1j), list(("apple", "banana")), tuple(("apple", "banana")), dict(name="John", age=36), set(("apple", "banana")), frozenset(("apple", "banana")), bool(5), bytes(5), bytearray(5), memoryview(bytes(5)).

Key Terms & Definitions

  • Data type — Category that defines the type of value a variable holds.
  • type() — Function that returns the type of an object.
  • Constructor — A function like int(), list(), etc., used to create objects of a specific type.

Action Items / Next Steps

  • Practice declaring variables of different data types in Python.
  • Use type() to check the types of your variables.
  • Experiment with constructors to explicitly create variables of specific types.