First Steps
We will now see how to run a traditional 'Hello World' program in Python. This will teach you how to write, save and run Python programs.
There are two ways of using Python to run your program - using the interactive interpreter prompt or using a source file. We will now see how to use both of these methods.
Using The Interpreter Prompt
Open the terminal in your operating system (as discussed previously in the Installation chapter) and then open the Python prompt by typing python3
and pressing [enter]
key.
Once you have started Python, you should see >>>
where you can start typing stuff. This is called the Python interpreter prompt.
At the Python interpreter prompt, type:
print("Hello World")
followed by the [enter]
key. You should see the words Hello World
printed to the screen.
Here is an example of what you should be seeing, when using a Mac OS X computer. The details about the Python software will differ based on your computer, but the part from the prompt (i.e. from >>>
onwards) should be the same regardless of the operating system.
$ python3
Python 3.6.0 (default, Jan 12 2017, 11:26:36)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World")
Hello World
Notice that Python gives you the output of the line immediately! What you just entered is a single Python statement. We use print
to (unsurprisingly) print any value that you supply to it. Here, we are supplying the text Hello World
and this is promptly printed to the screen.
How to Quit the Interpreter Prompt
If you are using a GNU/Linux or OS X shell, you can exit the interpreter prompt by pressing [ctrl + d]
or entering exit()
(note: remember to include the parentheses, ()
) followed by the [enter]
key.
If you are using the Windows command prompt, press [ctrl + z]
followed by the [enter]
key.
How It Works
A Python program is composed of statements. In our first program, we have only one statement. In this statement, we call the print
statement to which we supply the text "hello world".
Getting Help
If you need quick information about any function or statement in Python, then you can use the built-in help
functionality. This is very useful especially when using the interpreter prompt. For example, run help('len')
- this displays the help for the len
function which is used to count number of items.
TIP: Press q
to exit the help.
Similarly, you can obtain information about almost anything in Python. Use help()
to learn more about using help
itself!
In case you need to get help for operators like return
, then you need to put those inside quotes such as help('return')
so that Python doesn't get confused on what we're trying to do.
Summary
You should now be able to write, save and run Python programs at ease.
Now that you are a Python user, let's learn some more Python concepts.
1. the author of the amazing 'Beginning Perl' book ↩