Last week, we focused on how to navigate the Terminal, and set up our environments from which we can run Python. This week, we’re going to actually run Python. By the end of this part of today’s lesson, you will be able to...
- Run Python interactively through the Terminal
- Create, edit, and run .py “scripts”: saveable, reproducible Python codes
- Understand and demonstrate the basic use of the print() function
- Write and run your own classic “Hello World!” Python script
- Write a multi-line Python script and understand basic program layout
A Quick Reminder¶
Last week, we set up conda environments that we’ll be using for the whole Astro-RPS series. Before we move forward, you’ll want to move into your “working directory” where you’ve been working on everything up till now (reminder: you can use cd [dirname]
to navigate to your working directory from within the terminal). Once you’ve done that, activate the astro-rps environment:
conda activate astro-rps
Now, you can activate Python in the same terminal by simply typing:
python
or equivalently,
python3.8
If things work properly, you should see a message that includes a few details about the Python version you are running, your operating system, etc. If you do, you should see a blinking cursor ready for you to type code in, often preceded by a >>>. From here, we should already be able to do some basic calculations.
Now, in the same terminal, type
exit()
and hit enter. Once you’ve done so, congrats! You’ve started, written, completed, and quit your first Python session.
From here on, we actually will not recommend you use just “python” and instead recommend:
ipython
The differences are mostly unimportant, but it has some conveniences (like colored text for different keywords) that make it nicer to use.
To create a script, first exit Python. This should you leave in a normal bash shell, from which you can type
touch first_script.py
The command “touch” simply creates a basic text file with the name provided afterwards (here, first_script.py). We are now free to edit this file through whatever means we want: for now, open it up in whatever text editor you prefer (e.g., TextEdit, Notepad, Text Editor for Mac, Windows, and Linux respectively).
Now, try adding
print("Hello World!")
into that file and save it. Then, open up a bash terminal (don’t type python) and type
python first_script.py
from within the same directory where you saved the file. If you’re in the right place, your bash shell should print out the words Hello World. If you run into an error, check first that you’re in the right directory (and if not, use cd to get there).
Comments¶
What if we want to put something into the code that we don’t want to run? In that case, we can comment lines using the #. For our script above, we could add a description of what the code is doing as follows:
# This is a program to print the words Hello World.
print("Hello World!") # this is the line that does the printing.
As can be seen, anything after the hashtag is completely ignored by Python. This is true whether you’ve put the commented phrase on its own line or at the end of another line.
There’s not much more to comments than this! Broadly speaking, commenting is vital piece of making sure codes are understandable. We encourage you to include comments throughout your codes that indicate what important lines are doing. This will help avoid the situation where you log on a week later and totally lose the train of thought that you had the previous week (speaking from experience, we promise this really will happen).
Writing Multi-line Programs¶
With the basics down, we can start to complexify. In Python, distinct commands are separated onto different lines. In most situations, Python is linear: lines are executed from the top to the bottom. To see this for a case when you want to print two different things, you can write the code:
# This code prints a phrase and a number.
print("Hello World!")
print(5+5)
Each print statement has an implicit (but hidden) command at the end that tells it to move to the next line after printing, so you should find that this prints out each result on its own line. If you don’t want that, one choice of syntax to avoid that is:
print("Hello World!", 5+5)
where all we’ve done is separate the things we want by commas. As we’ll later discuss, print() is what we call a function that can take any number of arguments (things inside the parantheses).
When you try to run this script, you should find that “Hello World” does not print. Instead, you will get an IndentationError - telling you that Python expected the second line to be left-aligned with the first line. You might wonder: why does the first line not work, even if the error is on the second line?
The short answer is that Python does a quick check before running your code to see if there any obvious bugs. This procedure is not quite as robust as in some other programming languages, and it won’t catch things like DivideByZero errors, infinite loops, etc. However, it will raise an immediate error if it seems things like unusual spacing or undefined variables (more on the latter in the next notebook). Thus, for the example above, the error is raised before the first line of code is actually run, breaking the program flow.
What if we want to store information between lines?¶
Thus far, we’ve only done a very simple set of computations and immediately printed the result. What if we instead wanted to store the result of that calculation for later? In that case, we will want to define a variable. It’s best to see this by example:
result = 5 + 5
print(result)
Here, we have stored the result of the 5+5 . We could have named “result” anything we wanted - you’re free to choose the name (within certain rules - e.g., t can’t start with a number). In this context, the = sign acts as a so-called “assignment operator”: it takes whatever is on the righthand side and stores it in whatever we have called the variable on the lefthand side. It has nothing to do with equalities or truth/falsity.
We now have everything we need to write our first useful Python program. Let’s use an example from astrophysics: