# Python Lecture 1: On Numbers, Variables, and Functions

### Lecture Notes by Jakov Ivan S. Dumbrique (jdumbrique@ateneo.edu)

MATH 100.2: Topics in Financial Mathematics II \
First Semester, S.Y. 2021-2022 \
Ateneo de Manila University

Today, we will learn four topics in Python:

0. How to put comments in your code
1. Numbers
2. Variables
3. Functions

## Section 0: Comments in Python

While your machine's Python compiler will skip comments when it encounters comments, writing comments in your code is still important in order to make your code readable by humans (and your future self).


```python
# Single line comments start with a number symbol.
"""
    Multiline strings can be written using three "s, and are often used as documentation.
"""
    
```

## Section 1: Numbers

Numbers are a primitive datatype in Python. Numbers can either be integers (int) or float.


```python
# You have numbers
3  # => 3

# Math operations are what you would expect
1 + 1   # => 2
8 - 1   # => 7
10 * 2  # => 20
35 / 5  # => 7.0

# Integer division rounds down for both positive and negative numbers.
5 // 3       # => 1
-5 // 3      # => -2
5.0 // 3.0   # => 1.0 # works on floats too
-5.0 // 3.0  # => -2.0

# The result of division is always a float
10.0 / 3  # => 3.3333333333333335

# Modulo operation
7 % 3   # => 1

# Exponentiation (x**y, x to the yth power)
2**3  # => 8

# Enforce precedence with parentheses
1 + 3 * 2    # => 7
(1 + 3) * 2  # => 8
```




    8




```python
# Comparison operators

# to check equality
1 == 1 # => True
1 == 1.0 # => True

# to check inequality
(3 - 2) != 0 # => True

# other comparison operators
(3 - 2) > 1 # => False
(3 - 2) >= 1 # => True
(3 ** 2) + (4 ** 2) < 5 ** 2 # => False
(3 ** 2) + (4 ** 2) <= 5 ** 2 # => True
```




    True



<h3 style='color:blue'>Definition: Simple Interest</h3>

Suppose that an amount $P$ is invested for $T$ years at an interest rate of $r$ per annum (p.a.). If the interest rate is a **simple rate**, then the terminal value of the investment is
\begin{align}
    A &= P + \underbrace{Pr + Pr + \cdots + Pr}_{T \text{ years}} \nonumber \\
    &= P(1 + \underbrace{r + \cdots + r}_{T \text{ years}}) \nonumber \\
    &= P(1+rT). \label{Simple Interest}
\end{align}

<h3 style='color:green'>Example 1</h3> 

Suppose that $\$408$ is deposited in a newly created savings account that pays a simple interest of $3.435\%$ per annum. If no deposits or withdrawals are made on the account since the aforementioned transaction, how much is withdrawn if the account is closed and emptied in $6$ months?

$A = \$408\left[1+ 3.435\% \left(\dfrac{6}{12}\right)\right] \approx \boxed{\$415.01}.$


```python
408*(1+0.03435*(6/12))
```




    415.00739999999996



## Section 2: Variables

Variables provide a way to associate names with Python objects. We use variables in order to reuse names instead of writing explicitly the values repeatedly. 

We use an equal sign (=) to assign a value to a variable.


```python
pi = 3.14159
```


```python
pi
```




    3.14159



Variable names
1. Can contain uppercase and lowercase letters, digits (but they cannot start with a digit), and the special character _
2. Are case-sensitive! (pi is different from Pi)
3. Cannot be [Python reserved words (or keywords)](https://www.w3schools.com/python/python_ref_keywords.asp)


<h3 style='color:green'>Example 2: Assign variables to Example 1</h3> 

Suppose that $\$408$ is deposited in a newly created savings account that pays a simple interest of $3.435\%$ per annum. If no deposits or withdrawals are made on the account since the aforementioned transaction, how much is withdrawn if the account is closed and emptied in $6$ months?


```python
P = 408
r = 0.03435
T = 6/12

P * (1 + r*T)
```




    415.00739999999996



## Section 3: Functions

1. if you will reuse a piece of code, you should write a function for it
2. functions are not run in a program until they are “called” or “invoked” in a program
3. characteristics of a function:
    - has a name
    - has parameters (0 or more)
    - has a docstring (optional but recommended)
    - has a body
    - returns something

<h3 style='color:green'>Example 3: Write a function for Example 2</h3> 

Suppose that $\$408$ is deposited in a newly created savings account that pays a simple interest of $3.435\%$ per annum. If no deposits or withdrawals are made on the account since the aforementioned transaction, how much is withdrawn if the account is closed and emptied in $6$ months?


```python
def get_future_value_simple(P, r, T):
    """
    Input: principal P, simple interest rate r p.a., investment time period T in years
    Returns the future value of a principal P invested for T years at a simple rate r
    """
    return P * (1 + r*T)
```


```python
get_future_value_simple(408, 0.03435, 0.5)
```




    415.00739999999996




```python
get_future_value_simple(P=408, r=0.03435, T=0.5)
```




    415.00739999999996




```python
P = 408
r = 0.03435
T = 6/12
get_future_value_simple(P, r, T)
```




    415.00739999999996



<span style='color:red'>Tip:</span>  use functions if you will reuse a chunck of code. Think of a function as a black box that hides tedious coding details.


<h3 style='color:green'>Example 4: Reusing Functions</h3> 

Suppose you invest $\$500$ in an account that pays a simple interest of $5.75\%$ p.a. How much will it be worth in $1.5$ years? $20$ years?


```python
get_future_value_simple(P=500, r=0.0575, T=1.5)
```




    543.125




```python
get_future_value_simple(P=500, r=0.0575, T=20)
```




    1075.0000000000002



<h3 style='color:blue'>Definition: Compound Interest</h3>

Suppose that an amount $P$ is invested for $T$ years at an interest rate of $r$ per annum (p.a.). If the interest rate is **compounded** $\boldsymbol{m}$ **times a year**, then the terminal value of the investment is
\begin{align}
    A = P\left(1+\dfrac{r}{m}\right)^{mT}. \label{Compounding Interest}
\end{align}

<h3 style='color:green'>Exercise</h3> 

Suppose you invest $\$500$ in an account that pays a compound interest of $5.75\%$ p.a. How much will it be worth in $1.5$ years if the interest rate is 
1. compounded annually?
2. compounded semiannually? 
3. compounded quarterly?
4. compounded monthly?

Write a function and reuse that function to answer the above question.


```python
def get_future_value_comp(P, r, m, T):
    """
    Input: principal P, interest rate r p.a. compounded m times a year, investment time period T in years
    Returns the future value of a principal P invested for T years at an 
    interest rate r p.a. compounded m times a year
    """
    return P * (1 + r/m)**(m*T)
```


```python
P = 500
r = 0.0575
T = 1.5
get_future_value_comp(P, r, 1, T)
```




    543.739105494308




```python
get_future_value_comp(P, r, 2, T)
```




    544.3767255859376




```python
get_future_value_comp(P, r, 4, T)
```




    544.7048313758173




```python
get_future_value_comp(P, r, 12, T)
```




    544.9271497450427




```python

```

971c8a6299de1d527359e13c1dd4cad88f5661b0
ipynb
Jupyter Notebook
lectures/0Python/20210831_NumbersVariablesFunctions.ipynb
ateneomathdept/math100.2_2021Sem1
ac51a30e25d57a50ef8aa25fcfc0db64da0641b9
MIT
2021-12-12T04:44:09.000Z
2021-12-12T04:44:09.000Z
lectures/0Python/20210831_NumbersVariablesFunctions.ipynb
ateneomathdept/math100.2_2021Sem1
ac51a30e25d57a50ef8aa25fcfc0db64da0641b9
MIT
lectures/0Python/20210831_NumbersVariablesFunctions.ipynb
ateneomathdept/math100.2_2021Sem1
ac51a30e25d57a50ef8aa25fcfc0db64da0641b9
MIT
2021-10-04T04:16:18.000Z
2021-10-04T04:16:18.000Z
Qwen/Qwen-72B
1. YES
2. YES