We have spend the last few weeks talking about UNIX and bash. Today, we do a major gear-shift and start learning to program in python!
Python is a widely-used and high-level programming language. It is designed to be a language that is easy to learn and easy to read. In this course we will be using python 3. Python 2 is the predecessor to python 3, and is still widely used. They are obviously very similar, but python 3 has a few improvements and enhancements over python 2, so we will be sticking with that. Specifically, python 3.6 should be used, because all programs will be graded using python 3.6.
Before jumping in to writing python, we need to install it! If you plan on using the CS lab machines to do your homework, you don’t need to install this on your personal machine. But most likely, you will want it on your personal machine.
Python works well on Windows, Mac, and Linux. You are welcome to follow along with my lecture examples and programs on your laptops during class. Macs generally come with python installed by default, but most likely you it will be an older version (probably 2.7.X). Thus, you still will probably need to download/install 3.6. Windows machines typically do not come with any version of python, so you will also need to download/install if that is what you’re running.
If you do not already have python installed, you can download it at www.python.org/downloads.
If you have trouble downloading or installing it, email/visit myself or the TAs. Or, you can ask a classmate who has already installed it for some help!
I could go into detail here about what python is and what it can accomplish. However, the python website has a wonderful tutorial, and the first section goes into these details. I will give some assigned reading form this tutorial today and in the coming weeks, so I recommend you bookmark this page. For this lecture, Read sections 1, 2, and 3.
Officially, we will be using python 3.6 for all of the examples, exams, and grading in this class. However, the most recent version of python that lectura has is 3.4, and the most recent version of python that the Macs in the CS labs have is 3.5. I did a bit of research and as far as I can tell, 3.6 did not remove any features that 3.5 and 3.4 support, so any program you write that works with 3.4 or 3.5 in this class should also work on 3.6.
Similar to bash, python can be run in two modes. In “interactive” mode, we can run commands one-by-one on a command-line interface, as can be done when running a bash shell. We can also write python code files (containing multiple python commands, conditionals, loops, functions, etc.) and run them with the python interpreter. Today, we’ll focus on using the interpreter, but soon we will move on to writing and running python code from script files.
To fire up the interpreter, open up a new bash session, and then type python3
(assuming python is installed).
You should see something like:
$ python3
Python 3.6.0 (default, Dec 24 2016, 00:01:50)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Similar to how $
is the prompt for bash, >>>
is the prompt for python.
When you see >>>
on it’s own line, it indicates the python interpreter is waiting for a command.
As we know well by now, echo
is used in bash to print a string of characters to stdout.
If we want to print “Hello World” in bash, we would run echo "Hello World"
.
We use print()
function to accomplish this in python.
That being said, let’s write our very first, very simple, python command:
>>> print('Hello World')
Hello World
Wooohooo! We printed Hello World
to stdout.
Soon, I’ll explain a bit more about how this print()
function works.
Python programs are made up of one or more statements.
The above example of print("hello World")
is a single statement.
When using python in interactive mode, the user can specify individual statements and have them executed by pressing “enter”.
For the time being, we will stick to learning python in interactive mode. In the near future, we will learn how to write python scripts, which are files that can be executed and have multiple statements within them.
We will skip to the lecture 6 notes for the rest of the intro to python discussion.