Using the Python Console
Using the Python Console
>>>
The text >>> is called the console prompt: the Python console is “prompting” us to type in some
Python code to execute. If we type in a simple arithmetic expression,
>>> 4 + 5
>>> 4 + 5
9
The interpreter took our bit of code, 4 + 5 , and calculated its value, 9 , and displayed that value
to us. Let’s formalize this idea with some terminology that we’ll use throughout the course.
The expression 4 + 5 looks simple enough, but technically it is formed from two smaller
expressions—the numbers 4 and 5 themselves. We can ask Python to evaluate each of these
expressions, though the result is not very interesting.
>>> 4
4
>>> 5
5
A Python literal is the simplest kind of Python expression: it is a piece of code that represents the
exact value as written. For example, 4 is an integer literal representing the number 4.
What about the * symbol? We know from our study of mathematics that * means
multiplication, but what is it, technically, in the Python programming language? A literal,
expression, or something else? Let’s try to evaluate it in the Python console (typing in * and
pressing Enter): 2
2
This example will make much more sense if you’re following along on the Python console on your computer!
>>> *
Traceback (most recent call last):
... lots of text omitted ...
File "<input>", line 1
*
^
SyntaxError: invalid syntax
This is our very first Python error message, telling us that something went wrong when the
Python interpreter tried to execute our code. In this case, we received a syntax error (which the
interpreter calls SyntaxError ), which means that our code was not properly structured. This
should make sense: just writing * by itself is not a valid multiplication operation, as * expects
both an expression on its left and right sides to multiply.
In summary, 3 the expression 4 * 5 consists of two smaller expressions, the literals 4 and 5, joined
3 We’re deliberately using all of the terminology we introduced in this section. Pause at each italicized word and make
sure you understand it (or go back up to review its definition).
together with the arithmetic operator * , representing multiplication. We’ll devote the rest of this
chapter to exploring the different kinds of data we can represent in Python beyond just numbers.
We’ll see how to write literals for different data types and what computations we can perform on
them using different operators.
CSC110/111
CSC110/111Course
CSC110/111 CourseNotes
Course NotesHome
Notes Home
Home