This tutorial will walk you through creating a simple Hello World! program in Python. You have several options to run the program. You can
- use Python Interpreter to directly execute Python statements,
- create a Python script file (*.py) and run it from the command prompt,
- or use a Python editor like PyCharm.
If you want to test individual statements, Python Interpreter is your best choice. For instance, when you type 2+2 and press enter, the interpreter will print 4 on the console.
C:\Users\admin>python Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) Type "help", "copyright", "credits" or "license" for more information. >>> 2+2 4
To print “Hello World!” on the screen, you can use Python’s built-in function:
>>> print("Hello World!") Hello World!
If you want to create more complex programs and save your work, you can use a Python script file. Script files allow you to save your code and execute the same code later when you need it.
To create a Python script file, open your text editor (Sublime Text, Atom, Notepad++,…), and type the following code. Save your file as HelloWorld.py. Python script files have the extension .py.
Once you have created a script file, you can execute it from the command line.
C:\Users\Your Name>python HelloWorld.py Hello World!
Alternatively, you might prefer to use an IDE (Integrated Development Environment) for Python development. An IDE offers you a productive development environment.
PyCharm is one of the top IDEs for Python. PyCharm is a cross-platform development environment created by Jet Brains. It allows developing Python programs faster by tools like syntax highlighting, automatic code completion, builder and debugger. You can download the free Community edition of PyCharm from https://www.jetbrains.com/pycharm/download/.
Launch PyCharm IDE and click Create New Project.
By default, the project location is set to C:\Users\User Name\PycharmProjects\untitled but you can change it. Make sure that Base interpreter shows the correct Python installation. Then click Create button.
If you cannot see Base interpreter, click on Project Interpreter to expand the settings.
Once PyCharm finishes with creating the virtual environment, the editor opens with a screen like below.
To create your first Python program select New… from File menu, then select Python File. Type HelloWorld for Python file name.
Type the following code in HelloWorld.py file.
Hello World Python program is ready to run. To run the program, select Run… from Run menu. The program runs and the output of
print() function is displayed in Run window.