MScAS 2025 - Python Fundamentals
2025-09-23
Python has become the leading language for data science because of:
Python in Actuarial Work
Risk modeling, pricing algorithms, mortality analysis, regulatory reporting, portfolio optimization, and automated reserving calculations.
Comments: Notes for yourself and others (everything after #
is ignored)
Whitespace: Python uses indentation to group code
Object Type | Description | Example | Mutable? | Use Case |
---|---|---|---|---|
Numbers | Numerical data | 42 , 3.14 |
No | Calculations |
Strings | Text data | 'Auto' , "Life" |
No | Names, IDs |
Lists | Ordered collections | [1000, 1200, 1350] |
Yes | Premiums over time |
Dictionaries | Key-value pairs | {'name': 'John', 'premium': 1200} |
Yes | Policy records |
Tuples | Fixed sequences | (25000, 50000, 100000) |
No | Coverage limits |
Booleans | True/False | True , False |
No | Conditions |
Mutable = Changeable (like a draft document)
Immutable = Unchangeable (like a printed document)
Operator | Name | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 5 / 2 |
2.5 |
// |
Floor Division | 5 // 2 |
2 |
% |
Modulus | 5 % 2 |
1 |
** |
Exponentiation | 5 ** 2 |
25 |
Key Point: Spaces count as characters too!
Important
Python uses 0-based indexing. First character is position 0!
Important
Slicing uses [start:end]
where the end position is not included.
Tip
Use .get()
to safely access keys that might not exist.
Conditions are checked in order that they are written. First true condition runs, rest are ignored
Operator | Description | Example | Result |
---|---|---|---|
and |
Both must be true | True and False |
False |
or |
At least one true | True or False |
True |
not |
Inverts truth value | not True |
False |
== |
Equal to | 5 == 5 |
True |
!= |
Not equal | 5 != 3 |
True |
> , < |
Comparisons | 5 > 3 |
True |
>= , <= |
Greater/less or equal | 5 >= 5 |
True |
Use for loops when you know how many items to process.
Use while loops when you want to continue until a condition changes.
Caution
Be careful of infinite loops - make sure the condition eventually becomes false!
Functions have three parts: (i) Name, how you call it, (ii) Parameters, the inputs you provide, (iii) Return value, the output it gives back
Default parameters are used when you don’t provide a value.
try/except handles errors gracefully so your program doesn’t crash.
Important difference with some programming languages like R: Python uses references, not copies.
Classes, as part of Object-Oriented Programming, and are blueprints for creating objects with shared structure but unique data.
Python’s simplicity makes it ideal for actuaries transitioning to programming
Rich data types (lists, dictionaries) naturally represent actuarial data
Control flow implements business rules and decision logic
Functions allow you to create reusable calculations
Classes help model real-world entities like policies and claims
Have a look at the Python lecture notes for a detailed walkthrough of all these concepts.
Remember
The goal isn’t to memorize syntax, but to understand how Python’s building blocks can solve actuarial problems efficiently and clearly.
DSAS 2025 | HEC Lausanne