
  
  
 # Python for Engineers
 # Introduction to Python Variables and Operators:<br/>Newton's Equations of Motion



## What is covered?
1. Definition of a variable in python
2. The assignment operator
3. Definition of a statement in python
3. A variable's scope
4. Kinds of number variables
5. String variables
6. Printing a variable's value
7. Python Arithmetic operators

## Equations Review
Assuming constant acceleration   


\begin{equation}
v_f = v_i + a t
\end{equation}

\begin{equation}
s = v_i + \frac{1}{2}at^2 
\end{equation}

\begin{equation}
v_f^2 = v_i^2 + 2as
\end{equation}

where:  
$v_f$: final velocity in $m/s$  
$v_i$: initial velocity in $m/s$  
$a$: acceleration in $m/s^2$  
$t$: time in seconds  
$s$: distance in meters

## <sup>1</sup> [Variables in Python and the Assignment Operator](https://www.tutorialspoint.com/python/python_variable_types.htm)
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory<sup>1</sup>.  
  
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables<sup>1</sup>.


```python
# This code creates a variable called vi_mps and assigns it the value of 3.2
# Assignment operator: =
vi_mps = 3.2
```


```python
# To see what variables are in scope use the built in function dir()
# We call the part of a program where a variable is accessible its scope
dir()
```

### What is a [Statement](https://docs.python.org/3/reference/simple_stmts.html) in python?
A python statement is a set of logical instructions that the interpreter can read and execute. The code in the above cell is an example of an __assignment statement__, it assigns (modifies) the value of the variable `vi_mps`.  
The other kind of statement in python is an __expression statement__ (or just expression) which can be defined as a set of logical operations on number or strings (or anything) to compute a value. 


```python
# to see the variable value use print()
print(vi_mps)

# we can also use a string variable to make the print statement more friendly
vi_string = 'This is the initial velocity in m/s: '
print(vi_string, vi_mps)
```


```python
# vi_mps is a float number, we can also define an integer or a complex number in python
# here is an integer
fourier_series_terms = 5
print('the number of terms in the series is: ', fourier_series_terms)

# here is a complex number
root_1 = 4.82j
print('the root is: ', root_1)
```

## [Python Arithmetic operators](https://www.tutorialspoint.com/python/arithmetic_operators_example.htm)
__Example 1)__ Assume a vehicle is has a constant acceleration of 2.3 $m/s^2$, calculate its final speed after 4 seconds if it starts from an initial speed of 10 $m/s^2$


```python
vi_mps = 10
a_mpss = 2.3
t_sec = 4

# addition operator: +
# multiplication operator: *
# the left hand side of the following assignment statement is an expression
vf_mps = vi_mps + a_mpss * t_sec

print('The final velcocity of the vehicle: ', vf_mps)
```

__Example 2)__ Calculate the distance traveled by the vehicle in example (1).


```python
# exponent operator: **
s_m = vi_mps + 0.5 * a_mpss * t_sec ** 2
print('The distance traveled by the vehicle is: ', s_m)
```

__Example 3)__ A runner travels a distance of 50 meters at an acceleration of 1 $m/s^2$ what was their initial speed if they had a speed of $12 m/s$ ?


```python
runner_a_mpss = 1
runner_s_m = 50
runner_vf_mps = 12

# subtraction operator: -
runner_vi_mps = (runner_vf_mps ** 2 - 2.0 * runner_a_mpss * runner_s_m) ** 0.5
print("The runner's intial speed is: ", vi_mps)
```

a3842d17128ad095f5ac1d5bafa26feca773f0f5
ipynb
Jupyter Notebook
notebooks/Intro-to-Variables-and-Operators.ipynb
endlesseng/python-for-engineers
4c4ec4303f474e8013fcefbbe361d1ed9dc9ea36
MIT
notebooks/Intro-to-Variables-and-Operators.ipynb
endlesseng/python-for-engineers
4c4ec4303f474e8013fcefbbe361d1ed9dc9ea36
MIT
notebooks/Intro-to-Variables-and-Operators.ipynb
endlesseng/python-for-engineers
4c4ec4303f474e8013fcefbbe361d1ed9dc9ea36
MIT
2021-11-06T17:49:45.000Z
2021-11-06T17:49:45.000Z
Qwen/Qwen-72B
1. YES
2. YES