In the last article, we successfully installed and set up python 3.9.4 (the latest version of python) on our windows machine. And, now it’s the perfect time to use python to write our first program.
So, Let’s dive right in!
How to write your first python program:
Traditionally, the first program you write in a new language is called “Hello, World!”.
To print or to display “Hello World!” onto our computer screen we use a built-in (predefined) function called print().
What does print() do?
The print() function simply displays the string onto our computer screen which are inside the parenthesis of print() function.
You will learn about print() function in later tutorials.
Now, You might be wondering that, What is a string?
Basically, A string is a sequence of characters. In python, Strings are surrounded by either single quote, double quote, or triple quote.
And, as you already know, Hello World is also a string. Because, it is a sequence of characters.
Note: Space between any two words is also considered as a part of string.
To display “Hello World” onto our computer screen we have to type “Hello World” inside the parenthesis of print() function.
Here we go!
The Source code to print Hello, World! onto our computer screen:
print('Hello, World!') # or, (This line is a comment in Python, it does not effect on the execution of a code) print("Hello, World!") # or, Print('''Hello, World!''')
Hello, World! Hello, World! Hello, World!
💠 Writing “Hello, World” in the Python IDLE (Integrated development and learning environment) shell
Step 1: Click on start menu and type “IDLE” at the search box (as shown in the below image).
📝 Note: As soon as you click on the start menu, A vertical black blinking line will automatically shown in the search box. And, from the starting of this vertical black line you can type “IDLE”.
💡 Tip: You can directly search for the “IDLE” by clicking on the search box. There is no need to click on the start menu.
Step 2: Select “IDLE” (Python 3.9 64-bit).
– After clicking on the IDLE (Python 3.9 64-bit) option, you should see the following window.
The last line of the above window shows three consecutive greater than signs (i.e.,
>>>) called prompt followed by a vertical black blinking line.
Which simply means that, now you can write your python code.
Now, what you have to do is? You have to write the print(“Hello World”) statement just from the starting of the vertical black line.
After writing the print(“Hello World”) statement, hit the enter button of your keyboard.
you can see the output of your program as shown below.
💠 Writing “Hello, World” in a Python File
Here we are creating a python file in IDLE.
So, To create a Python file in IDLE, you may follow some easy steps:
Step 1: Click on start menu and type “IDLE” at the search box.
Step 2: Select “IDLE” (Python 3.9 64-bit).
Step 3: Now, Click on “File” menu and select “New File”.
💡 You can also use the keyboard shortcut
ctrl + N to selecting a new file.
After you click on New File, the following file will be shown. By default this file has no name.
In the above file you will also see a vertical black line just below the File menu. And, from the starting of this vertical black line you can write your python code.
But, Before writing any line of code, it is better to name the file something, so that you can access the file later by just typing the name of the file and you can also use the written code anywhere you want.
So, To save the new file with some meaningful name, click on “File” menu and select the “Save As…” option. Next, type the name of the file you want (Here I give “myfirstprogram”) and then click on save button.
After clicking on save button, you will see the following file with name as myfirstprogram.py (The extension of any python file is usually .py).
As you can see from the above image, The vertical black line shows where the cursor is currently at, and from this place you can write your code.
So, Just starting from the vertical black line, write the line of code to print the “Hello World” message.
Now to get the output in the screen, click on “Run” and select the “Run Module” option.
After clicking on the “Run Module”, the following popup will be shown with “Source Must Be Saved OK to Save?” message.
Here, just click on the OK button.
After clicking on the “OK” button, you should see the following window, where you can see the output of your program.
💠 Writing “Hello, World” using your Python 3.9 (64-bit) application
1. Click on start menu and type “Python” at the search box.
2. Select “Python 3.9 (64-bit)“.
After you click on Python 3.9 (64-bit), you will immediately see a new window. The last line (i.e.,
>>>) of this window is known as prompt, which simply means that now you can write your python code:
So, Write the line of code to print
"Hello World" just after the
>>> symbol. Next, hit the enter button.
After hitting the enter button, you can see the output of your program which will be just at the next line where you have written your
print("Hello World") statement .
You just wrote your first
"Hello, World!" program in Python.