CompSci 101: Types, control, and numpy arrays#
This is where we start with some computer science basics. Essentially, where every coding class begins. We’ll discuss data types and data structures, such as the differences between integers and floats, booleans (i.e. True or False), a bit about strings (text), and lists.
We’ll then get into more Python-specific material and introduce the crucial numpy package. This package will allow us to use arrays in Python. Arrays are a way to store things, like numbers, in Python.
Numpy arrays allow us to do a lot of different math, like linear algebra, in Python and are much faster than the Python list data type.
I also mention functional programming (something we’ll come back to) and control structures, like if and else statements.
What you’ll learn#
By the end of this chapter, you should be able to:
Understand the basic data types in Python (integers, floats, booleans, strings)
Work with data structures like lists, dictionaries, and tuples
Write control structures using
if,elif,else,for, andwhileCreate and manipulate numpy arrays for numerical work
Understand indexing and slicing to access parts of your data
Use vectorization and broadcasting for efficient calculations
Using AI for this material#
This chapter covers foundational concepts that AI tools are particularly good at explaining. When you’re stuck:
Control structures confusing? Ask Claude: “Walk me through this for loop step by step. What is the value of
iat each iteration?”Array shapes not making sense? Ask: “Why does my array have shape (3,) instead of (3,1)? What’s the difference?”
Broadcasting errors? Paste your error into Claude and ask: “Why can’t I multiply these two arrays together?”
AI is great at tracing through code line by line and showing you exactly what Python is doing at each step. Don’t be afraid to ask “dumb” questions — that’s how you learn.
Tip
When debugging loops and control structures, ask Claude to “trace through this code and show me the value of each variable at each step.” This is exactly how programmers debug — and AI can do it instantly.
Resources#
These notes follow along with Chapters 3 and 4 of Python for Finance, 2e.
You’ll also find this material in the Coding Basics section of Coding for Economists.