0% found this document useful (0 votes)
437 views30 pages

Python For Excel Users

Uploaded by

Aniela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
437 views30 pages

Python For Excel Users

Uploaded by

Aniela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

PYTHON FOR EXCEL USERS

Know Excel?You Can Learn Python

by Tracy Stephens

no starch press®

San Francisco
PYTHON FOR EXCEL USERS. Copyright © 2025 by Tracy Stephens.

All rights reserved. No part of this work may be reproduced or transmitted in any
form or by any means, electronic or mechanical, including photocopying, recording, or
by any information storage or retrieval system, without the prior written permission of
the copyright owner and the publisher.

First printing

29 28 27 26 25 12345

ISBN-13: 978-1-7185-0398-4 (print)


ISBN-13: 978-1-7185-0399-1 (ebook)

Published by No Starch Press®, Inc.


245 8th Street, San Francisco, CA 94103
phone: +1.415.863.9900
www.nostarch.com; [email protected]

Publisher: William Pollock


Managing Editor: ...
Start where you are. Use what you have. Do what you can.

—Arthur Ashe
About the Author
Tracy Stephens is a quantitative developer based in New York City. Her
experience includes building systematic trading strategies at some of the
world’s top financial institutions. A long-time Python evangelist, she
focuses on designing quantitative infrastructure that’s flexible,
explainable, and efficient—when she can successfully keep her one-eyed
tuxedo cat off her keyboard.
About the Technical Reviewer
Chris Lam is a software engineer based in Las Vegas with over 20 years
of engineering experience in various industries such as fintech,
healthtech, and gaming. He specializes in distributed systems, serverless
architecture, and streaming data. He holds a bachelor’s degree in
computer science from the University of California, Los Angeles.
BRIEF CONTENTS

Introduction

PART I: GETTING STARTED WITH PYTHON

Chapter 1: Setting Up Your Coding Environment


Chapter 2: Coding Fundamentals Explained Through Excel
Chapter 3: Automating Tasks with Python Scripts
Chapter 4: Tracking Changes with Version Control

PART II: WORKING WITH DATA

Chapter 5: Data Analysis Made Interactive


Chapter 6: Analyzing and Transforming Data
Chapter 7: Working with Databases
Chapter 8: Retrieving Data from the Internet
Chapter 9: Creating Charts and Visuals
Chapter 10: Building Interactive Reports

PART III: WRITING BETTER CODE

Chapter 11: Organizing Your Code with Classes


Chapter 12: Finding and Fixing Errors
Chapter 13: Three Good Coding Habits

Afterword
Index
CONTENTS IN DETAIL

INTRODUCTION
Who This Book Is For
How Spreadsheets Can Hold You Back
Detecting Errors
Documenting Your Work
Collaborating with Others
Integrating Modern AI Tools
The Benefits of Using Python vs. Excel
Speed
Modularity
Automation
Interoperability
Security
Alternatives to Python
VBA
Python in Excel
What’s in This Book

PART I
GETTING STARTED WITH PYTHON

1
SETTING UP YOUR CODING ENVIRONMENT
Why a Well-Configured Installation Matters
Using the Command Line
Navigating Directories and Subdirectories
Manipulating Files
Customizing and Storing System Settings
Working Within Corporate IT Policies
Installing Python
Running the Installer
Verifying the Installation
Running Some Code
Getting Started with Python Libraries
Tools ...
INTRODUCTION

When I started my first job in finance, Microsoft Excel was revered as a


productivity tool, as it is in many other industries. I worked with many
incredibly smart, highly skilled people who had spent decades-long
careers performing high-stakes and highly complex work in Excel.
Having spent hundreds or perhaps even thousands of hours wrangling
data in Excel, these seasoned professionals knew virtually all there was
to know about the software. They were familiar with all of Excel’s
quirks, and they knew how to stretch Excel to its limits of size and
complexity.
At some point, however, I started to notice that many of these
brilliant, ...
PART IGETTING STARTED WITH PYTHON
1SETTING UP YOUR CODING
ENVIRONMENT

When you start working with Python, it’s important to correctly


configure your coding environment. In this chapter, we’ll make sure the
basic tools you need are installed in the right places and, more
importantly, that you know where those places are. We’ll walk through
installing Python and Visual Studio Code (VS Code), a popular code
editor. We’ll also cover the basics of interacting with your computer via
a command line interface, an essential skill for troubleshooting your
setup if something goes wrong.

Why a Well-Configured Installation Matters


If you don’t take the time to correctly configure your ...
2CODING FUNDAMENTALS EXPLAINED
THROUGH EXCEL

In this chapter, we’ll explore several fundamental Python concepts by


explaining how they relate to Excel. We’ll start with the basics of
writing and formatting code, and then we’ll cover the most common
data types you’ll find in Python, including numbers, strings, dictionaries,
lists, and dates. We’ll begin working with operators and functions and
start using conditionals to make decisions in your code.
Learning to code can be intimidating. You need to understand many
unfamiliar concepts, and this can be hard for people starting from
scratch because they don’t have an existing mental framework for ...
3AUTOMATING TASKS WITH PYTHON
SCRIPTS

In this chapter, we’ll explore how to write Python scripts, a first step to
automation and an essential skill to free your work from the confines of
Excel. We’ll also cover two related programming concepts to help you
write code that’s both useful and reusable: performing iteration with
loops and creating user-defined functions. With the basics covered in
this chapter, you’ll be able to build simple Python scripts from scratch,
without needing to rely on a visual interface to assist you with
developing logic.

What Are Scripts?


In Python, scripts are files with the .py extension that contain code for
the ...
4TRACKING CHANGES WITH VERSION
CONTROL

In this chapter, we’ll explore version control, a way of automatically


tracking and managing changes to code files over time. We’ll focus on
the most popular tool for version control, Git: what it is, why it exists,
and what it can do for you. We’ll detail how to structure your Git
workspaces, known as repositories, and then go over step-by-step
instructions for getting started. You’ll learn basic skills like commits,
merges, and working with local and remote repositories. By the
chapter’s conclusion, you’ll be equipped with the knowledge and
confidence you need to use Git to keep track of files and collaborate ...
PART IIWORKING WITH DATA
5DATA ANALYSIS MADE INTERACTIVE

This chapter covers Jupyter Notebooks, an interactive alternative to


scripts for writing and running Python code. We’ll discuss the basics of
working with notebooks and will consider when it makes sense to use
them rather than scripts. In the end, you’ll learn to use Jupyter
Notebooks to improve the process of writing more exploratory and
analytical code.
Python scripts work great as a canvas for writing and running code,
but many times it helps to work out your ideas in an interactive
environment. It can often be helpful to see immediate feedback from
your code, see how it reacts to changing specific variables, ...
6ANALYZING AND TRANSFORMING DATA

In this chapter, we’ll dive into pandas, a library that simplifies data
analysis in Python. We’ll focus on working with the tabular data
structure in pandas called a DataFrame, which you’ll learn to create,
manipulate, and transform. You’ll see how to conduct the same kinds of
analyses you’re used to doing in Excel by using Python, and you’ll learn
a few more advanced techniques that Excel makes unnecessarily
difficult. With a Jupyter Notebook as your canvas and pandas as your
medium, you’ll find that Python makes the art of data analysis accurate,
efficient, and user-friendly.
Many times in Excel, you’re forced ...
7WORKING WITH DATABASES

In this chapter, we’ll look at how to use databases to store, organize, and
extract data. Compared to using Excel for the same tasks, databases can
make your workflow more efficient and reduce the chance of errors.
First, we’ll cover databases generally and why they’re helpful. Next,
we’ll create an example database and interact with it using Structured
Query Language (SQL), the primary programming language for
working with databases. Finally, we’ll integrate SQL commands into
Python scripts.
Our emphasis in this chapter is on working with database
infrastructure in a hands-on, useful way. SQL itself is pretty intuitive ...
8RETRIEVING DATA FROM THE
INTERNET

The chapter focuses on APIs, which allow you to write scripts to


retrieve data from external sources across the internet. We’ll cover the
basics of making API requests and processing the responses, and we’ll
consider some of the complexities that come up when working with
APIs, like pagination, error handling, and authentication. In the end,
we’ll show how to write code for making API requests in clean,
organized, and maintainable functions that you can use and reuse in
whatever capacity you may need.
In Excel, your work is siloed; it lives only within the Excel
application and the Microsoft applications that ...
9CREATING CHARTS AND VISUALS

This chapter covers charting in Python. We’ll examine the situations


where creating charts with Python code is superior to creating them in
Excel. Then we’ll explore how to work with Plotly, a powerful and
relatively simple charting library. We’ll start with basic line and bar
charts, then move on to more complex, multidimensional charts with
custom styling, reusable themes, and interactive elements.
No matter the goal of your data analysis—spotting trends, seeing
relationships, making decisions, or communicating ideas—visualizations
are one of the most powerful tools you have. Creating these
visualizations more ...
10BUILDING INTERACTIVE REPORTS

In this chapter, we’ll explore turning your charts into interactive reports
with a Python library called Dash. First, we’ll outline the basics of Dash
and where it fits in among other tools for creating interactive reports,
including Microsoft Power BI. Then we’ll cover the basics of two
programming concepts necessary to create applications with Dash:
HTML and CSS. Finally, we’ll put this all together in an application
that allows a user to customize the data behind one of the Plotly charts
we created in the preceding chapter.
By the end of this chapter, you should understand how to use the
Dash framework to build ...
PART IIIWRITING BETTER CODE
11ORGANIZING YOUR CODE WITH
CLASSES

This chapter introduces the basics of object-oriented programming (OOP),


a style of coding that revolves around creating and manipulating custom
data types called classes. We’ll start with a description of classes and how
to create them. Then we’ll look at some of the benefits of OOP through
a series of both theoretical and practical examples. By the end of the
chapter, you should be able to recognize when using classes in your code
will improve both your scripts and your coding workflow more broadly.
Much of this book has been about applying structure to your Python
workflow so that the tools do most of ...
12FINDING AND FIXING ERRORS

This chapter focuses on making sure your code works as expected and
knowing what to do when it doesn’t. We’ll walk through how to use the
debugger in VS Code through a simple example. This includes setting
breakpoints and using the debugger commands to step into and out of
functions in order to isolate the source of an issue. After that, we’ll cover
a few basic examples of creating tests to make sure your code is resilient
regardless of the conditions where you might run it.

The Art of Debugging


Debugging means to identify and resolve errors or issues in your code.
It’s one of the most essential skills in coding ...
13THREE GOOD CODING HABITS

In this chapter, we’ll cover three good habits that can improve the
quality of your code. We’ll focus on best practices that champion
progress over perfection. In other words, these guidelines are more than
just theoretically nice to have; they bring real value to every project you
aim to tackle with Python, particularly the kinds of projects that evolve
out of spreadsheets.
We’ll also explore how Excel can encourage less-than-ideal practices
in each of these areas, so as you shift more of your work to Python,
you’ll be better equipped to recognize and avoid them. These three
habits for effective coding will allow ...
AFTERWORD

A common paradox, first articulated by Socrates in ancient Greece and


revived in 1999 by two psychologists at Cornell University, is familiar to
anyone who’s ever tried to master a new skill: The more you know, the
more you realize you don’t know. Just as you take your first steps toward
mastery, you also begin to understand exactly how much more there is
to learn. As a result, confidence can be elusive, even though you’ve come
a long way.
Coding is certainly one of those skills where this paradox applies.
The range of concepts you can learn is practically infinite, and as your
knowledge expands, so does your awareness of what’s out ...
INDEX
Note: Page numbers in italics refer to figures or tables

Symbols
> (angle bracket), 8–9
* (asterisk), 122–123, 154
@ (at sign), 226–227
\ (backslash), 6
: (colon), 119, 289
{} (curly brackets), 33
$ (dollar sign), 7
== (equality operator) , 35
= (equal sign), 29
/ (forward slash), 6, 122–123
> (greater than) operator, 149
>= (greater than or equal to) operator, 149
# (hash mark), 29
< (less than) operator, 149
<= (less than or equal to) operator, 149
!= (not equal to) operator, 149
< > (not equal to) operator, 149
- operator, 122
% (percent sign), 149
+ (plus sign), 122–123
; (semicolon), 141
~ (tilde), 7
""" (triple quotes), 29
_ (underscore), 149, 245

A
accessor property, 124
ACID (Atomicity, Consistency, Isolation, Durability), ...

You might also like