After diving into how Python executes code and the difference between mutable and immutable objects, the next logical step is understanding what exactly we are working with inside that memory.
In Python, we often hear the phrase "Everything is an object," but to really grasp the language, we need to look at the different types of containers we use to store data. Think of these as different types of boxes, each designed for a specific purpose.
The Core Object Types
Python provides a rich set of built-in data types. Here’s a high-level overview of the main players you’ll encounter:
1. Numbers
It's not just basic counting. Python handles a variety of numeric formats:
- Integers:
1, 2, 100 - Floating Points:
3.14, 2.0 - Complex Numbers:
3 + 4j(wherejrepresents the imaginary part) - Special types: Like
DecimalsandFractionsfor high-precision math.
2. Strings
Strings can be wrapped in single quotes '...' or double quotes "...".
Example:
name = "Abhinav"
Python treats strings almost like a list of characters. You can access the first letter using name[0]. However, remember: strings are immutable. You can't change a single character in place; you have to create a new string.
3. Lists
A list is a continuous memory space where you can store multiple items. They are zero-indexed and highly flexible.
Example:
my_list = [123, "Abhinav", 3.14]
Lists are mutable, meaning you can change, add, or remove items without creating an entirely new list object.
4. Tuples
Tuples look a lot like lists but use parentheses () instead of square brackets [].
Example:
my_tuple = (1, 2, 3)
The big difference? Tuples are immutable. Once you create them, you can't change their contents. This makes them safer for data that shouldn't be accidentally modified.
5. Dictionaries (Dict)
In a list, you find things by their position (0, 1, 2). In a dictionary, you find things by a Key.
Example:
my_dict = {"name": "abhinav", "count": 2}
It’s a "Key-Value" pair system. You define the label (the key), and Python stores the data (the value).
Beyond the Basics
Python also includes more specialized types:
- Sets: These only store unique values (no duplicates allowed).
- Booleans: Simply
TrueorFalse. - None: Represents an empty state or a null value (useful when an API doesn't return data).
- Files: A mechanism to read from and write to external data.
Why the "Big Picture" Matters
As a developer, your job is often just moving data around fetching it from a database, looping through it, and displaying it.
If you don't know whether you're holding a List or a Dictionary, you won't know how to loop through it or extract the value you need. Mastering data types is the foundation. Once you are comfortable with these, things like loops, functions, and classes become much easier to navigate.
Quick Mental Model
- Numbers/Strings: The basic building blocks.
- Lists/Tuples: Ordered sequences of data.
- Dictionaries: Labeled data for quick lookups.
Written by
Abhinav Yadav