Skip to main content

Command Palette

Search for a command to run...

Python Basics: Data Types, Basic & Advanced Data Structures, and Collections

Updated
7 min read
Python Basics: Data Types, Basic & Advanced Data Structures, and Collections
P
Highly motivated Data Science and AI professional with a strong academic foundation in Physics, Chemistry, and Mathematics (B.Sc.). Completed a Data Science and AI Diploma from the reputed institute DataMites, gaining hands-on experience in machine learning, deep learning, and natural language processing (NLP). Successfully completed an Internship as a Data Science Intern at Rubixe AI Solutions, where I worked on real-world datasets, built predictive and analytical models, and contributed to businessdriven AI solutions. Passionate about applying data-driven techniques to solve complex problems and deliver impactful insights.

Introduction

Python provides a rich ecosystem for handling data — from simple numbers to complex datasets used in data science and AI. Understanding data types, basic and advanced data structures, and specialized structures like arrays, Series, and DataFrames is essential for writing efficient and scalable programs.

This guide covers:

  • Data Types in Python

  • Basic Data Structures

  • Advanced Data Structures

  • Arrays, Series, and DataFrames

  • Difference between Data Type & Data Structure

  • Python Collections


1. Data Types in Python

What is a Data Type?

  • A data type defines the kind of value a variable can hold and determines the operations that can be performed on it.

    Why Data Types Matter

    • Ensure correct operations on data

    • Optimize memory usage

    • Improve code readability and reliability

    Example

    age = 25        # Integer
    price = 99.99   # Float
    name = "Pranav" # String
    is_active = True # Boolean
    

Built-in Data Types in Python

1. Numeric Types

Type Description Example
int Whole numbers 10, -5
float Decimal numbers 3.14, -0.5
complex Complex numbers 2+3j
x = 10
y = 3.14
z = 2 + 3j

2. Sequence Types

Type Description Ordered Mutable
str Text data Yes No
list Ordered collection Yes Yes
tuple Immutable list Yes No
range Sequence of numbers Yes No

3. Boolean Type

is_logged_in = True

Represents True or False.


4. Set Types

Type Description
set Unordered, unique elements
frozenset Immutable set

5. Mapping Type

Type Description
dict Key-value pairs


2. Basic Data Structures in Python

A data structure is a way of organizing and storing data so it can be accessed and modified efficiently.

While data types define what kind of data, data structures define how data is organized.


2.1 List

Ordered, mutable collection.

items = [1, 2, 3]

Use Cases

  • Storing dynamic data

  • Iteration and indexing


2.2 Tuple

Ordered, immutable collection.

point = (10, 20)

Use Cases

  • Fixed data

  • Dictionary keys


2.3 Set

Unordered collection of unique items.

unique_numbers = {1, 2, 3}

Use Cases

  • Removing duplicates

  • Membership testing


2.4 Dictionary

Key-value storage and key must be unique.

student = {"name": "Pranav", "age": 21}

Use Cases

  • JSON data

  • Fast lookups


3. Additional Core Data Structures

These are not always emphasized but are essential.


3.1 Array (Using array Module)

An array stores elements of the same data type more efficiently than lists.

from array import array
arr = array('i', [1, 2, 3])

Advantages

  • Memory efficient

  • Faster numeric operations

Use Cases

  • Large numeric datasets

  • Performance-critical applications


3.2 NumPy Array (Scientific Computing)

Used extensively in data science.

import numpy as np
arr = np.array([1, 2, 3])

Features

  • Vectorized operations

  • Multi-dimensional arrays

  • High performance

Use Cases

  • Machine learning

  • Scientific computing


4. Data Structures for Data Science

Python’s data ecosystem includes powerful structures from libraries like pandas.


4.1 Series (Pandas)

A Series is a one-dimensional labeled array.

import pandas as pd
s = pd.Series([10, 20, 30])

Features

  • Index labels

  • Handles missing data

  • Vectorized operations

Use Cases

  • Time series data

  • Feature columns in ML


4.2 DataFrame (Pandas)

A DataFrame is a two-dimensional table-like structure.

df = pd.DataFrame({
    "Name": ["A", "B"],
    "Age": [20, 21]
})

Features

  • Rows & columns

  • Heterogeneous data

  • Powerful data manipulation

Use Cases

  • Data analysis

  • ETL pipelines

  • Machine learning datasets


4.3 Panel (Deprecated)

Previously used for 3D data in pandas, now replaced by multi-index DataFrames.


5. Advanced Data Structures


5.1 Stack

LIFO structure.

Applications: Undo systems, parsing.


5.2 Queue

FIFO structure.

Applications: Task scheduling, BFS.


5.3 Deque

Double-ended queue for fast operations on both ends.


5.4 Linked List

Efficient insertions/deletions.


5.5 Heap (Priority Queue)

Used for priority scheduling.


5.6 Tree

Hierarchical data structure.

Examples:

  • Binary Tree

  • Binary Search Tree

  • AVL Tree


5.7 Graph

Represents networks.

Applications:

  • Social networks

  • Route planning

  • Recommendation engines


6. Difference Between Data Type and Data Structure

Feature Data Type Data Structure
Meaning Type of value Organization of data
Example int, str list, tree
Purpose Define data Manage data
Complexity Simple Can be complex

7. Python Collections

Built-in Collections

Type Ordered Mutable Unique
List Yes Yes No
Tuple Yes No No
Set No Yes Yes
Dictionary Yes Yes Keys unique

Collections Module (Advanced)

  • Counter → Counting

  • defaultdict → Default values

  • OrderedDict → Ordered mapping

  • namedtuple → Structured tuples

  • deque → Efficient queues


8. Choosing the Right Structure

Scenario Best Structure
Numeric computing NumPy Array
Tabular data DataFrame
Single column data Series
Ordered dynamic data List
Unique items Set
Fast lookup Dictionary
Priority tasks Heap

Conclusion

Python offers a powerful range of data types and data structures — from simple lists to advanced structures like NumPy arrays, Series, and DataFrames used in data science.

Understanding these structures helps you:

  • Write efficient code

  • Handle large datasets

  • Build scalable applications

  • Prepare for data science & AI careers

A strong foundation in these concepts enables you to move confidently into advanced fields like machine learning, big data, and artificial intelligence.


Final Insight

The right data structure can transform a slow program into an efficient, scalable solution.