Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Use a Python shell to run statements and expressions interactively.
  • Explain the function of the up and down arrow keyboard shortcuts.

The interpreter

Python is a high-level language, meaning that the source code is intended for humans to understand. Computers, on the other hand, understand only low-level machine code made up of 1's and 0's. Programs written in high-level languages must be translated into machine code to run. This translation process can happen all at once, or a little at a time, depending on the language.

Python is an interpreted language: the source code is translated one line at a time while the program is running. The Python interpreter translates source code into machine code and runs the resulting program. If and when an error occurs, the interpreter stops translating the source code and displays an error message.

Most development environments include a Python shell for experimenting with code interactively. A shell, also called a console or terminal, is a program that allows direct interaction with an interpreter. The interpreter usually runs an entire program all at once. But the interpreter can run one line of code at a time within a Python shell.

Checkpoint

Running a Python shell

Concepts in Practice

Using a Python shell

1.
Python is a _____ language.
  1. high-level
  2. low-level
2.
Which of the following is the most basic line of code the interpreter can run?
  1. print(1 + 1)
  2. 1 + 1
  3. 1
3.
What result does the shell display after running the line name = input()?
  1. the name that was input
  2. nothing (except for >>>)

The arrow keys

A Python shell is convenient for exploring and troubleshooting code. The user can try something, look at the results, and then try something else. When an error occurs, an error message is displayed, but the program keeps running. That way, the user can edit the previous line and correct the error interactively.

The acronym REPL (pronounced "rep ul") is often used when referring to a shell. REPL stands for "read-eval-print loop," which describes the repetitive nature of a shell:

  1. Read/input some code
  2. Evaluate/run the code
  3. Print any results
  4. Loop back to step 1

Most shells maintain a history of every line of code the user types. Pressing the up or down arrow key on the keyboard displays the history. The up arrow displays the previous line; the down arrow displays the next line. That way, the user can repeat a line without having to type the line again.

Checkpoint

Correcting a typo

Concepts in Practice

Using the arrow keys

4.
Which arrow keys were needed to edit the typo?
  1. only the up arrow key
  2. the up and down arrows
  3. the up and left arrows
5.
What keys would the user press to go back two lines?
  1. press the up arrow twice
  2. press the down arrow twice
  3. press the left arrow twice

Try It

Exploring the shell

Running code interactively is a great way to learn how Python works. Open a Python shell on your computer, or use the one at python.org/shell. Then enter any Python code, one line at a time, to see the result. Here are a few expressions to try:

  • x = 5
  • 3*x - 5
  • 3 * (x-5)
  • x
  • type(1)
  • type('1')
  • str(1)
  • int('1')
  • abs(-5)
  • abs(5)
  • len("Yo")
  • len("HoHo")
  • round(9.49)
  • round(9.50)

Note: These functions (type, str, int, len, and round) will be explored in more detail later in the chapter. You can read more about the built-in functions in the Python documentation.

Try It

Correcting mistakes

Open a Python shell on your computer, or use the one at python.org/shell. Run the following two statements in the shell:

  • x = 123
  • y = 456

Making mistakes is common while typing in a shell. The following lines include typos and other errors. For each line: (1) run the line in a shell to see the result, (2) press the up arrow to repeat the line, and (3) edit the line to get the correct result.

  • print("Easy as", X)
  • print("y divided by 2 is", y / 0)
  • name = intput("What is your name? ")
  • print(name, "is", int(name), "letters long.")
  • print("That's all folks!)

The expected output, after correcting typos, should look like:

  • Easy as 123
  • y divided by 2 is 228.0
  • (no error/output)
  • Stacie is 6 letters long.
  • That's all folks!
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.