In this lecture, I'll introduce the Python programming language and how to interact with it; aka, the proverbial Hello, World! lecture. By the end, you should be able to:
While this lecture notebook is non-interactive, it is available for you (as are all the other lectures released in this course) to download and execute as an interactive notebook.
Instructions for how to do this are included in the Appendix at the end of the lecture. I encourage you to give it a try!
Python as a language was implemented from the start by Guido van Rossum. What was originally something of a snarkily-named hobby project to pass the holidays turned into a huge open source phenomenon used by millions.
The original project began in 1989.
Wondering why a 2.x branch has survived a decade and a half after its initial release?
Python 3 was designed as backwards-incompatible; a good number of syntax changes and other internal improvements made the majority of code written for Python 2 unusable in Python 3.
This made it difficult for power users and developers to upgrade, particularly when they relied on so many third-party libraries for much of the heavy-lifting in Python.
Until these third-party libraries were themselves converted to Python 3 (really only in the past couple years!), most developers stuck with Python 2.
Python is an intepreted language.
In practice, this distinction has become blurry, particularly in the past decade. Interpreted languages in general are easier to use but run more slowly and consume more resources; compiled languages in general are more difficult to program in, but run much more efficiently. As a result of these advantages and disadvantages, modern programming languages have attempted to combine the best of both worlds:
Python is a very general language.
Instead, as Jake VanderPlas put it:
"Python syntax is the glue that holds your data science code together. As many scientists and statisticians have found, Python excels in that role because it is powerful, intuitive, quick to write, fun to use, and above all extremely useful in day-to-day data science tasks."
One of the biggest reasons for Python's popularity is its overall simplicity and ease of use.
Python was designed explicitly with this in mind!
It's so central to the Python ethos, in fact, that it's baked into every Python installation. Tim Peters wrote a "poem" of sorts, The Zen of Python, that anyone with Python installed can read.
To see it, just type one line of Python code:
In [1]:
import this
Lack of any discernible meter or rhyming scheme aside, it nonetheless encapsulates the spirit of the Python language. These two lines are particular favorites of mine:
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Line 1:
Line 2:
Don't you just feel so zen right now?
* Lone exception: code golf
Enough reading, time for some coding, amirite?
So what does it take to write your first program, your "Hello, World!"? Pound-include iostream dot h? Import java dot io? Define a main function with command-line parameters? Wrap the whole thing in a class?
In [2]:
print("Hello, world!")
Yep! That's all there is to it.
Just for the sake of being thorough, though, let's go through this command in painstaking detail.
Functions: print()
is a function.
You can think of it as a direct analog of the mathematical term, $f(x) = y$. In this case, $f()$ is the function; $x$ is the input, and $y$ is the output.
Later in the course, we'll see how to create our own functions, but for now we'll make use of the ones Python provides us by default.
Arguments: the input to the function.
In this case, there is only one argument to print()
: a string of text that we want printed out. This text, in Python parlance, is called a "string". I can only presume it is so named because it is a string of individual characters.
We can very easily change the argument we pass to print()
:
In [3]:
print("This is not the same argument as before.")
We could also print out an empty string, or even no string at all.
In [4]:
print("") # this is an empty string
In [5]:
print() # this is just nothing
In both cases, the output looks pretty much the same...because it is: just a blank line.
print()
finishes printing your input, it prints one final character--a newline.This is basically the programmatic equivalent of hitting Enter
at the end of a line, moving the cursor down to the start of the next line.
Please feel free to fire up this notebook in interactive mode and change the arguments to see what you get! Instructions for doing so are in the Appendix below.
Briefly--a type of data format in Python that exclusively uses alphanumeric (A through Z, 0 through 9) characters.
Look for the double-quotes!
In [1]:
"5" # This is a string.
5 # This is NOT a string.
Out[1]:
Delineators for comments.
#
in Python, everything after that symbol on the same line is ignored.They're there purely for the developers as a way to put documentation and clarifying statements directly into the code. It's a practice I strongly encourage everyone to do--even just to remind yourself what you were thinking! I can't count the number of times I worked on code, set it aside for a month, then came back to it and had absolutely no idea what I was doing)
Ok, so Python can print strings. That's cool. Can it do anything that's actually useful?
Python has a lot of built-in objects and data structures that are very useful for more advanced operations--and we'll get to them soon enough!--but for now, you can use Python to perform basic arithmetic operations.
Addition, subtraction, multiplication, division--they're all there. You can use it as a glorified calculator:
In [6]:
3 + 4
Out[6]:
In [7]:
3 - 4
Out[7]:
In [8]:
3 * 4
Out[8]:
In [9]:
3 / 4
Out[9]:
Python respects order of operations, too, performing them as you'd expect:
In [10]:
3 + 4 * 6 / 2 - 5
Out[10]:
In [11]:
(3 + 4) * 6 / (2 - 5)
Out[11]:
Python even has a really cool exponent operator, denoted by using two stars right next to each other:
In [12]:
2 ** 3 # 2 raised to the 3rd power
Out[12]:
In [13]:
3 ** 2 # 3 squared
Out[13]:
In [14]:
25 ** (1 / 2) # Square root of 25
Out[14]:
Now for something really neat:
In [15]:
x = 2
x * 3
Out[15]:
This is an example of using Python variables.
5var
" would be illegal, but "var5
" would be fine) or conflict with reserved Python words (like print
).Here's an operation that involves two variables:
In [16]:
x = 2
y = 3
x * y
Out[16]:
We can assign the result of operations with variables to other variables:
In [17]:
x = 2
y = 3
z = x * y
print(z)
The use of the equals sign =
is called the assignment operator.
*
), Division (/
), Addition (+
), and Subtraction (-
) are also operators.What happens if I perform an assignment on something that can't be assigned a different value...such as, say, a number?
In [2]:
x = 2
y = 3
In [3]:
5 = x * y
CRASH!
Ok, not really; Python technically did what it was supposed to do. It threw an error, alerting you that something in your program didn't work for some reason. In this case, the error message is can't assign to literal
.
Parsing out the SyntaxError
message:
Error
is an obvious hint. Syntax
gives us some context.The "literal
" being referred to is the number 5 in the statement: 5 = x * y
x * y
to the number 5So we can't assign values to numbers. What about assigning values to a variable that's used in the very same calculation?
In [19]:
x = 2
y = 3
x = x * y
print(x)
This works just fine! In fact, it's more than fine--this is such a standard operation, it has its own operator:
In [20]:
x = 2
y = 3
x *= y
print(x)
Out loud, it's pretty much what it sounds like: "x times equals y".
This is an instance of a shorthand operator.
x
by y
and stored the product in x
, effectively updating it.+=
for addition, -=
for subtraction, and /=
for division.1: Let's say you want to count the number of words in Wikipedia. You have a variable to track this count: word_count = 0
. For every word you come across, you'll update this counter by 1. Using the shorthand you saw before, what would the command be to update the variable at each word?
2: What would happen if I ran this command? Explain. ("5" + 5)
3: In this lecture, we used what is essentially a Python shell in order to execute Python commands. Let's say, instead, we wanted to run a sequence of commands in a script. I've put a couple commands in the file commands.py
. How would you execute this script from the command prompt?
4: What would happen if I ran this command? Explain. x = y
All notebook lectures will be posted to the course GitHub repository here: https://github.com/eds-uga/csci1360-fa16
In order to execute the notebooks and make them interactive (i.e., you can change the code in them and re-execute it to see the results), you have two options:
If you choose to install your own Python environment in Assignment 0, you can host these notebooks on your own computer.
1: Download the contents of the course GitHub repository. You can do this by clicking the green "Clone or download" button and selecting "Download ZIP". Extract the archive somewhere on your computer.
2: Open up a command prompt / Terminal window (instructions for this are in Assignment 0). Within the shell, navigate to the folder with the contents of the ZIP archive you just extracted.
3: Run the command: jupyter notebook
and press Enter
. This will initialize the Jupyter notebook environment, and should immediately spawn a web server you can view through your browser (http://localhost:8888
). Provided you're in the correct directory, you should see a file with the extension .ipynb
(for "IPython Notebook, where "IPython" means "Interactive Python"). Click on that, and Jupyter should immediately render the notebook in interactive mode!
If you want to edit these lectures but didn't want to install Python on your own machine, no worries! A wonderful tool out of HHMI Janelia Farms, mybinder will host GitHub repositories with notebooks in them through their own servers.
1: Go to the course GitHub repository.
2: Click the "launch binder" badge you see on the front page. It may take some time to spin up, but it will eventually provide you an interactive interface to the notebooks.
Yup, that's all!
Regardless of whether you're hosting the notebooks yourself or using mybinder, you interact with the notebooks the same way.
If you want to add new cells: click the "Insert" drop-down menu and select where you'd like to add a new cell. You can then select what type of cell you want with the selectable drop-down; if you're writing Python, you'll want to select the Code
option.