Python Training Workshop 2019

Table of Content

Introduction

Before introducing Python to you, there is a statistic on the pull requests of different programming language in GitHub (Pull requests let you tell others about changes you’ve pushed to a repository on GitHub, i.e. let others know you want to make some changes to the branch.)

Python replaced Java as the second-most popular language on GitHub, with 40
percent more pull requests opened this year than
last.

In 2017, Python replaced Java as the second-most popular language on GitHub, with 40 percent more pull requests opened this year than last. (Statistics from https://octoverse.github.com/)

Could that be a reason that you should learn Python?

Era of Computation

As a beginner tutorial, I try not to assume anyone reading this snippet having a programming or computer science background. But I will expect they do have some common computer knowledge (at least knows how to Google). Concepts in programming can be very abstract, especially when you work in data types and algorithm. But I will try to explain the background knowledge when necessary.

Although it is still arguable that learning computer languages or coding is essential in the future (Or every child should learn coding), it is undoubtedly that programming language plays an important role in our future. One can imagine that having some knowledge in programming will beneficial to job hunting in the future. In Science research, we do a lot of numerical calculations and data reductions which rely heavily on efficient program. And sometimes, research students are required to code themselves.

Python as a versatile language

Python logo. Python is a high-level ,object-oriented and Interpreted programming language.

High-level language

Python is a high-level language which means that the user can care less about memory management or proper declaration of variables. It is less abstract than low-level language and less time to write and compile. The syntax can be expressed similar to English prose or mathematical equations. But it has relatively slower running time than some low-level language.

I find the snippet from stackoverflow.com is quite useful: https://stackoverflow.com/a/3468098

A list of programming languages ranging from very low to very high level:

Object-oriented language

Object-oriented language is an abstract term in computer science, which based on the concept of “objects”. An “object” may contain data, some form of fields, some intrinsic functions. The explanation of object-oriented language will be covered later in this snippet.

Interpreted language

Interpreted language means the evaluation of a line of code to obtain results can happen immediately rather than having to go through a time-consuming, compile and run cycle. Theoretically it speeds up the thinking and debug process.

Users of Python

Python is a general-purpose language. It can solve a lot of problem that a computer can solve. From numerical calculations, statistical analysis, to web framework and user front end, users can do them in Python. Astronomy is a field that utilize and develop many Python packages.

Python Installation via Anaconda

Anaconda is a popular tool to install Python among data scientists. It is suggested beginner can install this distribution to minimize the problem. Please refers to the following instructions:

Python Packages

The term “package” refers to a distribution of a bundle of Python software. Don’t mix this ‘‘distribution’’ up with a larger distribution of software as with a Linux distribution or Anaconda. Installing Python Package is not difficult, but you need to know the name of the package.

Different packages in Python

conda : package management system for Anaconda

Anaconda distribution have their own package management system called conda. To use conda, one should have open a proper Anaconda Python setup as the instruction. And the conda command in terminal is working (any terminal in MacOS/Linux; Anaconda prompt in MS Windows Start menu.)

To search/install packages:

pip : a package management system in Python

To search/install packages:

virtualenv

virtualenv is a tool to create isolated Python environments. Simply open your terminal and type

Create virtual Python environment in conda

conda has its own version to create a separated Python environment.

Jupyter Notebook

What does Jupyter Notebook do

You may want to run a Jupyter notebook when:

The interface of Jupyter notebook different from traditional programming with its code blocks implementations. These code blocks design enable you to get your result quick and taking care-less of your overall programming beauty. It also runs on a server-client mode that is capable of remote accessing and adapted to today’s cloud and mobile approach.

jupyter_logo

Open Jupyter in Linux/MacOS

Type

jupyter notebook

linux_jupyter

Open Jupyter in Windows

Open your Start menu, goes to Anaconda Folder, Click the Jupyter Notebook shortcut (Recommended). Or start the Anaconda Navigator and Launch windows_jupyter

Hand’s on Session

The hand’s on session requires a working python installations with Jupyter installed. The following links are read-only, they do not run calculations in your computer.

First Session:

Second Session:

In case you would like to have a copy, run the following command in your computer (Linux and Mac):

git clone --depth 1 https://github.com/ryan-leung/PHYS4650_Python_Tutorial

These Jupyter notebooks are stored under notebook sub-directory.

Other ways to run Python

Python interpreter

For Linux / OSX, type python in terminal. For windows, open Anaconda folder in Start menu.

Running a python script

You can always check the python version by running its interpreter.

A common shebang line used for the Python interpreter is as follows: #!/usr/bin/env python

You must then make the script executable, using the following command: chmod +x xxxxxxxxx.py

Text editors for python scripts

First, get yourself a text editor. No old-school editor like the notepad anymore.

A good text editor with syntax highlighting will save your life:

Terminal based editor:

Notepad++ (Left) and Sublime Text (Right)

Visual Studio Code (Left) and Textmate (Right)

Brackets (Left) and Atom (Right)

Spyder

Spyder - The Scientific PYthon Development EnviRonment

Features:

Online Platforms

Here are some online Python platform that are quite good indeed.

repl.it

repl.it, an editor + python interpreter

azure-notebook azure-notebook1

Microsoft Azure Notebooks, full stock Jupyter notebook

Train Your Python in spare time

Online Judge

An online judge is an online system to test programs in programming contests. They can also be used to practice your programming skills. Their system can compile and execute code. They provide some pre-constructed data and your submitted code will test against those data and the output will be captured and test against their model answer. If your algorithm (method) is correct and the code has correct syntax, your code shall meet the time limit and memory limit of their system. Some online judge rank the user answers against other users for comparison.

SPOJ

Sphere Online Judge (SPOJ)

HackerRank

HackerRank

AOJ

Aizu Online Judge (AOJ)

Good Reference Material

Credits

This tutorial have referenced the following materials: