<jupyter_start><jupyter_text>Introduction to python for physicists in the mezzanine floor  notebook_0_introduction<NAME> This notebooks are created with the intention to give a practical introduction to python programming language for a scientist.   I going to give a very personal vision of it, specially ho i become a swiss knife tool for me as a experimental-physcisist.  In this 1st notebook I'll what is python for me and how I work with it. In particular:* Motivation for learning python* Basic structure and characteristics of the language* How to work with packages and eviroments* How to work  with a jupyter notebooksThe notebooks in this series are at https://github.com/Iherrbenza/python-for-physicists-in-the-mezzanine-floor-. Software needed for this intro    First of all, you want to install the python programming language. Also, I recommend JupyterLab as a convenient notebook interface for python programming. Depending on your taste and your system, you might want to download these individually or as part of a full distribution like Anaconda.  The full distro in https://www.anaconda.com/products/individual. Bottom of the page, the installation links for each system.Miniconda is a minimal istallation of python envioroment, you will need to install the libraries you need.https://docs.conda.io/en/latest/miniconda.htmlJupyter lab o jupyter notebookhttps://jupyter.org/install.htmlhttps://jupyter.readthedocs.io/en/latest/install.htmlAn alternative, completely online solution is the Colaboratory platform by Google.  https://colab.research.google.com/notebooks/intro.ipynb?utm_source=scs-indexrecent=trueThis is a web-based jupyter notebook interface to python and allows you to run your code for free on their GPU servers.A third alternative is deepnote. This is also a free online platform with a jupyter interface.https://deepnote.com/dashboard  How I work with python  🐍  The use of python in scienceIs it a way to learn just one tool to rule all?**Computation models*** sympy* numpy* matplolib* _etc_**Analysis data*** numpy* matplotlib* pandas* scipy* scikit-learn* _etc_**I/O instrumets for experimental setup*** pyserial* pyvisa* opencv* socket* _etc_  advantages* Easy programming, minimizing the time required to develop, debug and maintain the code.* Python has a strong position in scientific computing.  Large community of users, easy to find help and documentation.  * Set of well settled scientific libraries, as.    * numpy: Numerical Python    * scipy: Scientific Python    * matplotlib: graphics library    *  _and more_    * Python is a very healthy language.    Is in a rising phase so it's worth to invest time on learn it, not going to die soon.    1st language in the [Tiobe list](https://www.tiobe.com/tiobe-index/)    disadvantages* Not very focus, other programing languages will perform better on the specific tasks.  But you can create wrappers aroung libraries to make perform better.   Emmbeding C libraries (numpy, camera programs), automation of web pages.* Not easy as Labview to create GUI.   But more flexible.  * It's becoming too much popular.    Watch the hackers!  😈  What is Python?Python is a general-purpose, object-oriented, high-level programming language.* **dynamically typed: no need to declare a variable** * **automatic memory management: no need to manage the memory stacks** * **interpreted: compliled in-time**   Sofware package and stack.The python sofware is built on a stack of packages, you can use the libraries already built. importing packages```pythonimport numpy as npnp.arange(10)import mathx = math.cos(2 * pi)from math import *x = cos(2 * pi)from math import cos, pix = cos(2 * pi)import myscripts.py```What code is execute if I import a file.At the en of the file;```pythondef my_function():    do what you want    return()if __name__ == "__main__":    my_function()````When I imported```pythonimport myscripts myscripts.my_function()```<jupyter_code># paths 

import sys
for p in sys.path: 
    print(p)
# to import files from othe directories you need to add
# the path to the system file

import sys, os
cwd = os.getcwd()
print(cwd)
sys.path.append(cwd + '\data')
from ANN_sketchy import *
cwd
do_graph()<jupyter_output><empty_output><jupyter_text>What is Anaconda? PyPI&pip vs Anaconda&conda  Whats pip? https://pypi.org/  **PyPI:** Python Package Index   **pip:** tool for installing Python packages from PyPIWhats Anaconda?  https://www.anaconda.com/products/individual<jupyter_code># versions 
!python --version
# list packages
!conda list
lp = !conda list
l = [print(i) for i in lp if 'matplotlib' in i] 
# dependencies
!conda search sympy=1.8 --info<jupyter_output><empty_output><jupyter_text>usual conda commands Conda is the program in the command line interface (CLI) for manage packages and envioromentsinstall a package;  ```conda install package_name=3.5.6```  update a conda;  ```conda update conda```  update package;  ```conda update numpy```  update all;  ```conda update all```more in the cheat sheet, [conda_sheet](https://docs.conda.io/projects/conda/en/4.6.0/_downloads/52a95608c49671267e40c689e0bc00ca/conda-cheatsheet.pdf)    conda enviromentsA way to solve dependecies is to create a new enviroment, new python installation (only the core libraries)create an enviroment  ```conda create --name py35 python=3.5```  A enviorment py35 has been created with a python 3.5 on it  ```conda env list```  List the envioroment you have  ```conda activate py35```  change to the new envioroment  ```conda install package_name=3.9.0```  this will install the packge in the activated enviroment    python virtual enviromentHas a different architecture, you create a folder where you run a protected python enviroment, python and its packages will be download in this folder.The Dy labscript has this mechanic, so it's safe to update or install new pakges out of this folder/virtualenviroment.  https://blog.teclado.com/python-virtual-environments-complete-guide/  Documentation, where to get infoEach package can have it's own webpage with documentation. This can be well done, easy language, full explanations, tutorials, examples etc.Or could be very short, complicated language, or non-existent documentation.Examples: * python https://www.python.org/* numpy https://numpy.org/* pandas https://pandas.pydata.org/docs/index.html* matplotlib https://matplotlib.org/stable/index.html* scipy https://www.scipy.org/* scikit-learn https://scikit-learn.org/stable/Stackoverflow is another resource to look for direct solutions to questions, very practical.https://stackoverflow.com/Or just google it!   IDEs and InterpretersIDEs (integrated development environment)To edit .py I use/used:* Atom: very nice interface, full of plugins, have an atom as a brand 😃⚛️, can be annoying to maintain.  * Sublime: very lightweight, nice interface, just a txt editor.* Visual Studio Code: nice interface (has improved), full of features (but this can be a problem), widly used. It's from microsoft, the evil. 🤑* IDLE: comes with just python very simple  🤷‍♂️ * spyder: matlab interface for python, plenty of things. very heavy (always give me problems), could be misleading and hide the potentialities of python.* _etc___Terminals__, that terrible thing..store your code in a .py file and execute it from the terminal.direct execution, interpreters:```python``````IPython```   GUIsWindows style:* Easy GUI building pysimplegui: https://pysimplegui.readthedocs.io/en/latest/   * Qt  * pygame   * tkinter* etcWeb apps:  * Voila: https://voila.readthedocs.io/en/stable/using.htmlhow-does-voila-work  * Holoviz: https://holoviz.org/  * Dash-Ploty* etc  Jupyter notebookI found myself using Jupyter notebooks for editing python code  The best thing for me: __try and error small pieces of code.__    Other good points of Jupyter lab are:* inline visualization of graphs and tables* work-diary style    More info about jupyter project [info conda](https://jupyterlab.readthedocs.io/en/stable/user/interface.html)  The cheatsheet to use jupyter lab from the terminal [cheat sheet  conda](https://www.anaconda.com/wp-content/uploads/2019/03/11-2018-JupyterLab-Notebook-Cheatsheet.pdf)  to open, the terminal, a scary thingsome people have problems to use the terminal, don't be afraid.1. open terminal (cmd, powershell, windows terminal, anaconda prompt)2. navigate to de place where you want to work (```cd whatsoever```)3. activate the enviroment you want to use ```conda activate your_env```4. call ```jupyter lab```  kernelsJupyter stand for Julia, Python, and R. You can run this 3 different languages on it  shortcutsThe shortcuts that I use in a regular basis ```shift + enter``` run cell  ``` enter``` ```esc``` editing or command While in command mode (```esc```):* ```up``` ```down``` navigate cells*  ```a``` ```b``` create cell up or down*  ```x``` ```c``` ```v``` cut, copy and paste cell  *  ```m``` ```y``` change to markdown or code*  ```control + shift + (up or down)``` move cell (custom)*  ```control + shift + -``` split cell*  ```shift + m``` merge cells While in editing mode (```enter```):* ```tab``` indent* ```control + ([])``` dedent indent selected lines* ```control + /``` comment uncomment lines* ```shift + tab``` show docstring  markdownIts a simple language to give extra formating to the text, useful to enrich your notebook. Need to declare the cell as a markdown cell, top menu or ```m``` in comand mode.To go back to code press ```y```  headersAlong with this notebook, I already set some markdown cells with headers to structure the document.  lists* This is a bullet list* This is a bullet list* This is a bullet list1. And you can also create ordered lists2. by using numbers3. and listing new items in the lists 4. on their own lines  block quotes> this a block quote  > here as well> And also can be nested>> like this>>> even more  reference links[chile]: http://en.wikipedia.org/wiki/Chile "Wikipedia Article About Chile" This is [a reference][chile]  tables|Header|Header|Header|Header||------|------|------|------||Cell  |Cell  |Cell  | Cell ||Cell  |Cell  |Cell  | Cell ||Cell  |Cell  |Cell  | Cell ||Cell  |Cell  |Cell  | Cell |  style and emphasis*Italics*  _Italics_ **Bold** __Bold__  codeThe word ```monospace``` will appear in a code-like form.```pythondef function(n):    return n + 1```  LaTeX math$z=\dfrac{2x}{3y}$  $F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$$$2x+3y=z$$uses matjax server, if you don't have connection could not run  images![][beta_dec][beta_dec]:https://upload.wikimedia.org/wikipedia/commons/a/aa/Beta-minus_Decay.svg "showing beta-minus decay."  videos<jupyter_code>from IPython.display import YouTubeVideo
YouTubeVideo("l0Zc7jhEcLg", 800, 500)<jupyter_output><empty_output><jupyter_text>HTLMDanger: This alert box indicates a dangerous or potentially negative action.More at: [HTLM](data\useful-html-for-jupyter-notebook.ipynb)  magic commands and more from IPython enhacementhttps://ipython.readthedocs.io/en/stable/interactive/magics.html<jupyter_code># list of commands

%lsmagic
# timing funtions

n = 100000
%time sum(range(n))
%timeit sum(range(n))
# check history of commands

%history -n 4-6
# listing variables in the kernel

%who
%who_ls int
# getting help in IPython

?         # help /on IPython 
%quickref # cheatsheet of IPython
len?
# source code

from scipy import ndimage

ndimage.measurements.center_of_mass??<jupyter_output><empty_output><jupyter_text>and you can try ```Shift + Tab``` with the mouse cursor over a function.<jupyter_code># with '!' in front of the command you can run terminal commands

!dir<jupyter_output><empty_output><jupyter_text>Tab completionTab completion is a useful way to explore the function of any object you’re dealing with. Simply type object_name. to view the object’s attributes. Tab completion also works on file and directory names.<jupyter_code>from scipy import ndimage

ndimage.measurements.c<jupyter_output><empty_output><jupyter_text>input, output, history IPythonWe can access different input or results of a cell by a tag or by history.<jupyter_code>10 + 10
# print last result
_
print('last output:', _)
print('next one   :', __)
print('and next   :', ___)
# equivalent
# print('last input:', _i)
# print('next one  :', _ii)
# print('and next  :', _iii)
Out[15] # looking to the cell 34 (remember that depends on the execution order)<jupyter_output><empty_output><jupyter_text>inputs and widgets<jupyter_code># input

enjoy = input('How much you enjoying this tutorial? ')

print('enjoy is:', enjoy)
# if jupyter widgets is install you can use more types of inputs 

import ipywidgets as wdg
slider = wdg.IntSlider()
display(slider)
date_pick = wdg.DatePicker(
    description='Pick a Date',
    disabled=False
)
display(date_pick)<jupyter_output><empty_output><jupyter_text>display results inline<jupyter_code># print a graphs 

import matplotlib.pyplot as plt

X = range(10)
Y = [x**x for x in X ]
# %matplotlib inline
plt.plot(X, Y)
# print a table

import pandas as pd

df = pd.read_csv('data\\2021-09-24_Dy_Rack2_12A.csv')
display(df)
# Animated display

from IPython.display import display, clear_output
import numpy as np
import time
import matplotlib.pyplot as plt
from scipy.special import jn

x = np.linspace(0,5)
f, ax = plt.subplots()
ax.set_title("Bessel functions")

for n in range(1,10):
    time.sleep(1)
    ax.plot(x, jn(x,n))
    clear_output(wait=True)
    display(f)

plt.close()<jupyter_output><empty_output><jupyter_text>emojis<jupyter_code>import emoji
print(emoji.emojize('Phd is very easy!!! :thumbs_up:'))
print("But you can copy and past as well")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1008508141411178, 'fraction_numerical': 0.008948217691066452, 'mean_word_length': 4.844406343763395, 'pattern_counts': {'":': 1, '<': 42, '<?xml version=': 0, '>': 50, 'https://': 26, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '304404', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 4241, 'n_tokens_neox': 4066, 'n_words': 1632, 'path': '0_Python_in_the_mezzanine.ipynb', 'repo_name': 'Iherrbenza/python-for-physicists-in-the-mezzanine-floor-', 'size': '13650'}