Intro to Ipython - Olmo Zavalaolmozavala.com/images/Courses/2016/Ipython.pdf · important tasks in...
Transcript of Intro to Ipython - Olmo Zavalaolmozavala.com/images/Courses/2016/Ipython.pdf · important tasks in...
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Intro to Ipython
Olmo S. Zavala Romero
Center of Atmospheric Sciences, UNAM
August 26, 2016
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Packages
“NumPy is the fundamental package for scientific computing with Python. Itcontains among other things: ”
A powerful N-dimensional array objectSophisticated (broadcasting) functionsTools for integrating C/C++ and Fortran codeUseful linear algebra, Fourier transform, and random number capabilities
http://www.numpy.org/
“Pandas provides high-performance, easy-to-use data structures and dataanalysis tools for the Python programming language. ”http://pandas.pydata.org/
“Matplotlib is a python 2D plotting library which produces publicationquality figures in a variety of hardcopy formats and interactive environmentsacross platforms.”http://matplotlib.org/
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
“The IPython project began in 2001 as Fernando Perez side project tomake a better interactive Python interpreter. In the subsequent 11 yearsit has grown into what’s widely considered one of the most importanttools in the modern scientific Python computing stack ”“. . . is designed from the ground up to maximize your productivity in bothinteractive computing and software developmen”
McKinney, Wes. Python for Data Analysis. Beijing: OReilly, 2013.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
IPython
IPython provides a rich architecture for interactive computing with:A powerful interactive shell.A kernel for Jupyter.Support for interactive data visualization and use of GUI toolkits.Flexible, embeddable interpreters to load into your own projects.Easy to use, high performance tools for parallel computing.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Run IPython
Linux (inside a terminal)ipython
windows (form the anaconda terminal)ipython
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Improvements in Ipython
Tab completion (functions, vars, methods, files, modules).? Before or after a variable will show some info.?? Shows source code of function.%run file Runs a file inside the terminal.Emacs shortcuts
and stores last outputs.iX and X stores specific input and output.
McKinney, Wes. Python for Data Analysis. Beijing: OReilly, 2013.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Shortcuts Ipython
Some shortcuts (similar to the terminal)
McKinney, Wes. Python for Data Analysis. Beijing: OReilly, 2013.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Magic commands
This commands are prefixed with %.To see a whole list of commands type %< tab >.%comm? for info about an specific command, for example %writefile?Please check %quickref or %magic
Some useful ones:
McKinney, Wes. Python for Data Analysis. Beijing: OReilly, 2013.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Do not sleep exercises
Make a factorial function without recursion myfact.
Make a factorial function with recursion myrecfact.Show the time to compute factorial of 1000 for the previous twofunctions. and the math.factorial function. Use %time yourfunc.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Do not sleep exercises
Make a factorial function without recursion myfact.Make a factorial function with recursion myrecfact.
Show the time to compute factorial of 1000 for the previous twofunctions. and the math.factorial function. Use %time yourfunc.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Do not sleep exercises
Make a factorial function without recursion myfact.Make a factorial function with recursion myrecfact.Show the time to compute factorial of 1000 for the previous twofunctions. and the math.factorial function. Use %time yourfunc.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
OS integration
Type !cmd to executes commands inside ipython.
McKinney, Wes. Python for Data Analysis. Beijing: OReilly, 2013.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
One more exercise
Assume there are two subfolders in the current folder and each subfolderhas a list of files. How can we list the file names that are in bothsubfolders.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
IPython notebook
“ Starting in 2011, the IPython team, led by Brian Granger, built a webtechnology based interactive computational document format that iscommonly known as the IPython Notebook. It has grown into a wonderfultool for interactive computing and an ideal medium for reproducible researchand teaching. Ive used it while writing most of the examples in the book; ”McKinney, Wes. Python for Data Analysis. Beijing: OReilly, 2013.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Run IPython notebook
Linux (inside a terminal)ipython notebook
windows (Click on the IPython icon)
Then open your browser at localhost:8888
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Ipython notebook Command mode shourtcuts
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Ipython notebook Edit mode shourtcuts
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
IPython
Text in cells:
Textfor italics
**for bold**# title level 1## title level 1* bullet pointEquations with Latex syntax →
√2 + x
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Matplotlib
“Making plots and static or interactive visualizations is one of the mostimportant tasks in data analysis.Matplotlib is a (primarily 2D) desktop plotting package designed for creatingpublication-quality plots. The project was started by John Hunter in 2002 toenable a MATLAB-like plotting interface in Python ” McKinney, Wes. Python for Data Analysis.
Beijing: OReilly, 2013.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Hello plot
Hello matplotlib inside python notebook%matplotlib inlineimport matplotlib.pyplot as pltx = [1,2,3,2,1]y = [2*i for i in x]plt.plot(x,y)plt.show()
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Matplotlib exercises
Make a list with values from 0 to 10 with intervals of .1.
Plot floor function from 0 to 10 with intervals of .1.Plot sine from 0 to 10 with intervals of .1.Plot sine from -2π to 2π with intervals of .01.Plot absolute value from -10 to 10.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Matplotlib exercises
Make a list with values from 0 to 10 with intervals of .1.Plot floor function from 0 to 10 with intervals of .1.
Plot sine from 0 to 10 with intervals of .1.Plot sine from -2π to 2π with intervals of .01.Plot absolute value from -10 to 10.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Matplotlib exercises
Make a list with values from 0 to 10 with intervals of .1.Plot floor function from 0 to 10 with intervals of .1.Plot sine from 0 to 10 with intervals of .1.
Plot sine from -2π to 2π with intervals of .01.Plot absolute value from -10 to 10.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Matplotlib exercises
Make a list with values from 0 to 10 with intervals of .1.Plot floor function from 0 to 10 with intervals of .1.Plot sine from 0 to 10 with intervals of .1.Plot sine from -2π to 2π with intervals of .01.
Plot absolute value from -10 to 10.
Python
Olmo S.ZavalaRomero
IPythonRunBasicsShortcutsMagiccommandsOSshortcutsNotebookShortcutsMatplotlib
Matplotlib exercises
Make a list with values from 0 to 10 with intervals of .1.Plot floor function from 0 to 10 with intervals of .1.Plot sine from 0 to 10 with intervals of .1.Plot sine from -2π to 2π with intervals of .01.Plot absolute value from -10 to 10.