0% found this document useful (0 votes)
10 views12 pages

Notebook

The document is a lecture notebook for an Introductory Programming course taught by Dr. Antony Jackson, covering fundamental programming concepts using Python. Key topics include variables, loops, algorithms, data structures, and libraries such as NumPy, Matplotlib, and SciPy. It also introduces Monte Carlo simulation for option pricing and the use of classes in Python.

Uploaded by

Iceblue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Notebook

The document is a lecture notebook for an Introductory Programming course taught by Dr. Antony Jackson, covering fundamental programming concepts using Python. Key topics include variables, loops, algorithms, data structures, and libraries such as NumPy, Matplotlib, and SciPy. It also introduces Monte Carlo simulation for option pricing and the use of classes in Python.

Uploaded by

Iceblue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Introductory Programming

Lecture Notebook
Joseph Connolly
December 10, 2024

Course Code: MTHT4004A


Course Name: Introductory Programming
Lecturer: Dr. Antony Jackson
Date: December 10, 2024

1
Introductory Programming 2

Contents
1 Introduction to Programming 3

2 Variables, Decisions, and Loops 4

3 Lists and Functions 5

4 Algorithms and Sorting 6

5 Collections and Comprehensions 7

6 Introduction to NumPy 8

7 Monte Carlo Simulation & Option Pricing 9

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.

• Python is a high-level, interpreted programming language that is easy to read and


write.

• Basic structure of a Python program:


print ( ” H e l l o , world ! ” )

• Key features of Python:

– Simple and readable syntax.


– Versatile and supports multiple programming paradigms (procedural, object-
oriented, functional).
– Large standard library for various tasks.
Introductory Programming 4

2 Variables, Decisions, and Loops


• Variables are used to store data values. Example:
x = 10
name = ” A l i c e ”

• Conditional statements help make decisions in programs.


if x > 5:
print ( ”x i s g r e a t e r than 5” )
e l i f x == 5 :
print ( ”x i s 5” )
else :
print ( ”x i s l e s s than 5” )

• Loops are used to repeat a block of code multiple times.

– For loop example:


for i in range ( 5 ) :
print ( i )

– While loop example:


count = 0
while count < 5 :
print ( count )
count += 1
Introductory Programming 5

3 Lists and Functions


• Lists are used to store multiple items in a single variable.
my list = [1 , 2 , 3 , 4 , 5]

• Accessing list elements:


print ( m y l i s t [ 0 ] ) # Accesses the f i r s t element

• Functions allow you to group code into reusable blocks.


def g r e e t ( name ) :
return f ” H e l l o , {name } ! ”

print ( g r e e t ( ” A l i c e ” ) )

• Example of a function with a loop:


def p r i n t l i s t e l e m e n t s ( l s t ) :
for item in l s t :
print ( item )

print list elements ( my list )


Introductory Programming 6

4 Algorithms and Sorting


• An algorithm is a step-by-step procedure for solving a problem.

• Example of a simple algorithm to find the maximum value in a list:


def find max ( l s t ) :
max value = l s t [ 0 ]
for num in l s t :
i f num > max value :
max value = num
return max value

• Sorting algorithms:

– Built-in sorting with Python:


my list = [3 , 1 , 4 , 1 , 5 , 9 , 2]
m y l i s t . s o r t ( ) # S o r t s in−p l a c e
print ( m y l i s t )

– Bubble Sort algorithm:


def b u b b l e s o r t ( l s t ) :
n = len ( l s t )
for i in range ( n ) :
for j in range ( 0 , n−i −1):
i f l s t [ j ] > l s t [ j +1]:
l s t [ j ] , l s t [ j +1] = l s t [ j +1] , l s t [ j ]
return l s t
Introductory Programming 7

5 Collections and Comprehensions


• Python collections include lists, tuples, sets, and dictionaries.

• Tuples are immutable sequences:


my tuple = ( 1 , 2 , 3 , 4 )

• Sets are unordered collections of unique elements:


my set = { 1 , 2 , 3 , 4}

• Dictionaries are collections of key-value pairs:


my dict = { ’ name ’ : ’ A l i c e ’ , ’ age ’ : 25}
print ( my dict [ ’ name ’ ] ) # A c c e s s e s t h e v a l u e a s s o c i a t e d w i t h t h e key ’ n

• List comprehensions provide a concise way to create lists:


s q u a r e s = [ x ∗∗2 for x in range ( 1 0 ) ]
print ( s q u a r e s )

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

• The core data structure is the numpy.ndarray (N-dimensional array).

• Example of array creation:


import numpy as np
a r r a y = np . a r r a y ( [ 1 , 2 , 3 , 4 ] )

• Common functions include:

– np.zeros((shape)): Creates an array filled with zeros.


– np.ones((shape)): Creates an array filled with ones.
– np.arange(start, stop, step): Creates an array with regularly spaced val-
ues.
– np.reshape(array, newshape): Reshapes an array to a new shape.
Introductory Programming 9

7 Monte Carlo Simulation & Option Pricing


• Monte Carlo simulation is a technique used for estimating numerical results by
simulating random samples. It is particularly used for pricing options by modeling
the random path of an underlying asset’s price.

• The formula for option pricing can be simplified as:

p = Pt−1 (1 + µ + Zσ) (1)

where:

– p is the option price.


– Pt−1 is the asset’s price at time t − 1.
– µ is the expected return of the asset.
– Z is a standard normal random variable.
– σ is the asset’s volatility.

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

• The most commonly used function for plotting is plt.plot().

• Example of creating a plot:


import m a t p l o t l i b . p y p l o t as p l t

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:

– plt.title(): Sets the title of the plot.


– plt.xlabel(): Sets the label for the x-axis.
– plt.ylabel(): Sets the label for the y-axis.
– plt.grid(): Adds a grid to the plot for better visualization.
Introductory Programming 11

9 Introduction to SciPy
• SciPy is a Python library that builds on NumPy and provides additional function-
ality for scientific and technical computing.

• Commonly used modules include:

– scipy.integrate: Integration and solving differential equations.


– scipy.optimize: Optimization and root-finding.
– scipy.stats: Statistical functions and distributions.

• Example of using scipy.optimize to find the minimum of a function:


from s c i p y . o p t i m i z e import minimize

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.

• Example of a simple class:


c l a s s Dog :
def i n i t ( s e l f , name , breed ) :
s e l f . name = name
s e l f . breed = breed

def bark ( s e l f ) :
return f ”{ s e l f . name} s a y s Woof ! ”

my dog = Dog ( ”Buddy” , ” Golden R e t r i e v e r ” )


print ( my dog . bark ( ) )

You might also like