02 - Python Fundamentals
02 - Python Fundamentals
Python
Python 101 for Economists
José Moreno
Data Lead
1
2. Python Fundamentals
2
2. Python
Fundamentals
Installation and Environments
3
Python Installation
Via Installer
● Download the installer ● Download the installer ● Most distros come with
● Open the installer and ● Open the installer and Python
follow the instructions follow the instructions
● If not, use package manager.
● PATH (select the default ● Verify: python --version
E.g., Ubuntu: sudo apt-get
option) install python3
Via Brew
● Verify: python --version
● Install brew
● Run: brew Install Python
● Verify: python --version
4
Development Editors (for lectures)
VS Code IDX
https://code.visualstudio.com/download https://idx.google.com/
5
Python Environments
6
Poetry: Modern Dependency Management Intro
7
2. Python
Fundamentals
Basic syntax: variables, data
types and operators
8
Variables in Python
GDP = 1000000000000
GROSS_DOMESTIC_PRODUCT= 1000000000000
UNEMPLOYMENT_RATE = 5.2
● Variables are containers for storing data values COUNTRY_NAME = "United States"
IS_DEVELOPED_COUNTRY= True
● Python is dynamically typed (no need to declare
def print_variables(
variable type, but we will use type hints!) gross_domestic_product
: int,
unemployment_rate: float,
● Naming conventions: country_name: str,
is_developed_country
: bool
○ Start with a letter or underscore ) -> None:
print(gross_domestic_product
)
○ Can contain letters, numbers, underscores print(unemployment_rate)
print(country_name)
○ Case-sensitive, use UPPER case for code print(is_developed_country
)
9
Data Types: Numbers
1. Integers (int)
● Whole numbers, positive or negative
Example: YEAR = 2024
2. Floating-point numbers (float)
● Numbers with decimal points
Example: INFLATION_RATE = 2.5
3. Complex numbers
● Numbers with real and imaginary parts
Example: Z = 3 + 2j
10
Data Types: Strings
● Sequences of characters
● Enclosed in single ('') or double ("") quotes
Examples:
COUNTRY = "France"
CURRENCY = 'Euro'
MESSAGE = "The economy is growing"
11
Data Types: Booleans
Examples:
IS_DEVELOPED_COUNTRY = True
IS_IN_RECESSION = False
12
Data Types: None
Example:
EMPTY_VALUE = None
13
Operators: Arithmetic
● Addition: +
● Subtraction: -
● Multiplication: *
● Division: /
● Floor division: //
● Modulus: %
● Exponentiation: **
14
Operators: Comparison
● Equal to: ==
● Not equal to: !=
● Greater than: >
● Less than: <
● Greater than or equal to: >=
● Less than or equal to: <=
15
Operators: Logical
16
Apply what we have learned (15 min)
First pseudo code in paper, then upload python code to your folder (e.g. u138478), name variables_future_value.py
● Check if the investment has doubled and print the result as a boolean value.
● Has the investment doubled AND is the future value greater than $2500? Print the result.
● Has the investment doubled OR is the future value greater than $2100? Print the result.
18
What are Python Type Hints?
They were introduced in Python 3.5 (PEP 484).They do not affect runtime behavior
(they are "hints", not enforced rules).
19
Why are Type Hints Important?
● Code Clarity: Makes intentions clear to other developers (and future you!)
● Error Prevention: Catches type-related errors early in development
● Better IDE Support: Enables better autocomplete and refactoring tools
● Documentation: Serves as inline documentation for expected types
● Easier Refactoring: Makes large-scale code changes safer
● Improved Static Analysis: Allows for more thorough code checks without
running the program
20
How to use them on VSCode?
{
"python.analysis.typeCheckingMode": "strict",
"python.analysis.diagnosticSeverityOverrides": {
"reportUnknownMemberType": false
}
}
21
2. Python
Fundamentals
Data structures: lists, tuples,
sets and dictionaries
22
Data Structures Intro
Python data structures are essentially containers for different kinds of data.
23
Lists: Definition and Properties
24
Lists: Common Operations
● Access: fruits[1]
● Append: fruits.append("orange")
● Remove: fruits.remove("banana")
● Length: len(fruits)
● Slice: fruits[1:3]
25
Tuples: Definition and Properties
26
Tuples: Common Operations
● Access: coordinates[1]
● Concatenation: new_tuple = coordinates + (30, 40)
● Length: len(coordinates)
● Tuples are immutable, so no append/remove.
27
Dictionaries: Definition and Properties
28
Dictionaries: Common Operations
● Access: person["age"]
● Add: person["profession"] = "Economist"
● Modify: person["age"] = 36
● Remove: del person["city"]
● Keys and values: person.keys(), person.values(),
person.items()
29
Sets: Definition and Properties
30
Sets: Common Operations
● Add: unique_fruits.add("orange")
● Remove: unique_fruits.remove("banana")
● Union: set1.union(set2)
● Intersection: set1.intersection(set2)
● Difference: set1.difference(set2)
● Is subset: set1.issubset(set2)
31
Sets vs Lists vs Tuples vs Dictionaries
Use case Dynamic data Fixed Data Key-value pairs Unique elements
32