Data Analysis with Python & SQL
An Interactive Introduction for Beginners
Preface
Welcome to this interactive textbook on Python and SQL for data analysis. This book is based on lecture notes from several courses I have been teaching at BI Norwegian Business School, such as Data Management and Python Programming and Python for Data Analysis, introducing master’s students to basic skills needed for business analytics.
Many of my students have asked me for years to publish the lecture notes and Jupyter notebooks used in these courses, so I have finally decided to convert them to this interactive textbook and make it available to everybody. I hope aspiring data analysts and scientists will find them useful when learning Python, SQL, and the basics of data analysis.
Modern society produces vast amounts of data continuously. Every minute, we generate enormous volumes of information, which companies need to collect, store, process, query, analyze, and visualize systematically, with the goal of extracting knowledge, making informed decisions, and driving innovation.
This book is aimed at beginners and intermediate users who want to bridge gaps between different areas of data analysis. No prior programming experience is required: we cover programming from zero, starting with what a program is and how to print a single line of text. We will introduce Python and SQL to get you started with automated data processing. At the same time, it is important to note that this book only scratches the surface. (These courses run for one semester, which limits how deep we can go.) The goal is to get you working with real data quickly; once you are comfortable with the material here, reference-depth treatments such as Wes McKinney’s Python for Data Analysis are a natural next step – see also the What is next? chapter at the end of the book.
Why Python and SQL? There are many reasons. Python is easy to learn and works well for teaching. According to Kaggle’s Data Science and Machine Learning Survey (2022), Python and SQL are the two most common programming skills for data scientists.
This book is interactive, so you can, and should, execute every piece of code you see, try the exercises, and experiment with modifying the code to see what happens. Everything runs in your browser—no installation is needed. One unique and helpful feature of this book is the explain function, available in some code cells, especially in the early chapters, which visualizes how the code executes step by step. If you are new to programming, spend some time going step by step through the code to understand how different parts work.
Note that any changes you make to the code are not permanent. Closing the page, reloading it, or moving to the next page will erase your changes, so if you want to keep them, copy your code and exercise solutions to another location.
A note on AI assistants
AI assistants such as ChatGPT, Claude, and GitHub Copilot are now part of every programmer’s toolbox, and you will almost certainly use them during and after your studies. They are excellent at explaining unfamiliar code, deciphering cryptic error messages, suggesting fixes, or showing alternative solutions – used this way, they can genuinely accelerate your learning.
But be careful about letting them do the exercises for you. The exercises in this book exist to build skills in your head, not to produce correct answers on a page. Programming, like mathematics or a foreign language, is learned by doing; if an AI does the doing, the learning does not happen. And the skills matter even in the age of AI tooling: you cannot assess, correct, or direct AI-generated code in real-world settings if you have not first learned to write and read code yourself.
A practical rule of thumb: it is always fine to ask an AI to explain something, but try every exercise yourself first – and if you do use AI to get unstuck, make sure that afterwards you could solve a similar exercise without it.
