Notebook
Notebook
Lecture Notebook
Joseph Connolly
December 10, 2024
1
Introductory Programming 2
Contents
1 Introduction to Programming 3
6 Introduction to NumPy 8
8 Introduction to Matplotlib 10
9 Introduction to SciPy 11
10 Classes in Python 12
Introductory Programming 3
1 Introduction to Programming
• Programming involves creating a set of instructions that tell a computer how to
perform a task.
print ( g r e e t ( ” A l i c e ” ) )
• Sorting algorithms:
• Dictionary comprehension:
s q u a r e d d i c t = {x : x ∗∗2 for x in range ( 5 ) }
print ( s q u a r e d d i c t )
• Set comprehension:
u n i q u e s q u a r e s = {x ∗∗2 for x in range ( 1 0 ) }
print ( u n i q u e s q u a r e s )
Introductory Programming 8
6 Introduction to NumPy
• NumPy is a fundamental package for scientific computing in Python, offering sup-
port for large, multi-dimensional arrays and matrices, as well as a variety of math-
ematical functions to operate on them.
where:
• Implementation in Python:
import numpy as np
np . random . s e e d ( 4 2 ) # For r e p r o d u c i b i l i t y
# Parameters
P t m i n u s 1 = 100 # Current p r i c e
mu = 0 . 0 5 # Expected return
sigma = 0 . 2 # Volatility
Z = np . random . normal ( 0 , 1 ) # Standard normal random v a r i a b l e
# Option p r i c e c a l c u l a t i o n
p = P t m i n u s 1 ∗ ( 1 + mu + Z ∗ sigma )
print ( ” Option P r i c e : ” , p )
Introductory Programming 10
8 Introduction to Matplotlib
• Matplotlib is a powerful library for creating static, interactive, and animated visu-
alizations in Python.
x = [1 , 2 , 3 , 4 , 5]
y = [2 , 3 , 5 , 7 , 11]
p l t . p l o t ( x , y , l a b e l= ’ Line ’ )
p l t . x l a b e l ( ’X−a x i s ’ )
p l t . y l a b e l ( ’Y−a x i s ’ )
p l t . t i t l e ( ’ Simple P l o t ’ )
plt . legend ()
p l t . show ( )
• Customizing plots:
9 Introduction to SciPy
• SciPy is a Python library that builds on NumPy and provides additional function-
ality for scientific and technical computing.
def f u n c ( x ) :
return x ∗∗2 + 3∗ x + 2
r e s u l t = minimize ( func , 0 ) # S t a r t i n g p o i n t a t 0
print ( ”Minimum a t x =” , r e s u l t . x )
Introductory Programming 12
10 Classes in Python
• Classes in Python are used to create user-defined data structures, allowing the
bundling of data and methods.
def bark ( s e l f ) :
return f ”{ s e l f . name} s a y s Woof ! ”