4.1 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69fd75321df075656f836fff | How to Use the Python Interactive Shell | 19 | how-to-use-the-python-interactive-shell |
--description--
In the previous lesson, you learned how to run Python scripts locally. But there might be times where you don't want to create full Python programs and just need to test out some Python code. This is where the Python interactive shell comes in.
But first, let's review what a terminal is.
A terminal is a text-based interface that lets you interact with your computer by typing commands. Each operating system comes with a default terminal app. On macOS, you can use the Terminal app. On Windows, you can use Command Prompt or PowerShell. On Linux, each desktop environment has its own default terminal app, like GNOME Terminal or Konsole.
Open up a terminal app and type in python and press Enter. This will start a Python interactive shell. An interactive shell is a program that lets you type commands one at a time and see the results.
When you start a new session, you might see this type of output initially:
Python 3.12.2 (main, Mar 21 2024, 22:48:26) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> symbol means Python is waiting for you to type a command. Try printing "Hello, world!" to the terminal:
print("Hello, world!")
You should see the text appear like this:
>>> print("Hello, world!")
Hello, world!
After the text is printed, Python goes back to the following:
>>>
This means you can type in another command.
The Python interpreter is following what is known as the Read, Evaluate, Print, Loop cycle. Or REPL for short. Whenever you type in commands, the interpreter will read the input, evaluate it, print the result and loop back to show the >>> so you can type more commands.
What happens if you try to type in an invalid command like this?
>>> something random
Well, the Python interpreter will still follow the same REPL process. In this case, you will get an error message:
>>> something random
File "<stdin>", line 1
something random
^^^^^^
SyntaxError: invalid syntax
If you want to leave the interactive shell you can type exit() or press Ctrl + D for (Mac/Linux) or Ctrl + Z + Enter for (Windows).
Using Python's interactive shell is great for small experiments with code or basic exploration. While you are going through the remaining lessons in the course, you can type in some of the new methods you learned inside an interactive shell.
If you are planning to work with longer programs or working across multiple files, then it is better to work in a code editor or IDE.
--questions--
--text--
What is an interactive shell?
--answers--
A program used to format Python code.
--feedback--
Refer back to the beginning for the correct answer.
A program that lets you type commands one at a time and see the results.
A program used to lint Python code.
--feedback--
Refer back to the beginning for the correct answer.
A program that lets you download popular Python libraries.
--feedback--
Refer back to the beginning for the correct answer.
--video-solution--
2
--text--
What does REPL stand for?
--answers--
Run, Execute, Process, Launch
--feedback--
Refer back to the end for the correct answer.
Read, Execute, Print, Load
--feedback--
Refer back to the end for the correct answer.
Read, Evaluate, Print, Loop
Read, Enter, Parse, Log
--feedback--
Refer back to the end for the correct answer.
--video-solution--
3
--text--
Which of the following is a correct way to leave an interactive shell?
--answers--
Type exit() in the terminal.
Type stop() in the terminal.
--feedback--
Refer back to the end for the correct answer.
Type end() in the terminal.
--feedback--
Refer back to the end for the correct answer.
Type terminate() in the terminal.
--feedback--
Refer back to the end for the correct answer.
--video-solution--
1