This notebook was put together by Jake Vanderplas for UW's Astro 599 course. Source and license info is on GitHub.
%run talktools.py
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.
**Interpreted** | No need for a compiling stage |
**Object-oriented** | Objects: complex data structures with attributes and methods |
**High-level** | Abstraction from the way the machine actually executes |
**Dynamic** | Variables can change meaning on-the-fly |
**Built-in** | Fewer external requirements |
**Data structures** | Ways of storing/manipulating data |
**Script/Glue** | Code that controls other programs |
**Typing** | The kind of variable (int, string, float) |
**Syntax** | Grammar which defines the language |
**Library** | reusable collection of code |
"...in December 1989, I was looking for a 'hobby' programming project that would keep me occupied during the week around Christmas. My office ... would be closed, but I had a home computer, and not much else on my hands. I decided to write an interpreter for the new scripting language I had been thinking about lately: a descendant of ABC that would appeal to Unix/C hackers. I chose Python as a working title for the project, being in a slightly irreverent mood (and a big fan of Monty Python's Flying Circus)."
from IPython.display import YouTubeVideo
YouTubeVideo("i9SSOWORzw4")
Open-sourced development from the start (currently BSD-licensed)
Large development community
Version 2.0 (2000), 2.6 (2008), 2.7 (2010)
Version 3.X (2008) is not backward compatible
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
N = 200
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r ** 2 * np.random.rand(N)
ax = plt.subplot(111, polar=True)
plt.scatter(theta, r, c=theta, s=area, alpha=0.75);
from IPython.display import YouTubeVideo
YouTubeVideo('tlontoyWX70', start=30)
!curl -O http://matplotlib.org/mpl_examples/pylab_examples/demo_agg_filter.py
%run demo_agg_filter.py
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
from IPython.display import HTML
HTML("""<video width='700' height='500' preload='none' controls poster='http://jakevdp.github.io/downloads/videos/double_pendulum_frame.png'><source src='http://jakevdp.github.io/downloads/videos/double_pendulum.mp4' type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'></video>""")
If you have never used the command-line, you're in for a treat
Mac OSX: in Finder/Applications, search for "Terminal"
Linux/Unix: Ctrl-Alt-t
Windows: run "cmd"
Type python
at the command-line to start the interpreter
Execute a command: Type print "hello world"
The best option is one which includes code highlighting
Use your editor to open hello_world.py
(here we use OSX's mate
)
Edit the file to say print "hello world"
In the terminal, run python hello_world.py
IPython provides an enhanced command-line interface
It can be started by typing ipython
:
Useful features include tab completion, help (?
), etc.
Basic use is just like the standard interpreter:
The IPython notebook can be started by typing ipython notebook
:
Your web browser should open to an interactive notebook page
Notice that this slideshow is written as an IPython notebook!
We've introduced Python and motivated its use in science
We've talked about four ways to use Python
Next up: basic training - getting started in Python