Skip to main content

Command Palette

Search for a command to run...

Introduction to Data Types in Python

An Introductory Guide to Core Python Object Types and Data Types for Beginners

Published
•3 min read•View as Markdown
Introduction to Data Types in Python
H

I believe the best way to master a concept is to explain it to someone else. I’m a Full Stack Developer navigating the ecosystems of JavaScript and Python, building everything from responsive frontends to robust backends. I use this space to document my learning journey, break down complex topics, and share practical solutions to the bugs I encounter. Let's learn in public together!

When working with any programming language, understanding data types is fundamental—and Python is no different. Python is a dynamically typed language, which means you don't need to explicitly define the data type of a variable. Python automatically detects and assigns the appropriate data type based on the value you assign.

In this blog, we'll get a brief overview of the core data types in Python. These are also often referred to as object types because, in Python, everything is an object.

🔢 Number Types

Python supports several numeric types, including:

  • int: Integer values
    x = 1234

  • float: Decimal values
    pi = 3.1415

  • complex: Complex numbers with real and imaginary parts
    z = 3 + 4j

  • bin: Binary representation
    0b111

  • Decimal: High-precision decimal numbers from the decimal module
    from decimal import Decimal; Decimal('10.5')

  • Fraction: Rational numbers from the fractions module
    from fractions import Fraction; Fraction(3, 4)

🔤 String

Strings are sequences of Unicode characters, enclosed in either single or double quotes.

language = 'Python'
framework = "Django"

Strings are immutable and support a variety of operations such as slicing, formatting, and concatenation.

List

Lists are mutable, ordered collections that can hold items of any type.

my_list = [1, [2, "three"], 4.5]
range_list = list(range(10)) # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

You can modify a list by adding, removing, or changing its elements.

Tuple

Tuples are similar to lists, but immutable (they cannot be changed after creation).

my_tuple = (1, 'spam', 4, 'U')
chars = tuple('spam')

You can also create named tuples for more readable, structured data.

Dictionary

Dictionaries are mutable, unordered collections of key-value pairs.

food = {'food': 'burger', 'taste': 'yum'}
hours = dict(hours=10)

They’re great for storing related data and support quick lookups by key.

Set

Sets are unordered collections of unique elements.

char_set = set('abc')
another_set = {'a', 'b', 'c'}

Sets support standard mathematical set operations like union, intersection, and difference.

Boolean

Booleans represent one of two values:

is_active = True
is_admin = False

They are often used in conditions and logical operations.

None

None is a special constant in Python that represents the absence of a value.

result = None

It's often used as a default placeholder for variables that are yet to be assigned.

File Objects

While file isn’t a formal data type, it’s an important mechanism you’ll use frequently.

f = open('random.txt')        # Reading a text file
f = open(r'C:\ham.bin', 'wb') # Writing a binary file

Python provides a built-in way to open, read, and write to files using file objects.

Functions, Modules, Classes

These are user-defined or built-in objects:

  • Functions: Defined using the def keyword.

  • Modules: Collections of functions and variables in separate files.

  • Classes: Used to create user-defined data types (objects) with attributes and methods.

Advanced Data Types

As you progress in Python, you’ll encounter advanced object types, such as:

  • Decorators: Modify the behavior of functions or methods.

  • Generators: Functions that yield items one at a time (lazy evaluation).

  • Iterators: Objects that implement the iterator protocol (__iter__, __next__).

  • MetaProgramming: Writing code that manipulates code (e.g., using metaclasses).

Conclusion

This was just a high-level overview of Python’s object types and data types. These building blocks are essential for every Python developer. In the upcoming blogs, we’ll dive deeper into each category, learn how to work with them, and understand how Python internally handles them.