<!-- 

author:  Michelle P. Kuchera
created: 18 Aug 2016
license: This code is released under the GNU GPL. Please feel free to use, modify, re-release at your will. You can not construct closed-source or proprietary software with this code. Please contact me if this is your desire.

-->


# Introduction to Jupyter Notebooks and Python

### Welcome to your (maybe) first **Jupyter notebook**!

In this class, we are learning to use the python language to solve problems in physics. So far, we have used an Integrated Development Enviroment (IDE) to write and execute python code. Today, we will use a Jupyter notebook to go through a brief overview of the python language. We will use both the IDE and the Jupyter notebooks to write python code this semester.

## Let's get started. 

From the Jupyter [documentation](http://jupyter.readthedocs.io/en/latest/index.html):

> First and foremost, the Jupyter Notebook is an interactive environment for writing and running code. The notebook is capable of running code in a wide range of languages. However, each notebook is associated with a single kernel.

In this class, we will use the Jupyter notebook to run Python code. You should therefore have this notebook opened in a Python kernel. Look at the top right of the browser window. If you see "Python 2" or "Python 3", this is a python notebook.

### Code cells allow you to enter and run code
You run code by holding `Shift` + `Enter`.

#### Try this with the python code below.



```python
a = 6
b = 2
c = a+b
print(c)
```

    8


#### Now, try to change a value of `a` or `b` above and re-execute the above cell.

### A *comment* is a non-code note within code. In python, a comment is preceded by a `#` for single-line comments.

Comments are useful as notes to yourself (the programmer), and/or notes to a user of your code. 

Below, the code from the first cell is reproduced with helpful comments.

####  Run this code again and note that the addition of the comments did not change the execution of the code


```python
# The following is simple python code, with comments, for adding two variables together
a = 6 # assigning the value of 6 to variable a
b = 2 # assigning the value 2 to the variable b
c = a+b # adding the variables a and b and storing the result in c
print(c) # printing the contents of c to the screen, right below the cell
```

    8


### A *function* is a block of code that can be called at any point after it has been evaluated


```python
def add(a,b): # a function is indicated by def. We named this function add. It takes two values as arguments
    c = a+b   # we add the two values that were passed into the function
    return c  # and return the result
```


```python
answer = add(6,2)    # We store the result of add into a new variable answer
print(answer)
answer = add(5,100)  # We overwrote the variable answer with the new value
print(answer)        
test = add('h','i')  # The + sign concatenates other data types, such as characters, rather that adding them
print(test)
```

    8
    105
    hi


**The python language itself does not contain all of the mathematics and visualization that we would be interested in for exploring physics.**

**To take advantage of the math and visualization that is useful to us in this course, we can to use python packages. These contain *modules*, which are files that define *objects* and *functions* that we will use this semester.** 

Let's start with `SymPy`, a package for symbolic (rather than numeric) calculations.
#### We must `import` the package of interest:


```python
from sympy.interactive import printing # for pretty printing of variables, etc
printing.init_printing(use_latex='mathjax') # specifying what type of pretty printing (this will be LaTeX)
# now import the package and define a namespace, sym, that will precede the function calls from the sympy function
import sympy as sym 
```

#### Now, let's use the package in a simple example


```python
x = sym.symbols("x") # defining x as a variable
sym.Integral(1/x,x) # Calling the Integral function within sympy, this defines our integral
```




$\displaystyle \int \frac{1}{x}\, dx$




```python
sym.integrate(1/x,x) # Calling the itegrate function within sympy, which symbolically integrates the function
```




$\displaystyle \log{\left(x \right)}$



**The three most commonly used python packages for scientific computing are `math` (for math functions), `numpy` (for arrays), and `matplotlib` (for plotting).
In fact, you will sometimes see numpy and matplotlib imported together as one package called `scipy`.**

### Below, I have written an example of receiving user input as a starting point.


```python
h = input("height in m: ")
print("the height is", h, "meters")
```

    height in m: 9
    the height is 9 meters


## Now, some final notes about the Jupyter notebooks:
#### Jupyter notebook files are designated by the *.ipynb* extension.
#### We can download notebooks in other formats, but they will not be executable.
#### The notebooks run *locally* (on your computer) in a web browser.
#### When the notebook application is launched, a local notebook server runs.
#### For each notebook you open, an iPython *kernel* starts running.
#### The kernel does *not* stop running when the browser tab is closed.
#### Make sure to shutdown each kernel that you begin, either from the notebook itself or the dashboard.

## I hope you had fun working through this Jupyter notebook! 

#### Please run everything as you would like me to see it, then go to File->Rename. 
#### Save this file as lastname1_lastname2_jupyter.ipynb (you do not have to enter the extension if you don't see it)

1fda68c489f3975a1216f8f4ea9d6523ab66d135
ipynb
Jupyter Notebook
Introduction_to_Jupyter_Notebooks_and_Python_Modified.ipynb
mbabiuc/computationalphysicsmu
facbcdbfd0a7f193755db8e35653b69b548d8a65
CC0-1.0
Introduction_to_Jupyter_Notebooks_and_Python_Modified.ipynb
mbabiuc/computationalphysicsmu
facbcdbfd0a7f193755db8e35653b69b548d8a65
CC0-1.0
Introduction_to_Jupyter_Notebooks_and_Python_Modified.ipynb
mbabiuc/computationalphysicsmu
facbcdbfd0a7f193755db8e35653b69b548d8a65
CC0-1.0
Qwen/Qwen-72B
1. YES
2. YES