Interacting With Python
Interacting With Python
Python Tricks
Email…
At this point, you should have a working Python 3 interpreter at hand. If you need
help getting Python set up correctly, please refer to the previous section in this
tutorial series.
Here’s what you’ll learn in this tutorial: Now that you have a working Python
setup, you’ll see how to actually execute Python code and run Python programs. By Table of Contents
the end of this article, you’ll know how to: • Hello, World!
• Using the Python Interpreter
• Use Python interactively by typing code directly into the interpreter
Interactively
• Execute code contained in a script file from the command line • Running a Python Script from
• Work within a Python Integrated Development Environment (IDE) the Command Line
• Interacting with Python
It’s time to write some Python code!
through an IDE
• Online Python REPL Sites
Hello, World! • Conclusion
There is a long-standing custom in the field of computer programming that the first
code written in a newly installed language is a short program that simply displays
the string Hello, World! to the console.
Python
print("Hello, World!")
You will explore several different ways to execute this code below.
The session continues in this manner until you instruct the interpreter to terminate.
Most of the example code in this tutorial series is presented as REPL interaction.
For example, in Windows, there will likely be a program group in the Start menu
labeled Python 3.x, and under it a menu item labeled Python 3.x (32-bit), or
something similar depending on the particular installation you chose.
Alternatively, you can open a terminal window and run the interpreter from the
command line. How you go about opening a terminal window varies depending on
which operating system you’re using:
Using your operating system’s search function to search for “command” in Windows
or “terminal” in macOS or Linux should find it.
Improve Your Python
Once a terminal window is open, if paths have been set up properly by the Python
install process, you should be able to just type python. Then, you should see a
response from the Python interpreter.
Windows Console
C:\Users\john>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Technical Note: If you are on a Linux system and installed Python 3, it may be
that both Python 2 and Python 3 are installed. In that case, it is possible that
typing python at the prompt will start Python 2. Starting Python 3 may require
typing something else, like python3.
If you installed a more recent version of Python 3 than the one that was
included in the distribution, you may even need to specify the version you
installed specifically—for example python3.6.
If you are not seeing the >>> prompt, then you are not talking to the Python
interpreter. This could be because Python is either not installed or not in your
terminal window session’s path. It’s also possible that you just haven’t found the
correct command to execute it. You can refer to our installing Python tutorial for
help.
1. Ensure that the >>> prompt is displayed, and the cursor is positioned after it.
2. Type the command print("Hello, World!") exactly as shown.
3. Press the Enter key.
The interpreter’s response should appear on the next line. You can tell it is console
output because the >>> prompt is absent:
Python >>>
If your session looks like the above, then you have executed your first Python code!
Take a moment to celebrate.
Congratulations!
• You remembered the opening quotation mark but forgot the closing one:
Python >>>
Python >>>
Python >>>
Python >>>
If you got some sort of error message, go back and verify that you typed the
command exactly as shown above.
Python >>>
C:\Users\john>
Python >>>
>>> ^Z
C:\Users\john>
• If all else fails, you can simply close the interpreter window. This isn’t the best
way, but it will get the job done.
Eventually though, as you create more complex applications, you will develop
longer bodies of code that you will want to edit and run repeatedly. You clearly don’t
want to re-type the code into the interpreter every time! This is where you will want
to create a script file.
Python scripts are just plain text, so you can edit them with any text editor. If you
have a favorite programmer’s editor that operates on text files, it should be fine to
use. If you don’t, the following are typically installed natively with their respective
operating systems:
• Windows: Notepad
• Unix/Linux: vi or vim
• macOS: TextEdit
Using whatever editor you’ve chosen, create a script file called hello.py containing
the following:
Python
print("Hello, World!")
Now save the file, keeping track of the directory or folder you chose to save into.
Start a command prompt or terminal window. If the current working directory is the
same as the location in which you saved the file, you can simply specify the filename
as a command-line argument to the Python interpreter: python hello.py
Windows Console
Directory of C:\Users\john\Documents\test
C:\Users\john\Documents\test>python hello.py
Hello, World!
If the script is not in the current working directory, you can still run it. You’ll just have
to specify the path name to it:
Windows Console
C:\>cd
C:\
C:\>python c:\Users\john\Documents\test\hello.py
Hello, World!
Shell
jfs@jfs-xps:~$ pwd
/home/jfs
jfs@jfs-xps:~$ ls
hello.py
A script file is not required to have a .py extension. The Python interpreter will run
the file no matter what it’s called, so long as you properly specify the file name on
the command line:
Shell
jfs@jfs-xps:~$ ls
hello.foo
But giving Python files a .py extension is a useful convention because it makes them
more easily identifiable. In desktop-oriented folder/icon environments like Windows
and macOS, this will also typically allow for setting up an appropriate file
association, so that you can run the script just by clicking its icon.
• Syntax highlighting: IDEs often colorize different syntax elements in the code
to make it easier to read.
• Context-sensitive help: Advanced IDEs can display related information from
the Python documentation or even suggested fixes for common types of code
errors.
• Code-completion: Some IDEs can complete partially typed pieces of code (like
function names) for you—a great time-saver and convenience feature.
• Debugging: A debugger allows you to run code step-by-step and inspect
program data as you go. This is invaluable when you are trying to determine
why a program is behaving improperly, as will inevitably happen.
IDLE
Most Python installations contain a rudimentary IDE called IDLE. The name
ostensibly stands for Integrated Development and Learning Environment, but one
member of the Monty Python troupe is named Eric Idle, which hardly seems like a
coincidence.
The procedure for running IDLE varies from one operating system to another.
If you get an error saying command not found or something to that effect, then IDLE is
apparently not installed, so you’ll need to install it.
Follow whatever procedure is appropriate for your distribution to install IDLE. Then,
type idle3 in a terminal window and press Enter to run it. Your installation
procedure may have also set up a program icon somewhere on the desktop to start
IDLE as well.
Whew!
Using IDLE
Once IDLE is installed and you have started it successfully, you should see a window
titled Python 3.x.x Shell, where 3.x.x corresponds to your version of Python:
The >>> prompt should look familiar. You can type REPL commands interactively,
just like when you started the interpreter from a console window. Mindful of the qi of
the universe, display Hello, World! again:
It also provides context-sensitive help. For example, if you type print( without
typing any of the arguments to the print function or the closing parenthesis, then
flyover text should appear specifying usage information for the print() function.
• If you have typed in several statements, you can recall them with Alt + P
and Alt + N in Windows or Linux.
• Alt + P cycles backward through previously executed statements;
Alt + N cycles forward.
• Once a statement has been recalled, you can use editing keys on the keyboard
to edit it and then execute it again. The corresponding commands in macOS
are ⌘Cmd + P and ⌘Cmd + N .
You can also create script files and run them in IDLE. From the Shell window menu,
select File → New File. That should open an additional editing window. Type in the
code to be executed:
From the menu in that window, select File → Save or File → Save As… and save the
file to disk. Then select Run → Run Module. The output should appear back in the
interpreter Shell window:
Once both windows are open, you can switch back and forth, editing the code in one
window, running it and displaying its output in the other. In that way, IDLE provides
a rudimentary Python development platform.
Thonny
Thonny is free Python IDE developed and maintained by the Institute of Computer
Science at the University of Tartu, Estonia. It is targeted at Python beginners
specifically, so the interface is simple and uncluttered as well as easy to understand
and get comfortable with quickly.
Like IDLE, Thonny supports REPL interaction as well as script file editing and
execution:
Thonny is especially easy to get started with because it comes with Python 3.6 built
in. So you only need to perform one install, and you’re ready to go!
Versions are available for Windows, macOS, and Linux. The Thonny website has
download and installation instructions.
IDLE and Thonny are certainly not the only games going. There are many other IDEs
available for Python code editing and development. See our Python IDEs and Code
Editors Guide for additional suggestions.
This approach may be unsatisfactory for some of the more complicated or lengthy
examples in this tutorial. But for simple REPL sessions, it should work well.
You should get a page with a window that looks something like this:
The familiar >>> prompt shows you that you are talking to the Python interpreter.
• PythonFiddle
• repl.it
• Trinket
Conclusion
Larger applications are typically contained in script files that are passed to the
Python interpreter for execution.
But one of the advantages of an interpreted language is that you can run the
interpreter and execute commands interactively. Python is easy to use in this
manner, and it is a great way to get your feet wet learning how the language works.
The examples throughout this tutorial have been produced by direct interaction
with the Python interpreter, but if you choose to use IDLE or some other available
IDE, the examples should still work just fine.
Continue to the next section, where you will start to explore the elements of the
Python language itself.
Python Tricks
Get a short & sweet Python Trick delivered to your inbox every couple of
days. No spam ever. Unsubscribe any time. Curated by the Real Python
team.
Email Address
Improve Your Python
About John Sturtz
John is an avid Pythonista and a member of the Real Python tutorial team.
Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who
worked on this tutorial are:
Real Python Comment Policy: The most useful comments are those
written with the goal of learning from or helping out other readers—after
reading the whole article and all the earlier comments. Complaints and
insults generally won’t make the cut here.
Name
Keep Reading
basics python