With this section, we move from using Bash to learning Python. This page provides basic information and introduction to Python.
edit me
To use:Page numbers and headings mentioned in the Python Reader's Guide for Everyone are from the 6/5/2016 PDF version of the text. This is the most recent version available at the time of the summer 2020 course delivery. Page numbers may differ from the printed or other version.
Chuck Severance, the author ofPython for Everyone: Data Exploration with Python 3says in its preface:
Few of my students wanted to become professional computer programmers. Instead, they wanted to be librarians, managers, lawyers, biologists, economists, etc. who wished to use technology skillfully in their chosen field.
As far as I know, this also applies to most of my students, which contributes to my liking this text.
Also, in Chapter 1, Chuck says:
This book assumes that everyone needs to know how to code, and that once you know how to code, you'll figure out what to do with your new skills.
I'm sold! I hope you like this text as much as I do!
Some general Python notes
Python 2 versus Python 3
Python 2 was released in 2000 and quickly became very popular. Python 3 was released in 2008 with some changes that broke a lot of the Python 2 code. For example, python3 requires parentheses around print statements (generally equivalent to bashEco
Domain)
Felshaken 2: press "Hello World!" Felshaken 3: press ("Hello World!")
Because of these changes, many people have resisted the move to Python 3. There is still a lot of code that is Python 2, and there are still people writing in Python 2 (though hopefully not more!).
Some of the new features in Python 3 are so good that Python 2 Holdouts even made a "future" module and it does things like:
aus __future__ import print_function
But the world keeps turning and it's a decade later! Python 2 was originally defined as "End of Life" in 2015, but that wasn't until January 1, 2020Support has officially endedand what should beTHE ENDPython 2 version 2.7.18 was released on April 20, 2020.
But you have to know this:
- We will use Python 3,
- When looking for help, search Google and
- You can find Python 2 code. If you learn to recognize it (printing instructions is the easiest way), you can fix incompatibilities in code obtained from other sources.
Advice:Speaking of getting code from other sources...
Note that encoders rarely start with an empty file. Feel free to copy code from the web (credit the source), reuse your old code, merge snippets from different sources, etc. I rarely write large blocks of code without borrowing a few bits from other scripts. Don't feel like you have to start from scratch!
Python from HiPerGator
At HiPerGator we install and maintain different versions of Python for users. There is a version that comes with the operating system and that is what will be used unless you load a module for the version you want. In general, load a Python module even if you don't care much about the version, as this is where most user-requested Python modules are added. For example:
[magitz@login2 ~]p.squal python/usr/bin/python[magitz@login2 ~]p.sFelshaken--ExecutionPython 2.7.5[magitz@login2 ~]p.sPython Load Module[magitz@login2 ~]p.sFelshaken--ExecutionPython 3.7.6[magitz@login2 ~]p.s
The shebang line (#!) in hyphens
Remember the shebang line for bash scripts?
#!/usr/bin/bash
We need the same for Python scripts. Many sources on the internet will tell you what to do:
#!/usr/bin/python
However, this means that the script is always executed/usr/bin/python
even if this is not the python you want or python is not installed there.
Instead, use:
#!/usr/bin/env python
/usr/bin/env
is a program to browse the environment path and look for a command,Felshaken
in that case. This is more portable and allows Python to be installed anywhere in the user's path.
Ways to use Python
Perhaps even more than Bash, there are many ways to use Python. In Bash, we start by running commands on the HiPerGator command line and move on to writing scripts. In addition to a command prompt and interactive Python scripts, Python adds Jupyter notebooks and some very cool features in VSCode and other development environments. Which one you use is very much up to you, your preferences, and the task at hand.
Python interactive view
Python has an interactive command prompt, similar to the bash prompt:
[share magitz@login3]p.spython3 Load module[share magitz@login3]p.sPython3Python 3.6.5(Default, April 10, 2018, 00:31:24) [CCG 4.8.5 20150623(Roter Hut 4.8.5-16)]not like linux"Help","Copyright ©","loans"Ö"license" forMore information.>>>
And you can start working in Python there. Py4E text does a lot in the command prompt. I personally don't find it very useful. Using Python's command prompt is quite limiting and generally more frustrating than anything else. I rarely use the Python flag.
script Python
As with Bash, we can paste the Python commands into a text file and run the script:
[magitz@login3 examples]p.sdisabled people/ufrc/bsc4452/share/Class_Files/Examples[magitz@login3 examples]p.sgatoHolaMundo.py#!/usr/bin/env python3# This is a simple text printing scriptpress("Hello World!!")[magitz@login3 examples]p.sPython Load Module[magitz@login3 examples]p.spython HelloWorld.py Hello World!![magitz@login3 examples]p.s
This is what I do most of the time, but it requires going back and forth between editing a file and running it. It has the advantage of being relatively easy to run on the cluster and requiring little configuration.
Editing scripts in VSCode
Like bash scripts, Python scripts are plain text files. Any text editor can be used to write Python scripts. But VSCode (and others) have a number of additional benefits worth checking out. We've already discussed the contextual coloring that VSCode does for many languages, including Bash and Python. This is clearly easier to read and spot errors than plain text.
Good text editors also have code hints (some rely on machine learning algorithms that can often suggest entire blocks of code for you!) and feature hint popups to help you write your code. They have linters to check their syntax and flag errors. And they are configurable to the way you want to code.
VSCode can also run your code and help you debug it by showing you variable values as you step through your code. You can see the output on the console.
It requires a few extra steps (installing Microsoft's remote development extension and connecting to HiPerGator as a remote SSH server), but you can even edit and run code in HiPerGator from your local computer!
This is my main development tool. Whether I'm developing locally on my own computer or remotely on HiPerGator, for larger projects I use VSCode scripts.
Jupyter-Notebooks
Advice:For the rest of the semester, I would generally recommend Jupyter notebooks for most things. This is the easiest way to test things out and work on smaller projects.
Jupiteris a web application that creates documents with live code, views, and Markdown-formatted descriptive text (used in most of these pages).
Jupyter can be run on HiPerGator by accessingjhub.rc.ufl.edu(Must be on UF network)
To use:According to this page, most of my Python notes are in Jupyter notebooks and not in markdown pages as we've seen so far. I will be running these notebooks on JupyterHub on the cluster. You can do the same (make a clone ofRepository) or use mybinder.org to view them.
Also note that the Jupyter pages for the course are in their own repository to allow service notebooks to work optimally through Binder:https://github.com/comptoolsres/Jupyter_content.
Chapter 1: Why should you learn to write programs?
-
Page. 3:1.2 Computer Hardware Architecture: this is a good overview of the things we cover in thecomputer conference.
-
Page. 4:understand programming: As in the bash section, we are talking abouttop down designand thinking of describing the steps to go to the supermarket, the author here uses the analogy of telling a story.
(Video) PY4E - Files (Chapter 7 Part 1)Even though the author talks about the need to learn the vocabulary and grammar of a language, we noticed that you already know some Bash grammar. Programming languages use similar grammar and vocabulary.
-
Page. 6:1.5 Talking with Python: We will use Python in HiPerGator. To access the Python prompt, log in, load the Python module, and type python:
[magitz@login3 ~]p.sPython Load Module[magitz@login3 ~]p.spython python 3.7.6 | packed by conda-forge |(Default March 23, 2020 11:03:20 PM) [GCC 7.3.0] no tipo Linux"Help","Copyright ©","loans"Ö"license" forMore information.>>>
-
Page. 8th:1.6 Terminology: interpreters and compilers: Let's talk about that for a momentCreating applications in the Linux area. This section deals furtherinterpretedcontracompiledprogramming languages.
-
Page. 10:1.7 What is a program?: Just like the bash scripts we write, Python can be programmed.
To use:Page. eleven:1.8 What is a program?
A copy of all the example code used in the Py4E text is at/blue/bsc4452/share/Class_Files/Py4E_files/code3/
.
-
Page. 12:1.10 What can go wrong?Quite! As I've said many times, expect bugs, expect things not to work. Reading and understanding Python error messages is a skill! Note that, of the three error types, Python only reports syntax errors. Logical and semantic errors are harder to find and will cause your code not to do what you intended.
-
Page. 14:1.11 Troubleshooting: In addition to the methods here, we'll look at how to use the VSCode debugger in the class to step through your code and write test cases to see if you get the expected response.
-
P. sixteen:1.14 exercises: I don't usually assign the exercises, but I encourage you to think about it and try some of them. They are a good way to test yourself and develop a more fluent handling of chapter content.
Hang tags: Felshaken