Learn how start, write code in and exit Python interactive sessions in Windows, Ubuntu and Mac OS.
Running Python Code Interactively
Let’s take a look at running code interactively. First, on Windows. Here we are on the Windows desktop. To run a command prompt, hold down the Windows key and tap R, and then type
cmd for command.
Here, you can see the Command window, and typing
python and then hitting Enter will allow us to interact with Python. So you can type the classic thing such as
print('Hello World!'), and you’ll see that gets executed.
Now, running code interactively on Ubuntu. To get to the terminal window, right-click on the desktop and pick Open Terminal. Now in the terminal, you can type
python3, rather than just
python, and now you can interact with Python.
Again with the classic
print('Hello World!'). Or you can do some maths, and Python will evaluate it straightaway. Or you can assign a variable… and later on print the value of that variable out. To exit Python,
exit(), and to exit the terminal, just type
Running Python interactively on macOS. To find a program on Mac, press the Command key and tap Space, and then type the name of the program in question. That gets you into the terminal, where you can type
Become a Member to join the conversation.