Lecture 1.1

Python and Big Data in Economics

Chapter 1 · Basics

Guoliang Ma
The Chow Institute, 2025

What you will learn

  • Very elementary Python theory
  • Some building blocks
  • Some functions
    • Memorize the names
    • Know the meaning of their parameters
    • Understand and interpret the output

If a program is a building, variables are the blocks, and syntax tells us how to put the blocks together to form walls. Different programs are different ways to put the walls together.

1. The boring ⚠ variables

From counting to abstraction

Programming puts mathematical ideas into practice. You have already encountered abstraction in mathematics.

In primary school, we counted 1, 2, 3, …. In middle school, we used x to represent a generic number.

So x = 1, x = 2, or x = 100 does not surprise us: a single letter x can be versatile.

From names to memory

In Python, x is a name. We want a variable to represent a value.

Python stores that value in an object in memory, then binds the name to the object. An existing object can also be reused.

See the original explanatory video →

1. Variables

Quick verification · names and object identity

# quick verification
a = 1
print(id(a))
b = a
print(id(b))

# not appearing as an address? format it
print(f"{id(b):02X}")

1. Variables

In the previous chunk of code, we saw:

  1. print — displays the supplied content.
  2. id — returns an object's identity. In CPython, this is its memory address.
  3. f-strings — format text using f"..." and {...}.

1. Variables

In-class exercise 1.1

  1. Create a name–value pair to store the name of a person: "Alice".
  2. Print Alice's name.
  3. Bind the variable to "Bob".
  4. Use an f-string to print the contents of name.

1.1 “Names refer to objects”

  • “Everything in Python is an object.”
  • For now, think of objects as chunks of memory with specific structures.
Original diagram: names a, b, and c point to separate objects in memory, each with its own structure.

Python naming conventions

1.1 “Names refer to objects”

What is special about an object?

In-class exercise 1.1.1

Can you summarize some features of an object?

Hint: We have learned that everything is an object. What do they have in common?

An object comprises …

1.1 “Names refer to objects”

  • An object's memory layout describes how its contents are arranged in memory.
  • This arrangement tells us how the chunk of memory stores the data.
Original memory-layout diagram: the name b refers to an object with an address, a type, and a value.
import sys
sys.getsizeof(a)

1.2 Objects’ types

Types are a large topic: Python's built-in types documentation is long. Let's start with a few examples.

a = 1
b = 2.0
c = 3.14 - 5j
cond = True

In-class exercise 1.2.1

Use the type function to find the type of each object.

1.2.1 Simple types — Boolean

The Boolean type is special in Python. Let's investigate.

We need a new function, isinstance, to determine whether an object is an instance of a type.

Start here

a = 1
isinstance(a, int)
isinstance(a, type(a))

Now, please try

cond = True
isinstance(cond, int)
print(cond + 1)

1.2.1 Simple types — Boolean

In-class exercise 1.2.1.1

  1. If True in Python is also 1, what about False?
  2. Try other types with isinstance. Do you find anything interesting?
  3. Search the web for the documentation of isinstance and read it.

1.2.2 Complex types

  • Our previous examples held individual values. In Python, we can also put several values together in a container.
  • In school mathematics, 1 is a number, 2 is a number, and {1, 2} is a set.
  • Python has sequences, mappings, strings, and other types that bring values together.

1.2.2 Sequences — list & tuple

Lists and tuples are frequently encountered. Here, we create them by enumerating their elements:

l = [1, 2, 3]
t = (1, 2, 3)

In-class exercise 1.2.2.1

  1. Try to print a list.
  2. Try to print one element of a list.
  3. Create a list containing 100 numbers, from 1 to 100.

1.2.2 Sequences — list & tuple

In-class exercise 1.2.2.2

  1. Try to print a tuple.
  2. Try to print one element of a tuple.
  3. Create a tuple containing 100 numbers, from 1 to 100.

1.2.2 Sequences — list & tuple

List functions and tuple functions

  • Lists and tuples have operations designed for their types.
  • A function accessed through a particular object is called a method.
  • Some methods change the object; others do not. Check each method's behavior.

Operations to investigate

  • append vs. extend
  • pop
  • The + operator

1.2.2 Sequences — list & tuple

You know how to get an element from a list or a tuple. Now let's try to set an element's value.

In-class exercise 1.2.2.3

  1. Use l and t that you just created.
  2. Set the second element of l to -1.
  3. Set the second element of t to -1.
  4. Analyze the changes in the memory layout.

1.2.2 Sequences — list & tuple

Similarities and comparisons · each column starts afresh. “OK” means a valid operation, not a True result.

Access & membership

l = [1, 2, 3]
t = (4, 5, 6)

print(l[1])  # OK
print(t[2])  # OK

l[1] = "list"  # OK
t[2] = "tuple" # TypeError

1 in l  # OK
2 in t  # OK; False

# Both are sequences.

Assignment & deletion

l = [1, 2, 3]
t = (4, 5, 6)

l[0] = 1  # OK
t[0] = 1  # TypeError

del l[1]  # OK
del t[1]  # TypeError

Methods

l = [1, 2, 3]
t = (4, 5, 6)

l.append(4)    # OK
l.reverse()    # OK
l.extend((5,)) # OK
l.pop(0)       # OK

# AttributeError:
t.append()
t.reverse()
t.extend()
t.pop()

In-place addition

l = [1, 2, 3]
t = (4, 5, 6)

l += [6, 7]
# Same id as before.

t += (8,)
# id changes.

Try the statements individually in your notebook: an intentional error stops a cell.

1.2.2.1 Slices

We can take one element from a list — or many elements, using a slice.

The notation is start:stop[:step].

Original slice grammar diagram: literal colons are marked green, chosen start/stop/step values red, and the optional step part blue.
  • Colons: must appear as written.
  • Start, stop, step: numbers you choose.
  • Square brackets in this grammar: mark an optional part. If included, it follows the same rules.

1.2.2.1 Slices

Slice objects

  • Create a slice object with slice(start, stop, step).
  • Use it in square brackets: l[s]. Colon notation such as l[1:10:2] also creates a slice.
s = slice(1, 10, 2)
l[s]

In-class exercise 1.2.2.4

  1. Is slice a function?
  2. Is l a function?

1.2.2.1 Slices

In-class exercise 1.2.2.5

Are these slices?

  1. 1:2:1
  2. 2:4:7
  3. 9:1:-1
  4. a:b:c
  5. 1.5:2.3:3.14
  6. a:2:3
  7. 6:7
  8. :-5:-1
  9. ::-1

1.2.2 Sequences — array & deque

These are also sequence types. We will not cover them in detail, but …

  • An array is list-like, but its elements are restricted to a type specified by a type code.
  • Arrays store basic values compactly. Performance depends on the operation; an array is not always faster than a list.
  • Import array before using it.
  • Explore the documentation for deque.

There are too many functions and methods to cover in class. You have learned the “how to.”

Review

  • Python names refer to objects.
  • Objects have an identity, a type, and a value.
  • Basic types include Boolean, sequences, and more.