Python – Hello World Example
Currently there are two main versions of Python called Python 2 and Python 3. The difference between the two versions can be highlighted by the simple print method:
- In Python 2 this is written as print ‘Hello World’
- In Python 3 this is written as print (‘Hello World’)
There are tools available, such as the 2–3 utility, that will partially automate translation from Python 2 to Python 3 but we are still left with significant work to do.
In all our articles when we refer to Python we will always be referring to Python 3.
Hello World example using Python
As the ‘Hello World’ program is about the simplest program in any language, you are doing this without the complexities of the actual language being used. This is very useful as it ensures that your environment, that is the interpreter, any environmental settings, your editor (or IDE) etc. are all set up appropriately and can process (or compile) and execute (or run) your program.
- Create a file as hello with extension as py
- Write below python code to hello.py file
- Run the program using command prompt by navigating to file location and give command as > python hello.py
You can use any text editor or IDE (Integrated Development Editor) to create a Python file. Examples of editors commonly used with Python include Vim, Notepad++, Sublime Text or Visual Studio Code; examples of IDEs for Python include PyCharm and Eclipse. Using any of these tools we can create file with a .py extension. Such a file can contain one or more Python statements that represent a Python program or Script.
Interactive Hello World example using Python
Let us make our program a little more interesting; lets get it to ask us our name and say hello to us personally. The updated program is:
print('Hello, world') user_name = input('Enter your name: ') print('Hello ', user_name)
In the above program, we have used following built in methods,
print() used to print anything that is passed into it,
input() used to get input from users
and variable user_name to store input from user. In fact, in Python the variable name is not restricted to hold only a string such as ‘TalksInfo’ and ‘Hello World’; it can also hold other types of data such as numbers or the values True and False.
This is referred to in Python as Dynamic Typing. That is the type of the data held by a variable can Dynamically change as the program executes.
This article made us getting started with Hello World example using Python Programming.