Here's a quick list of the conventions I use throughout this guide:
$ echo hello world
hello world
That dollar sign means: we're in the system shell, i.e the program that you're put into as soon as you open the Terminal app. The dollar sign is often the symbol used to signify where you can begin typing in commands (you should see a blinking cursor there).
When following my examples, do not copy the dollar sign.
This is not where you can run Python code. However, you will be running various system commands, such as:
$ cd ~/Desktop/compciv-2016
change directory, in this case, to your compciv-2016
folder if you happen to put it on your Desktop$ git add --all
– add changes to your Git repo$ git status
– find out the current state of your Git repoIn order to complete the assignments, you must write your code and save it as a Python script file, e.g. a.py
.
When I say something to the effect of: "execute your script via the Python command-line interpreter", I mean this:
$ python a.py
It's important to understand the nuance here: that $
means that you're supposed to be in your system shell. But your shell has access to a command named python. And one way to run your Python script, i.e. a.py
, is to pass it into the python command as an argument. When the script finishes running, you are back at your system shell.
Pretend a.py
contains this code:
print("hello")
print("world")
This is how I would depict the process of executing that script and seeing its output:
$ python a.py
hello
world
The lack of the $
in front of the lines for hello
and world
signify that they are the output of the script, not commands that you actually run.
Generally, when I describe Python code to be written into a file and saved, it will look like this:
print("hello")
for x in range(10):
print("world!")
print("all done")
Note that there is no prompt symbol. This is just plaintext, suitable for copy-pasting into your own file which you can then execute from your command-line (though you should always type out the examples to actually learn them).
However, running Python by creating a script and executing it from the command-line can be a slow way to learn new techniques and syntax, at first. So frequently, I will suggest that you "try this code out in your interactive Python shell".
This means, first of all, that you must run the ipython program from your system shell:
$ ipython
Here's where things get confusing…
ipython is itself a shell, except unlike your system shell, it can only understand Python code. You can't run git status
, for example, because that's a command that only your system shell can understand.
Otherwise, you can run Python code by typing it into the prompt and hitting Enter. The point of using the interactive Python shell is that it is, well, interactive, and it's much easier to try things out.
This is the syntax convention I use to indicate that code is meant to be typed into the interactive Python shell; note that I use the triple right-angle brackets – >>>
– to indicate the prompt:
>>> print("hello world")
hello world
>>> 500 + 500
1000
For indented code blocks, such as for for-loops and conditional branches, I use the convention of triple dots to indicate that the corresponding line is part of a block. In other words, hitting Enter will not produce a response – because the interactive shell is waiting for you to finish writing your block of code:
>>> for n in range(5):
... print("hello world", n)
hello world 0
hello world 1
hello world 2
hello world 3
hello world 4
One more thing: if you want to exit out of ipython and back into your system shell, just type exit:
>>> exit
In both the Python shell and the system shell, I occasionally use the pound sign – #
– to indicate a comment that is meant to document a particular line. Comments have no effect on the code. Everything after the #
is not read by the shell program, nor is it meant for you to actually type out:
$ git status # run this for fun!
>>> print("hello world") # this prints something!
hello world