Introduction to Python

MScAS 2025 - Python Fundamentals

Ilia Azizi

2025-09-23

Why Python for Data Science?

Python has become the leading language for data science because of:

  • Readability: Code reads almost like English
  • Rich ecosystem: Powerful libraries for every data science need
  • Versatility: From simple calculations to complex ML models
  • Industry adoption: Widely used in both academia and finance/insurance

Python in Actuarial Work

Risk modeling, pricing algorithms, mortality analysis, regulatory reporting, portfolio optimization, and automated reserving calculations.

Python Fundamentals

Comments: Notes for yourself and others (everything after # is ignored)

Whitespace: Python uses indentation to group code

Data Types Overview

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)

Numbers in Python

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

Strings: Working with Text

Key Point: Spaces count as characters too!

Important

Python uses 0-based indexing. First character is position 0!

Lists: Ordered Collections

Important

Slicing uses [start:end] where the end position is not included.

Dictionaries: Key-Value Storage

  • Keys = Like “labels” on folders (must be unique)
  • Values = Documents inside those folders (can be anything)

Tip

Use .get() to safely access keys that might not exist.

Control Flow: Making Decisions

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

Loops: Repeating Tasks

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: Reusable Code

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.

Variable Assignment

Important difference with some programming languages like R: Python uses references, not copies.

Classes

Classes, as part of Object-Oriented Programming, and are blueprints for creating objects with shared structure but unique data.

Key Takeaways

  1. Python’s simplicity makes it ideal for actuaries transitioning to programming

  2. Rich data types (lists, dictionaries) naturally represent actuarial data

  3. Control flow implements business rules and decision logic

  4. Functions allow you to create reusable calculations

  5. 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.

Questions?