Python Programming for Data Analysis

A Comprehensive Guide from Basics to Machine Learning

Author

Asmui Rahim

Published

July 12, 2026

Preface

Welcome

Welcome to Python Programming for Data Analysis, a comprehensive guide designed to take you from the fundamentals of Python programming to practical applications in data analysis and machine learning.

This book is the result of teaching Python programming to students and professionals in data science. It has been refined through countless classroom interactions, student feedback, and real-world applications.

About This Book

Who Is This Book For?

This book is designed for:

  • Students taking introductory data science or programming courses
  • Researchers who want to learn Python for data analysis
  • Professionals transitioning into data science roles
  • Self-learners seeking a structured approach to learning Python

What You’ll Learn

By the end of this book, you will be able to:

  1. Understand Python fundamentals including variables, data types, and operators
  2. Control program flow using conditionals and loops
  3. Work with data structures including lists, tuples, dictionaries, and sets
  4. Write reusable code using functions and object-oriented programming
  5. Handle files and errors professionally
  6. Manipulate numerical data using NumPy
  7. Analyze data using pandas DataFrames
  8. Create visualizations with Matplotlib and Seaborn
  9. Build machine learning models with scikit-learn
  10. Apply statistical analysis including hypothesis testing

How to Use This Book

Each chapter builds upon the previous one, so we recommend reading them in order if you’re new to Python. However, if you have some programming experience, feel free to jump to the topics that interest you most.

Each chapter includes:

  • Clear explanations with real-world context
  • Executable code examples you can run immediately
  • Practice exercises with solutions
  • Summary tables for quick reference

Prerequisites

To get the most out of this book, you should:

  • Have access to a computer (Windows, Mac, or Linux)
  • Be willing to practice by typing and running code
  • Have basic familiarity with computers and file systems

No prior programming experience is required!


About Python for Data Analysis

Why Python?

Python is one of the most popular programming languages for data science and analysis. Here’s why millions of data scientists choose Python:

Easy to Learn
Python’s clean syntax makes it an excellent first programming language. Code reads almost like English.
Versatile
From web development to machine learning, Python can do it all. Learn once, apply everywhere.
Powerful Libraries
NumPy, pandas, Matplotlib, scikit-learn - Python has world-class libraries for every data task.
Active Community
With millions of users worldwide, help is always available. Stack Overflow, forums, and tutorials abound.
Industry Standard
Major companies like Google, Netflix, and NASA use Python. Learning it opens doors to exciting careers.

The Data Analysis Workflow

flowchart LR
    A["Collect Data"] --> B["Clean Data"]
    B --> C["Explore Data"]
    C --> D["Analyze"]
    D --> E["Visualize"]
    E --> F["Model"]
    F --> G["Communicate"]

This book covers each step of the data analysis workflow:

Step What You’ll Learn Chapters
Fundamentals Python basics, control flow Ch. 1-4
Data Structures Lists, dictionaries, comprehensions Ch. 5-6, 8
Functions Reusable code, OOP Ch. 7, 11
Data Wrangling NumPy, pandas Ch. 12-13
Visualization Matplotlib, Seaborn Ch. 14-15
Machine Learning Classification, regression Ch. 16, 18
Statistics Hypothesis testing Ch. 17

Course Information

This book supports data analysis and programming courses. The topics align with a comprehensive curriculum:

  1. Python Variables and Data Types
  2. Basic Input/Output
  3. Control Flow (if/else)
  4. Loops (for/while)
  5. Sequences (Strings, Lists, Tuples)
  6. Data Structures (Dictionaries, Sets)
  7. Functions
  8. List Comprehensions and Lambda Functions
  9. File Operations
  10. Exception Handling
  11. Object-Oriented Programming
  12. NumPy
  13. pandas
  14. Matplotlib
  15. Seaborn
  16. Machine Learning: Classification
  17. Inferential Statistics
  18. Machine Learning: Regression

Author’s Note

Teaching Python programming has been one of the most rewarding experiences of my career. Watching students progress from writing their first print("Hello, World!") to building machine learning models reminds me why I love this field.

This book represents years of refining how to explain complex concepts in accessible ways. I’ve learned as much from my students’ questions as they have learned from my lectures.

A few words of advice as you begin:

  1. Type the code yourself. Don’t just copy and paste. Typing helps you learn.

  2. Make mistakes. Errors are how we learn. Read error messages carefully—they often tell you exactly what went wrong.

  3. Experiment. Modify the examples. See what happens. Break things and fix them.

  4. Practice regularly. Programming is a skill that improves with consistent practice, not marathon sessions.

  5. Ask questions. Use online communities like Stack Overflow and Reddit when you get stuck.

I hope this book serves you well on your data science journey. The skills you learn here will open doors to exciting opportunities in research, industry, and beyond.

Happy coding!

Asmui Rahim Department of Statistics January 2025


Acknowledgments

This book would not be possible without:

  • The Python Software Foundation and the Python community
  • The developers of NumPy, pandas, Matplotlib, Seaborn, and scikit-learn
  • My students who asked thoughtful questions and helped refine these materials
  • The global data science community for their generous sharing of knowledge

Technical Details

This book was created using:

  • Quarto for document authoring and publishing
  • Python version 3.x
  • Jupyter for interactive code execution
  • VS Code/JupyterLab as development environments

The source code for this book is available on GitHub. Feel free to:

  • Report issues or errors
  • Suggest improvements
  • Contribute to the content

How to Cite This Book

If you use this book in your research or teaching, please cite it as:

Rahim, A. (2025). Python Programming for Data Analysis: A Comprehensive Guide from Basics to Machine Learning. Department of Statistics.


Getting Started

Ready to begin? Head to Chapter 1: Variables and Data Types to start your Python journey!