- 1 Introduction:
- 2 How Does This Webpage Work?
- 3 Logging in to HPCC
- 4 Let's Play With Python:
- 5 Python Variables
- 6 Input
- 7 Interacting with the System
- 7.1 Assignment:
- 8 Functions and Control
- 9 Resources:
We are teaching python because it is an excellent language for you to learn. It is RELATIVELY easy to grasp the concepts and flow of the language.
It helps that it's popular.
It also helps that it is not compiled.
What is Python?
Python is an interpreted language, it is read, line by line, by the python executable, and each line is executed in its proper order.
This means that is has more flexibility than compiled languages. It also means that errors are not ALWAYS found when you run it.
What can you Do with Python?
Whatever you want (mostly).
So, what are WE going to do with it?
How Does This Webpage Work?
We will have three types of text. Material that is part of a discussion, which will be in plain text, like this:
See what I did there?
We will have code that you can copy and paste into a file:
Expected output will be displayed in "info" boxes:
We will also have commands to copy and paste into a command line. They will look like this:
So, what are we going to do?
Logging in to HPCC
For Windows Users:
1) Install MobaXterm:
2) Open MobaXterm
3) Connect to hpcc:
For Mac Users:
1) Install Xquartz:
2) Open a terminal
3) Connect to HPCC:
ssh -XY email@example.com
For Linux Users:
ssh -XY firstname.lastname@example.org
You should see something similar to this:
This is the "gateway" DO NOT DO ANY WORK ON THIS MACHINE.
Well except for this:
module load powertools
What is happening is that you are being logged in to a small portion of the cluster, where you can do work, and not get in eachother's way. When it works you should see this:
Once you are logged in, we can start doing "real" work
Let's Play With Python:
First, we are going to load the necessary modules to be able to DO anything with python (Most of these will be used LATER):
module load spyder scipy numpy matplotlib
now, let's open up a python only Interactive Development Environment (spyder)
You SHOULD get a new window that looks like this:
On the left is an open area for your code (temporary file).
On the right is an "object inspector", and a "console"
Object inspector will show you objects as you create them in the console, console will run your code.
So, let's try it out:
Copy this into the left hand box:
and press the "play" button:
In the lower Right hand box we should have:
Well, that was easy.
Everybody can go home.... RIght?
What's that, you want to do more?
Variables are created when assigned value
Variables are much similar to most languages, except you do not explicitly declare type (unless you really really want to)
(ignore the parts you don't understand)
Typing of variables
Variables are automatically typed, if you don't tell Python differently:
Python is given a variable name (variable), and told to make it equal to "Hello World"
So, now it treats variable as a string. Python can do a lot of things to strings. More on that later.
What CAN'T Python do to strings? (Math)
So, it won't do it. But this:
So what happened?
Dynamic Variable Typing.
Types of variables cannot be combined, but you CAN recast variables, or have variables retype. I wouldn't recommend it most of the time though.
So, what can we do next?
Information by command line
As with most programs, any information following the command is parsed by the program as arguments
Let's make a program. Paste this into a new file in your main spyder window:
Now select save as, and navigate to ~/introToPython
save the file as: printArgs.py
Go to the terminal and:
python printArgs.py one two three
Or, we can get information WHILE the program is running
Reading from STDIN
This starts to show some of the specifics of how python does things. Bear with me, all will be made clear(er).
To read, we have to OPEN the stdin handle. In other words, we need to read FROM the terminal, and tell Python we want to.
Reading from a file:
Now the fun begins.
15 minute break
Interacting with the System
To do anything useful, you need to be able to do some basic things:
run commands (maybe)
To do this, you will need to use a python library. (os).
To use the things in that library, you need to understand how to call functions from libraries.
So, this example:
- We import libraries of functions (os and subprocess)
- Then we print the result of os.getcwd()
- Then we use subprocess.chdir to change the working directory, to ../introToPython (where the file is)
- then we open the file as a FILEHANDLE (that you can perform functions on)
- Then we declare two lists
- THEN we read the whole file in a for loop (this loop generates a new value for the variable 'line' for every line in filehandle f it reads
- Then we do fancy functions (split) on that string (line.split(' ') returns a list of all values in variable line separated by ' ') and assign it to a temporary set of values.
- THEN we append each of those values to the lists
- THEN we print those lists
Now, your turn.
The file "rabbitFox.csv" is a two column file with rabbit and fox populations over time.
1) open the file
2) read each line
3) Find the difference between rabbit and fox populations for each generation
4) save it to a list
5) print that list
(for bonus points, request a number from STDIN and either multiply or divide the value by it before you move on)
Functions and Control
For tests, or for general statements, you DON'T NEED PARENTHESES:
But, you also don't need curly brackets, blocks of code (denoted by the previous line having a : ) are INDENTED
Loops function the same way (we've already seen one):
But wait, there's more.
Who recognizes this:
Python has a function called RANGE to deal with this:
so, this steps through the ranges.
OK, so now to the next challenge:
Write a program that runs through ONE for loop, and prints every even number between 1 and 25
as with most languages, sometimes, you want to do the same thing in multiple places, so you want to write a function, and call it in both places. Functions are just blocks of code, that are DEFINED.
So, now we can play a little:
in Spyder, open the files
Generate Fox Data:
(this example is borrowed)
1) functions everywhere
2) uses the random() function to determine if individuals survive, starve, or get eaten
3) steps through 200 generations and saves
1) loadtext() is WAY easier to get data from tables than the for loop from before
2) Graphing is done using the matplotlib library
1) Change the number of generations from a number to a variable.
2) Allow that variable to be set by using stdin or a command line argument
3) make the probablities variables
4) Make the save file a variable, so that you can see multiple result sets
5) Run the program
1) graph your results
2) what can you change?
Other Python resources:
And of course: