Python Programming¶
Prerequisite¶
In order to conduct this lesson you should
- Log onto your FutureSystems account
- Completed the Shell lesson
- Completed the Editing Files lesson
Learning Goals¶
At the end of this lesson you will be able to:
- use Python in your FutureSystems account
- use the interactive Python interface
- understand the basic syntax of Python
- write and run Python programs stored in a file
- have an overview of the standard library
- install Python libraries using
virtualenv
Description¶
Python is an interpreted, dynamic, high-level programming language and is suitable for a wide range of applications.
The ideals of Python are expressed in The Zen of Python, of which several statements are:
- Explicit is better than implicit
- Simple is better than complex
- Complex is better than complicated
- Readability counts
The main features of Python are:
- Use of indentation whitespace to indicate blocks
- Object orient paradigm
- Dynamic typing
- Interpreted runtime
- Garbage collected memory management
- a large standard library
- a large repository of third-party libraries
Python is used by many companies (such as Google, Yahoo!, CERN, NASA) and is applied for web development, scientific computing, embedded applications, artificial intelligence, software development, and information security, to name a few.
Acknowledgments¶
Portions of this lesson have been adapted from the official Python Tutorial copyright Python Software Foundation.
Introduction¶
Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.
The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, https://www.python.org/, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.
The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications.
This tutorial introduces the reader informally to the basic concepts and features of the Python language and system. It helps to have a Python interpreter handy for hands-on experience, but all examples are self-contained, so the tutorial can be read off-line as well.
This tutorial does not attempt to be comprehensive and cover every single feature, or even every commonly used feature. Instead, it introduces many of Python’s most noteworthy features, and will give you a good idea of the language’s flavor and style. After reading it, you will be able to read and write Python modules and programs, and you will be ready to learn more about the various Python library modules.
Using Python on FutureSystems¶
In order to use Python you must log into your FutureSystems account. Then at the shell prompt execute the following command:
$ module load python
This will make the python
and virtualenv
commands available to
you.
Tip
The details of what the module load
command does are described
in the future lesson Software Modules.
Interactive Python¶
Start by entering the interactive loop by executing the command:
$ python
You should see something like the following:
Python 2.7 (r27:82500, Aug 10 2010, 11:35:15)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>>
is the prompt for the interpreter.
This is similar to the shell interpreter you have been using.
Tip
Often we show the prompt when illustrating an example. This is to provide some context for what we are doing. If you are following along you will not need to type in the prompt.
This interactive prompt does the following:
- read your input commands
- evaluate your command
- print the result of evaluation
- loop back to the beginning.
This is why you may see the interactive loop referred to as a REPL: Read-Evaluate-Print-Loop.
Syntax¶
Print Statements and Strings¶
Let us explore the syntax of Python. Type into the interactive loop and press Enter:
print "Hello world from Python!"
The output will look like this:
>>> print "Hello world from Python!"
Hello world from Python!
What happened: the print
statement was given a string to
process. A statement in Python, like print
tells the
interpreter to do some primitive operation. In this case, print
mean: write the following message to the standard output.
Tip
Standard output is discussed in the /class/lesson/linux/shell lesson.
The “thing” we are print``ing in the case the the **string**
``Hello world from Python!
. A string is a sequence of characters. A
character can be a alphabetic (A through Z, lower and upper case),
numeric (any of the digits), white space (spaces, tabs, newlines,
etc), syntactic directives (comma, colon, quotation, exclamation,
etc), and so forth. A string is just a sequence of the character and
typically indicated by surrounding the characters in double quotes.
So, what happened when you pressed Enter? The interactive Python
program read the line print "Hello world from Python!"
, split it into
the print
statement and the "Hello world from Python!"
string, and
then executed the line, showing you the output.
Variables¶
You can store data into a variable to access it later. For instance, instead of:
>>> print "Hello world from Python!"
which is a lot to type if you need to do it multiple times, you can store the string in a variable for convenient access:
>>> hello = "Hello world from Python!"
>>> print hello
Hello world from Python!
Booleans¶
A boolean is a value that indicates the “truthness” of something.
You can think of it as a toggle: either “on” or “off”, “one” or “zero”, “true” or “false”.
In fact, the only possible values of the boolean (or bool
) type in Python are:
True
False
You can combine booleans with boolean operators:
and
or
>>> print True and True
True
>>> print True and False
False
>>> print False and False
False
>>> print True or True
True
>>> print True or False
True
>>> print False or False
False
Numbers and Math¶
The interactive interpreter can also be used as a calculator. For instance, say we wanted to compute a multiple of 21:
>>> print 21 * 2
42
We saw here the print
statement again. We passed in the result of
the operation 21 * 2
. An integer (or int) in Python is a numeric value
without a fractional component (those are called floating point
numbers, or float for short).
The mathematical operators compute the related mathematical operation to the provided numbers. Some operators are:
*
— multiplication/
— division+
— addition-
— subtraction**
— exponent
Exponentiation is read as x**y
is x
to the y
th power:
You can combine floats and ints:
>>> print 3.14 * 42 / 11 + 4 - 2
13.9890909091
>>> print 2**3
8
Note that operator precedence is important. Using parenthesis to indicate affect the order of operations gives a difference results, as expected:
>>> print 3.14 * (42 / 11) + 4 - 2
11.42
>>> print 1 + 2 * 3 - 4 / 5.0
6.2
>>> print (1 + 2) * (3 - 4) / 5.0
-0.6
Types and Using the REPL¶
We have so far seen a few examples of types: strings, bools, ints, and floats. A type indicates that values of that type support a certain set of operations. For instance, how would you exponentiate a string? If you ask the interpreter, this results in an error:
>>> "hello"**3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
There are many different types beyond what we have seen so far, such as dictionariess, lists, sets. One handy way of using the interactive python is to get the type of a value using type()
:
>>> type(42)
<type 'int'>
>>> type(hello)
<type 'str'>
>>> type(3.14)
<type 'float'>
You can also ask for help about something using help()
:
>>> help(int)
>>> help(list)
>>> help(str)
Tip
Using help()
opens up a pager. To navigate you can use the
spacebar to go down a page w
to go up a page, the arrow keys to
go up/down line-by-line, or q
to exit.
Writing and Saving Programs¶
Make sure you are no longer in the interactive interpreter.
If you are you can type quit()
and press Enter to exit.
You can save your programs to files which the interpreter can then execute. This has the benefit of allowing you to track changes made to your programs and sharing them with other people.
Start by opening a new file hello.py
:
$ nano hello.py
Now enter write a simple program and save:
print "Hello world!"
As a check, make sure the file contains the expected contents:
$ cat hello.py
print "Hello world!"
To execute your program pass the file as a parameter to the python
command:
$ python hello.py
Hello world!
Congratulations, you have written a Python module. Files in which Python directives are stored are called modules
You can make this programs more interesting as well. Let’s write a program that asks the user to enter a number, n, and prints out the n-th number in the Fibonacci sequence:
$ nano print_fibs.py
We can now run this like so:
$ python print_fibs.py 5
8
Let break this down a bit. The first part:
python print_fibs.py 5
can be translated to say:
The Python interpreterpython
should run theprint_fibs.py
program and pass it the parameter5
.
The interpreter then looks at the print_fibs.py
file and begins to
execute it.
The first line it encounters is:
import sys
This line consists of the import
keyword.
Here import
attempts to load the sys
module, which has several useful items.
Next the interpreter sees the def
keyword. The begins the
definition of a function, called fib
here. Our fib
function
takes a single argument, named n
within the function definition.
Next we begin a multi-line string between the triple double-quotes. Python can take this string and create documentation from it.
The fib
function returns the n-th number in the Fibonacci sequence.
This sequence is mathematically defined as (where n is subscripted):
This translates to Python as:
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
Next we have the block:
if __name__ == '__main__':
If the interpreter is running this module then there will be a variable __name__
whose value is __main__
.
This if statement checks for this condition and executes this block if the check passed.
Tip
Try removing the if __name__ == '__main__'
block and run the
program.
How does it behave differently?
What about if you replace with something like:
print fib(5)
print fib(10)
The next line:
n = int(sys.argv[1])
does three different things.
First it gets the value in the sys.argv
array at index 1.
This was the parameter 5 we originally passed to our program:
$ python print_fibs.py 5
Substituting the parameter in, the line can be rewritten as:
n = int("5")
We see that the 5
is represented as a string.
However, we need to use integers for the fib
function.
We can use int
to convert "5"
to 5
We now have:
n = 5
which assigns the value 5
to the variable n
.
We can now call fib(n)
and print
the result.
Installing Libraries¶
Often you may need functionality that is not present in Python’s standard library. In this case you have two option:
- implement the features yourself
- use a third-party library that has the desired features.
Often you can find a previous implementation of what you need. Since this is a common situation, there is a service supporting it: the Python Package Index (or PyPi for short).
Our task here is to install the `autopep8`_ tool from PyPi.
This will allow us to illustrate the use if virtual environments using
the virtualenv
command, and installing and uninstalling PyPi
packages using pip
.
Virtual Environments¶
Often when you use shared computing resources, such as
india.futuresystems.org
you will not have permission to install
applications in the default global location.
Let’s see where grep
is located:
$ which grep
/bin/grep
It seems that there are many programs installed in /bin
such as
mkdir
and pwd
:
$ ls /bin
alsacard dbus-cleanup-sockets env hostname mailx pwd
alsaunmute dbus-daemon ex igawk mkdir raw
...
If we wished to add a new program it seems like putting it in /bin
is the place to start.
Let’s create an empty file /bin/hello-$PORTALNAME
:
$ touch /bin/hello-$(whoami)
touch: cannot touch `/bin/hello-albert': Permission denied
Tip
Recall that $PORTALNAME is your username on FutureSystems, which can
also be obtained using the whoami
shell command.
It seems that this is not possible. Since india
is a shared
resources not all users should be allowed to make changes that could
affect everyone else. Only a small number of users, the
administrators, have the ability to globally modify the system.
We can still create our program in our home directory:
$ touch ~/hello-$(whoami)
but this becomes cumbersome very quickly if we have a large number of programs to install. Additionally, it is not a good idea to modify the global environment of one’s computing system as this can lead to instability and bizarre errors.
A virtual environment is a way of encapsulating and automating the creation and use of a computing environment that is consistent and self-contained.
The tool we use with Python to accomplish this is called virtualenv
.
Let’s try it out. Start by cleaning up our test earlier and going into the home directory:
$ rm ~/hello-$(whoami)
$ cd ~
Now lets create a virtual env:
$ virtualenv ENV
PYTHONHOME is set. You *must* activate the virtualenv before using it
New python executable in ENV/bin/python
Installing setuptools............done.
Installing pip...............done.
When using virtualenv
you pass the directory where you which to
create the virtual environment, in this case ENV
in the current
(home) directory. We are then told that we must activate the virtual
environment before using it and that the python program, setuptools,
and pip are installed.
Let’s see what we have:
$ ls ENV/bin
activate activate.csh activate.fish activate_this.py easy_install
easy_install-2.7 pip pip-2.7 python python2 python2.7
It seems that there are several programs installed. Let’s see where
our current python
is and what happens after activating this
environment:
$ which python
/N/soft/python/2.7/bin/python
$ source ENV/bin/activate
(ENV) $ which python
~/ENV/bin/python
Important
As virtualenv stated, you must activate the virtual environment before it can be used.
Tip
Notice how the shell prompt changed upon activation.
Fixing Bad Code¶
Let’s now look at another important tool for Python development: the Python Package Index, or PyPI for short. PyPI provides a large set of third-party python packages. If you want to do something in python, first check pypi, as odd are someone already ran into the problem and created a package solving it.
I’m going to demonstrate creating a user python environment, installing a couple packages from pypi, and use them to examine some code.
First, get the bad code like so:
$ wget --no-check-certificate http://git.io/pXqb -O bad_code_example.py
Let’s examine the code:
$ nano bad_code_example.py
As you can see, this is very dense and hard to read. Cleaning it up by hand would be a time-consuming and error-prone process. Luckily, this is a common problem so there exist a couple packages to help in this situation.
Using pip
to install packages¶
In order to install package from PyPI, use the pip
command.
We can search for PyPI for packages:
$ pip search --trusted-host pypi.python.org autopep8 pylint
It appears that the top two results are what we want so install them:
$ pip install --trusted-host pypi.python.org autopep8 pylint
This will cause pip
to download the packages from PyPI, extract
them, check their dependencies and install those as needed, then
install the requested packages.
Note
You can skip ‘–trusted-host pypi.python.org’ option if you have a patch on urllib3 on Python 2.7.9.
Using autopep8
¶
We can now run the bad code through autopep8 to fix formatting problems:
$ autopep8 bad_code_example.py >code_example_autopep8.py
Let’s look at the result. This is considerably better than before. It is easy to tell what the example1 and example2 functions are doing.
It is a good idea to develop a habit of using autopep8
in your
python-development workflow. For instance: use autopep8
to check
a file, and if it passes, make any changes in place using the -i
flag:
$ autopep8 file.py # check output to see of passes
$ autopep8 -i file.py # update in place
Resources¶
Code Academy has introductory lessons. The Python documentation site is good place to start. The Python website has a long list of introductory books. See also the Python Module index for “builtin” functionality and StackOverflow python tags for a reputable resource. Additionally, searching Google is a valuable skill.
If you are developing using Python on your local machine, we recommend the use of the PyCharm IDE.
Exercises¶
Lab - Python - FizzBuzz¶
Write a python program called fizzbuzz.py that accepts an integer n from the command line. Pass this integer to a function called fizzbuzz.
The fizzbuzz function should then iterate from 1 to n. If the ith number is a multiple of three, print “fizz”, if a multiple of 5 print “buzz”, if a multiple of both print “fizzbuzz”, else print the value.
Lab - Python - Setup for FutureSystems¶
- Create a virtualenv
~/ENV
- Modify your
~/.bashrc
shell file to activate your environment upon login. - Install the
docopt
python package usingpip
- Write a program that uses
docopt
to define a commandline program. Hint: modify the FizzBuzz program. - Demonstrate the program works and submit the code and output.