Skip to content

1.1 Expressions in the Terminal

This note introduces Python in your terminal, how it works at a high level as well as the general evaluation procedure.

Get back to toc CS61A.

Our Goal

Our goal here is simple. Type code into the terminal, and see it execute your code. Later on in the course, you'll see exactly how one might go about interpreting the code you type in your terminal. This is the Interpretation of Computer Programs, but as of now, we are interested in gaining prerequisite knowledge to understand the Structure of Computer Programs.

First, make sure you have Python on your system. Once you have it, continue reading on.

Open up whatever terminal you use. You can activate what we call an interactive Python session through the command python3. By interactive, I mean you can write code line-by-line and Python will read it as you put it in. You'll know if you are in a Python session if you see the triple greater than sign >>>. You can type in whatever mathematical expression you want (kind of like a more powerful calculator). Try putting some arithmetic expressions in the line and press ENTER. The result will be spit out on the line below, then Python will ask you for the next line.

>>> 2
2
>>> 2 + 2
4
>>> 24 - 12 * 2
0
>>>

You can exit out of a Python interactive session by typing in exit() on the input line.

Summary

Try some expressions out in your terminal. There are many nuances to things you'll learn in the class, and whenever you are confused, test it out by just typing python3 in your terminal.

Next section 1.2 Evaluation Procedure Get back to 1. Python Interpreter or toc CS61A