Python: 'Hello, World!' Program
In this tutorial, you will learn to write and run your first 'Hello, World!' program in Python.
Python Shell
We first start the Python Shell in both MacOS and Linux systems.
Mac OS
Python comes pre-installed on Mac OS X. However, it is most likely the older 2.x version. You can check its version with the command:
$ python --version
To start the interactive shell for the Python interpreter, type python
in the terminal and press ENTER
.
$ python
This interactive shell is known as Python Shell. The interpreter is in interactive mode when the prompt consists of three greater-than signs (>>>
), known as the Python REPL prompt.
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
If you have just typed that in your console and want to exit, type exit()
.
Or if you have downloaded and installed the updated version Python 3.x, you can also just type python3
in the terminal to start the updated shell.
$ python3
The prompt is ready to accept Python commands.
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
We will make use of the Python function print()
to print our 'Hello, World!' message. Type print('Hello, World!')
and hit ENTER
. The print()
function is passed the string value 'Hello, World!', which is also known as an argument (a value which is passed to a function is called an argument).
>>> print('Hello, World!')
The print()
function prints the string Hello, World!
to the console.
Type exit()
to quit the shell.
>>> exit()
Linux: Ubuntu/Debian
As in Mac OS, you type python3
in the terminal to start the Python Shell.
IDLE's Python Shell
Python's IDLE (Integrated DeveLopment Environment) is both code editor and interactive interpreter combined.
In Mac OS, get to /Applications
and expand the /Python 3.x
directory
Python's IDLE is launched. You will see the interactive Python prompt >>>
Go to File
> New File
and type print('Hello, World!')
into the new unamed file.
Save the program with .py
extension inside some directory. Here, I have saved it as hello.py
inside the /python-programs
directory.
To run our hello.py
script, go to Run
option and select Run Module (F5)
.
It will print the "Hello, World!" statement into the console.
From the Command Line
Navigate to the /python-programs
directory where the hello.py
script file is located. Next run the below command.
$ python3 hello.py
Notes
- REPL is short for Read-Eval-Print-Loop.