Introduction to Python

Reading Materials for This Chapter

This chapter of the lecture note has been partially extracted and modified from the following materials:

Fortran vs. Python

So far we have studied Fortran. We now begin to study another language called Python. Let’s take a moment and consider why these two choices of language are good for scientific computing. Obviously, there are couple of good reasons we look for studying these two language choices:

  • Both are good examples of high-level languages which are much easier to program than low-level languages (e.g., machine languages, assembly languagues).
  • Being high-level languages, they both have to be processed into low-level languages before they can run.
  • They are different types of languages which are processed in two different ways: interperted (Python) vs. compiled (Fortran) languages. With the interpreter, Python is processed at runtime, as opposed to Fortran which is to be compiled before executing it.
  • They follow two different programming paradigms: object-oriented (Python) vs. procedural (Fortran) languages. The object-oriented programming in Python allows to encapsulate code within objects. An object is a component of a program which knows how to perform certain actions and how to interact with other elements of the program. On the other hand, the prodedural programming in Fortran carries out step-by-step instructions as implemented in codes based on procedures, also known as routines or subroutines.
  • Both are very useful for certain common tasks in scientific computing. They can easily be combined together to take advantage of the best of both worlds.
  • Many scientific programs are written in one of these languages, so it’s really worthwhile to learn them.
  • Both are freely available and students and researchers can easily set up laptops and desktops to use them for their research.

Note

Objects are the basic units of object-oriented programming. Each object is associated with its property and methods. A quick logical example of explaining an object would be a person. First of all, a person has a name which can be thought as a property. A person can also do something, such as walking. This would be considered a method of the person. A method in object-oriented programming is like a procedure in procedural programming. The key difference between the two programming paradigms lies in the fact that the method is part of an object which is the embodiment of a concept called class. See short clips on comparing the two paradigms:

Among the listed above, one of the biggest differences between Python and Fortran lies in the way how they are processed into a low-level machine friendly language. In the case of Python, an interpreter reads Python’s high-level program and executes it, by literally following what the program says. It processes the program a little at a time, reading lines and performing computations exactly as they are written in the program. This makes the interpreted languages slower than the compiled languages. Python can be executed by a Python interpreter in two different ways: interactive mode and script mode. For instance, the interpreter displays the result:

>>> 1+1  # this is what you type
2        # this is how the interpreter replies

The chevron >>> is the prompt the interpreter uses in order to indicate that it is ready. The interactive mode is particularly handy when running small pieces of code, because you can type and execute them immediately at the prompt.

Alternatively, you can write and save a code in a file with the file extention .py (e.g., yourCode.py)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# /lectureNote/chapters/chapt03/codes/examples/yourCode.py


print 'hello python!'

a = 1+4
print 'a = 1+4 = ', a

if a<=6:
    print 'this is correct'
    print 'do we print more?'
elif 6<a and a<10:
    print 'this is wrong'
    
print 'is this to be printed or not?'

'''
block comment:
this is a comment
'''

and use the interpreter to execute the contents of the file. This is called the scpript mode in which you can run the code as follow:

$ python yourCode.py

hello python!
a = 1+4 =  5
this is correct
do we print more?
is this to be printed?

The script mode should be the default way you run your code in particular when the code is of anything more than a few lines. In this way you can save your code as a script so that you can modify and run it anytime in future.

Compared to an interpreter, a compiler reads the program and translates it completely before the program starts running. In this context, the high-level program is called the source code, and the translated program is called the object code or executable. Once a program is compiled, we can execute the executable as many as we can without further translation.

Note

Obviously there is no interactive mode in a compiled language, but only the script mode.

Learning two new languages in a quarter along with the many other topics we will be covering may seem overwhelming, but in many ways it makes sense to learn them together. By comparing features in the two languages it may help clarify what the essential concepts are, and what is truly different about the languages vs. simply different syntax or choices of convention.

Running Python

By now you should have already installed software packages for Python on your machine(s). Please make sure the following essential items are operational on your computer before we proceed to learn Python:

  1. Python 2.7.10 or higher,
  2. Python scientific libraries (e.g., Anaconda, NumPy and SciPy, Matplotlib, etc.)

However, in case you haven’t had a chance to install these yet, you can still use an online terminal where you can run basic Python commands in interactive mode using the free online interpreters available in the below:

  1. A full list of online terminals from www.tutorialspoint.com
  2. iPython
  3. SciPy

Python 2 vs. Python 3?

The class lecture note will assume the use of Python 2 as the default choice and examples will be given as Python 2 style (specifically, Python 2.7). You can choose any one of them, or choose both of them.

To see discussions on the comparing the two, see:

There are various ways for users to switch back and forth betweeen Python 2 and 3. You can create an alias command to set either one of the two Python installation paths in .bash_profile. An upgraded version of such Python version managements is to use pyenv, which can be installed easily on your machine. To see how to install it, take a look at the following article:

Note

After installing (or checking out) pyenv, make sure you add the following lines in your .bash_profile:

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ source ~/.bashrc

Note

Various ways of installing a Python software include:

  1. Install Anaconda (a full blown-up installations including various scientific packages and their dependencies; a couple of GB)
  2. Install Miniconda (a smaller version of Anaconda that includes only conda, Python, their dependencies, pip, zlib, etc. You can then install additional Python softwares on top of Miniconda)
  3. Install pip first using, say, wget; install others manually using pip.