Writing Your First Python Program : Hello World

After you’ve installed IDLE for Python, it’s time to write your first Python program with IDLE.

We will write and run a simple program to print a message “Hello World” on the screen or console.

You can use another text editor, but for simplicity, we will use Python Shell’s built-in file editor (or Editor window). This is where you’ll be writing your first program to display “Hello World”.

IDLE, an integrated development environment (IDE), provides two different windows to write and run a Python program. They are:

  • Shell window
  • Editor window

Let’s understand each one in detail with example programs.

Shell window to create First Python Program


The shell window provides us the feature to run Python statement or instruction one by one in the interactive prompt. It allows the programmer to write one statement at a time.

In the interactive prompt, we can run lines or pieces of codes before using them in a bigger program.

When we will open IDLE on your computer system, the shell window pops up. So, let’s write our first Python program to display a message, “Hello World” on the console.

Open IDLE on your computer system and write the following code below.

print("Hello World")

After writing the above code, press Enter key. As you press the Enter key, Python reads, interprets, and outputs “Hello World” on the console. If any bugs or errors are in the code, then it will display. The screenshot like below should appear.

Writing first Python program in shell window

As you can see in the above screenshot, Python use blue color to show the output, keywords in orange color, and text in quotation in green color. Thus, Python shell is useful to run one statement or a piece of codes immediately.


Note:

A prompt is a text, or a symbol used to represent the system’s readiness to execute the next input. The symbols “>>>” means the computer is waiting for a new statement.

Editor window (Script mode)


The shell window (or interactive prompt) is best to run a single line of statement at a time. It is the best place for solving mathematical expression as well.

However, we cannot write the code every time on the shell window. It is not appropriate to write multiple lines of code.

The major drawbacks of using shell window is that it does not save our code. When we close it, the code you wrote is gone forever. Therefore, if you are working on an extensive project or game, you should not use shell window.

You should use IDLE’s editor window. It will let you save your code. IDLE’s editor window also contains built-in tools to help you write your programs and troubleshoot any errors or bugs.

Using editor window (script mode), we can write multiple lines of code into a file that we can execute it later. Follow all the steps below to create, save, and run your first Python program in editor window or script mode.


(a) Launch IDLE

A shell window opens up when we start IDLE. Ignore it and click on File in the IDLE menu. Choose a New File to create a blank editor window in which we will write our program. Look at the screenshot below.

Launch IDLE


(b) Type the first line of code

Now, in the editor window, write the following line of code.

print("Hello World")

The word “print” is a Python instruction that tells the Python interpreter to print “Hello World” on the console.


(c) Save your file

Before you run your first program code, you must save it. To save it, press “Ctrl + S” or go to the File menu and choose Save option.

Save your program file


(d) Save the file

A pop-up box will open up. Write a name for your program, such as “helloworld”, and click Save, as shown in the below screenshot. Python programs often have a name ending with “.py” extension, which makes them easy to recognize.

When we save a program, Python automatically adds “.py” extension at the end, so we don’t need to write it in.

Type file name


(e) Run your first Python program

After saving the first python program, it’s time to run the program to see if it works. To run your program, click on “Run” on the menu and choose “Run Module”, as shown in the below screenshot.

Run your first python program


(f) Output

As you click on Run module, you will see the message “Hello World” in the shell window. Look at the below output as shown in the screenshot.

Output of first python program


(g) Fix mistakes

If the program code is not working properly, stay calm! Every beginner or programmer makes mistakes, and finding these “bugs” or “errors” is vital if you want to become an expert at coding.

Go back to the editor window and check your program code for typing errors. These typing errors can be in the code. They are:

  • Did you include the brackets properly?
  • Did you write the word “print” accurately?

Fix any mistakes, then attempt to run the program code again.


(h) Add more lines of code

Now, go back to the editor window and write two more lines of code in your program.

print("Hello World")
person = input("What is you name?")
print("Hello,", person)

The second line asks for your name and then stores it in a variable called person. The third line uses your name to display a new greeting. When you will run the above code, the following output will generate.

Output:
    Hello World
    What is you name? Deepak
    Hello, Deepak

(i) Final task

Run the program code again to check it. When you enter your name and press the enter/return key, the shell will display a personalized message.

Congratulations, you have completed your first Python program! You have placed your first step towards becoming a powerful Python programmer.


In this tutorial, we have covered step by step on how to write your first Python program with IDLE. Hope that you will have understood of writing and running your first python program in both interactive prompt and script mode.
Thanks for reading!!!
Next ⇒ Download and Install PyCharm⇐ Prev Next ⇒

Please share your love