Welcome to my lessons


Bo Zhang (NAOC, mailto:bozhang@nao.cas.cn) will have a few lessons on python.

  • These are very useful knowledge, skills and code styles when you use python to process astronomical data.
  • All materials can be found on my github page.
  • jupyter notebook (formerly named ipython notebook) is recommeded to use

These lectures are organized as below:

  1. install python
  2. basic syntax
  3. numerical computing
  4. scientific computing
  5. plotting
  6. astronomical data processing
  7. high performance computing
  8. version control

What is python?

  • 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.

Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn't catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python's introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.

reference: https://www.python.org/doc/essays/blurb/

comparison with some other programming languages: https://www.python.org/doc/essays/comparisons/

Advantages as scientific/astronomical computing language

  1. easy-to-use/understand
  2. extensions
  3. astronomical packages
  4. high performance computing
  5. open source

Websites you must know if you use python

How to install python?

type these in your terminal:

$ sudo apt-get install pip
$ sudo pip install python

pip can install almost all python packages. So I recommend you use this to install all packages.

An alternative is to use easy_install, which is a very similar tool.

type $ python to enter the python shell


In [1]:
print "Hello World"


Hello World

Recommended IDEs (Interactive Development Environments)


In [2]:
%magic

In [3]:
%%bash
ls -a


.
..
cham_teaches_python_01_install_python.html
cham_teaches_python_01_install_python.ipynb
cham_teaches_python_02_basic_syntax.html
cham_teaches_python_02_basic_syntax.ipynb
cham_teaches_python_03_numerical_computing.html
cham_teaches_python_03_numerical_computing.ipynb
cham_teaches_python_04_plotting.html
cham_teaches_python_04_plotting.ipynb
cham_teaches_python_04_scientific_computing_optimization.ipynb
cham_teaches_python_05_aplpy_healpy.html
cham_teaches_python_05_aplpy_healpy.ipynb
cham_teaches_python_05_astronomical_data_processing.html
cham_teaches_python_05_astronomical_data_processing.ipynb
cham_teaches_python_06_high_performance_computing.html
cham_teaches_python_06_high_performance_computing.ipynb
cham_teaches_python_07_version_control.html
cham_teaches_python_07_version_control.ipynb
data
exercise
.git
.gitignore
.ipynb_checkpoints
stuff01
test.pdf

In [ ]:

HOMEWORK

  1. try to install python & numpy using pip
  2. try at least two of the (recommended) python IDEs
  3. try at least two of the magic function in jupyter/ipython

In [ ]: