# Introduction to Python

## By John Chang [GitHub](https://eve-ning.github.io/)

## Math to Python Translation

It's easier to understand python by looking at it as math

Here are some **math-to-python** translations

### Translating Math Operations

### Example 1.1

\begin{align}
x = 40;
y = 3x * 4
\end{align}
\begin{align}
y = {?}
\end{align}



```python
x = 40
y = 3 * x * 4
```

*How do you know if the code worked? You have to `print` them*


```python
print(x)
print(y)
```

    40
    480


You cannot print something that doesn't exist yet, it will "throw" a `NameError: name is not defined` Error


```python
# print(a)
```

### Example 1.2

\begin{align}
x = 50, y = 100
\end{align}
\begin{align}
z = {?}
\end{align}



```python
x = 50
y = 100
z = x / y

print(z)
```

    0.5


### Example 1.3

A plant branch, on average 15 leaves, the plant has 12 branches. Approximate the total amount of leaves


```python
leaves = 15
branches = 12
print(leaves * branches)
```

    180


### Tutorial 1.1

Find the value of Voltage in the following equation with Python

\begin{align}
Current = 14.6;
Resistance = 39.0 * 10 ^{-3}
\end{align}
\begin{align}
Voltage = Current * Resistance
\end{align}



```python
# print(voltage)
```

### Tutorial 1.2

Find the value z of the following equation with Python

\begin{align}
x = 15;  
y = 5;
\end{align}
\begin{align}
z = \frac{10y^2+10}{5xy+15} * \frac{3}{2}
\end{align}


```python
# print(z)
```

### Translating Math Functions

Sometimes, you would want to use the same complex function multiple times. So you define a function for it

\begin{align}
GST(Base) = Base * 0.07 
\end{align}
\begin{align}
GST(100) = 7
\end{align}
\begin{align}
GST(300) = 21
\end{align}

\begin{align}
sin(degrees) = {...}
\end{align}
\begin{align}
sin(90) = 1
\end{align}
\begin{align}
sin(180) = 0
\end{align}

It's rather rare in math to define your own functions, however, it's unavoidable for programming

### Example 2.1

\begin{align}
f(x) = 5 x ^ {3}
\end{align}
\begin{align}
f(10) = {?}
\end{align}
\begin{align}
\frac{f(5)}{f(2)} = {?}
\end{align}


```python
def f(x):
    return 5 * x * x * x

print(f(10))
print(f(5)/f(2))
```

    5000
    15.625


\begin{align}
f(f(2)) = {?}
\end{align}


```python
print(f(f(2)))
```

    320000


#### Pre-built functions

In math, there are functions such as **ln**, **sin**, etc. Only sometimes, base Python will have the capability to execute the functions on demand


```python
# sin(40)
```


```python
# ln(1.4)
```

`NameError: name is not defined` means it just doesn't exist yet!

You don't have to define these functions, just **google**

Google 1st Result: [`python sin`](https://docs.python.org/2/library/math.html)

When you see a result that is **name**.*function*, it means it's in a package that you need to `import`


```python
import math
math.cos(math.pi)
```




    -1.0



### Example 2.2

\begin{align}
sin(cos(\pi^{9})) = {?}
\end{align}


```python
import math
math.sin(math.cos(pow(math.pi, 9)))
```




    -0.0971323539481088



`pow` is an in-built function, `math.pow` also works.

### Tutorial 2.1

\begin{align}
sin2(x,y) = sin(x) * sin(y)
\end{align}
\begin{align}
cos2(x,y) = cos(x) * cos(y)
\end{align}
\begin{align}
sin2(3\pi,3) * cos2(2\pi,2) = {?}
\end{align}


```python
import math
def sin2(x,y):
    return
def cos2(x,y):
    return
print()
```

    


### Tutorial 2.2

\begin{align}
ln(-sin2(e^\pi,1)) = {?}
\end{align}


```python
# print()
```

We will be mainly using these few packages for *Data Preparation*

- pandas
- numpy

# Answers

# Tutorials

\begin{align}
Current = 14.6;
Resistance = 39.0 * 10 ^{-3}
\end{align}
\begin{align}
Voltage = Current * Resistance
\end{align}



```python
14.6 * 39.0 * 0.001  
```




    0.5694



\begin{align}
x = 15;  
y = 5;
\end{align}
\begin{align}
z = \frac{10y^2+10}{5xy+15} * \frac{3}{2}
\end{align}


```python
x = 15
y = 5
z = (10 * y * y + 10) / (5 * x * y + 15) * 3 / 2
print(z)
```

    1.0


\begin{align}
sin2(x,y) = sin(x) * sin(y)
\end{align}
\begin{align}
cos2(x,y) = cos(x) * cos(y)
\end{align}
\begin{align}
sin2(3\pi,3) * cos2(2\pi,2) = {?}
\end{align}


```python
import math
def sin2(x, y):
    return math.sin(x) * math.sin(y)
def cos2(x, y):
    return math.cos(x) * math.cos(y)
sin2(3 * math.pi, 3) * cos2(2 * math.pi, 2)
```




    -2.1575819320582253e-17



\begin{align}
ln(-sin2(e^\pi,1)) = {?}
\end{align}


```python
print(math.log(-math.sin(math.exp(math.pi)*1)))
```

    -0.0914821575035909



```python

```

f40e0834a336111023fa37f496e74f27e1d02584
ipynb
Jupyter Notebook
src/Intro To Python.ipynb
BowinJH/MLWeek-D1
8e86584dc45f1cefd00f258bf4f3d3be6fedfc10
MIT
2021-01-16T05:10:34.000Z
2021-01-16T05:10:34.000Z
src/Intro To Python.ipynb
Samarth26/MLWeek-D1
abfdff677052bfed9e95ce1688fb3566b861883a
MIT
src/Intro To Python.ipynb
Samarth26/MLWeek-D1
abfdff677052bfed9e95ce1688fb3566b861883a
MIT
2020-05-09T09:16:02.000Z
2020-10-02T06:54:11.000Z
Qwen/Qwen-72B
1. YES
2. YES