Note that this site is in currently in version 1.0.0-alpha.   Some functionality may be limited.

1. Interacting with Python

Let’s begin by starting an “interactive session” session with Python. This means we will be using Python in the terminal, which is a special space that allows us to run little bits of Python, experimenting and exploring what it can do, without having to save it. Think of this interactive space as a playground. Later on, we will be working with Python in a more robust way, doing what we call saving and executing Python scripts. For now, though, let’s start an interactive session with Python, which is accessed through the terminal. Open your terminal and type:

$ python

at the prompt. You should see something like this

Python 3.7.6 (default, Jan  8 2020, 13:42:34)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Unlike the normal $ terminal prompt (or % if you are on MacOS), the Python prompt looks like this:

>>>

These greater-than symbols > are how you know that you have entered an interactive session with Python. Now you are interacting directly with Python, rather than in the regular terminal. Keep an eye on these greater-than symbols, as a common early source of confusion is entering terminal commands into the Python prompt or entering Python commands into the terminal.

A Little Math

Let’s try a little math at the Python prompt. In the example below, type the text that appears after the Python prompt (the >>>). The line below is the output that is returned. This will be a standard convention when giving examples using the Python prompt.

>>> 2 + 3
5

>>> 14 - 10
4

>>> 10 * 10
100

>>> 6 / 3
2

>>> 21 % 4
1

The first four operations above are addition, subtraction, multiplication, and division, respectively. The last operation is modulo, or mod, which returns the remainder after division.

Note the way you interact with Python at the prompt. After entering an expression such as 2 + 3, Python “evaluates” it to a simpler form, 5, and then prints out the answer for you. This process is called the Read Eval Print Loop, or REPL. Reading takes commands from you, the input is evaluated or run, the result is printed out, and the prompt is shown again to wait for more input. The normal terminal (the one with the $) is another example of a REPL.

The REPL is useful for quick tests and, later, can be used for exploring and debugging your programs interactively. You might consider it a kind of playground for testing and experimenting with python expressions.

Challenges for lesson 1

Assignment: Challenge

  1. For a few minutes, practice moving in and out of Python’s interactive mode (aka the REPL). You can get out of Python by hitting control + d (or control + z or control + z + enter if you’re on a computer running Windows) or by typing exit(). You can get back in the REPL by typing python at the $ prompt. Remember that you’re in the REPL when you see >>>, and you’re in bash or your terminal when you see the $.

  2. One “operator” (math symbol) we didn’t learn is the exponent—you know, “x raised to the power of…” If you were Guido van Rossum, the creator of Python, how would you define this operator?

  1. The exponent operator is two asterisks (**). For example, the number 3 to the power of 2 would be expressed as 3**2.

Questions

Try again!

What are the characteristics of the REPL? Select all that apply.

Terms Used in Lesson

Can you define the terms below? Hover over each of them to read a preview of the definitions.

REPL

REPL stands for Read, Eval, Print, Loop. It is also known as the Python Interpreter or Interactive Shell, and it refers to a Python environment that processes your code as soon as you press enter. …

See term page

Workshop overall progress