In the last tutorial, we completed our Python installation and setup. It’s time to create your first program
The first classic program is usually ‘Hello World’ so let’s get started;
Remember in the last tutorial, we tested if python was successfully installed and setup in our machine. If you are not sure you should read our previous tutorial before you continue.
We are going to see different ways we can use to create and run our first python hello world program.
As stated before, we will use Windows for most of our Python code but feel free to use any operating system of your choice.
Hello World in Command Line
Go the Windows search bar and enter Python.
Click on Python command line tool to open.
In order to write Hello World python program, enter the code below in the prompt.
Click enter to see the result of your code.
If everything works out well for you, you will see an output similar to the image below.
We can see from the code above that the print method has an opening and closing bracket. In older python version, the same code can been written without the brackets.
This sound so easy and simple. Now we need to execute our python code in a different way.
Rather than typing our codes in command line and executing it there, we can create a file and add our code inside the file and execute the code.
Execute Code in Python File
Python files end with .py extension. You can create a python file using a simple text editor.
- Open NotePad program in Windows
- Open the File menu, and click on save as link to open a save dialog
- Change the Save as type option to All Files
- In the File name input box, add text.py and click the save button as shown below.
Now, open the created python text.py file and add the code below to it.
To run the python file, we need to put the file into a python interpreter to execute it.
Although we will get to it but you should realized by now that semicolon is optional in python. We will learn more about it in our future tutorials.
Execute Python Code Using IDE (PyCharm)
As your code base starts to increase, it might become difficult to write, organize, manage and run your code using command line.
To become productive and have access to different tools like code debugging, code suggestions and more you will need a dedicated IDE for python.
There are many Python IDE’s out there. Some are free while others are paid.
We are going to use PyCharm free community edition IDE for our code example in this tutorial.
Feel free to use any python IDE of your choice.
If you have downloaded and installed PyCharm in your system, launch PyCharm IDE from your installation location or shortcut to open it.
From the welcome screen, click the Create New Project link as shown below
In the new open dialog, add the name of the project. I will use FirstProject but you can use any name of your choice.
Create a new python file from File > New > Python File and name it text.py
Add the print hello world code above to the file and click the run button to run the code.
If everything works right you will see an output similar to the image below.
Wow, congrats you have finally created your first python project. Take some rest to refresh your mind and come back for the next tutorial.
In this tutorial, we have explore difference methods to write and run simple python hello world program.
Along the line we have also download and installed PyCharm python IDE and left some other python concepts in our future tutorials.
In the next tutorial, we will cover some essential information a beginner python developer needs to be familiar with and we will go ahead to explore data types in python.