If you landed here, I am assuming you heard about Python programming and want to learn it. Well, that’s great. And the first step in learning any new programming language is to write the infamous Hello World program.
Let’s write our first Python program to print “Hello World” on the console. Let’s first get to the program and then we will learn how a Python program works. But, before that, we have to install Python on our computer.
Downloading and Installing Python
Python comes with different installers for different operating systems. So, the installation process also differs slightly, but it’s super easy and quick to install Python on any operating system.
1. Installing Python on Windows
Python provides a UI-based full installer for Windows. Go to the Python Releases for Windows page and from the “Stable Releases” section, download the Windows Installer EXE file. Just run the installer and it will install Python in Windows. Make sure to check the option to add Python binary folder to PATH to save you extra effort.
2. Installing Python on Mac OS
Go to the Python Releases for macOS page and from the “Stable Releases” section, download the macOS 64-bit universal2 installer package. Run the installer and it will be done in a jiffy.
3. Installing Python on Linux/Unix
Most of the Linux operating systems comes pre-installed with Python. Just run the “python3 –version” command to confirm it and check the version. If you want to use the latest version, follow the instructions on this page on Python Docs.
If you are serious about learning Python, PyCharm IDE is a must. Go to the PyCharm Downloads page and download the free Community version for your operating system and install it. It’s super easy and it will save you a lot of time.
Python Hello World Program
Now that we have installed Python in our system, we are ready to write our Python program to print Hello World. Here is the python script to print “Hello World” on the console.
Yes, that’s it. It could not have been simpler than this.
Here is the output when this script is executed from the PyCharm IDE.
It’s the simplest program we will ever write. Let’s execute it from the python command line interpreter too.
$ cat hello_world.py print("Hello World") $ python3.7 hello_world.py Hello World $
Print “Hello World” from Python Command Line Interpreter
Python comes with an interpreter, which is a shell-like interface to run python scripts. Let’s see how to print the “Hello World” message on the console from the python interpreter.
$ python3.7 Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("Hello World") Hello World >>>
Understanding Python Hello World Program
- The print() function is one of the built-in functions. This function prints the argument to the console.
- We are calling the print() function with the string argument “Hello World” so that it would get printed on the console.
- When we are executing a python script file, statements get executed one by one. The print() statement gets executed and it prints the “Hello World” message onto the console.
We started our journey to learn Python programming with the conventional “Hello World” program. We installed Python and PyCharm IDE in our system. We learned that the python program can be executed from the PyCharm IDE and terminal. We also got some idea about the python interpreter tool, which is very useful in running small python code snippets.
If you have reached this far, you are destined to learn Python. I would suggest you go through these 5 tutorials next.