Writing the First Python Program

Summary: in this tutorial, you will learn how to write the first simple but famous program in Python called “Hello World”. The goal of this tutorial is to help you get started with the development environment and execute a simple Python program.

This tutorial requires Eclipse with PyDev installed in your system. If you do not have these tools in place, you can follow the setting up Python development environment tutorial to set them up.

New Python Project in Eclipse

The following steps illustrate how to write a simple program and execute it from Eclipse:

Launch Eclipse IDE

Select File > New > Project…

Enter the project name: HelloWorld and click OK button

New Python Project

New Python Project

From the the project select New > Source Folder to create a folder inside project folder to store source code files.

Create New Source Folder

Create New Source Folder

 

Enter the source folder as src

Enter Source Folder

Enter Source Folder

Right click on the src folder and choose New > File

Create New File

Create New File

Enter the file name as hello.py, where py is the Python’s file extension.

New Python File

New Python File

Enter the source code as below:

Hello World Source Code

Hello World Source Code

Run Python Program

To run the program, select Run As > Python Run

Run Python Project

Run Python Project

The output window will display the message  Hello World

Python Hello World Output

Python Hello World Output

Congratulation! you have developed the first program in Python successfully.  The program is so simple, just one line of code. In this program, you’ve used a built-in function in Python called  print() to display a string on screen.

Python IDLE

Python IDLE (Integration Development  Environment) is the Python IDE that comes with the Python distribution. Python IDLE is also known as interactive interpreter. It has a number of features such as text editor with syntax highlighting, smart indent and auto completion, etc., that help you work with Python quickly.

The following illustrates how to launch Python IDLE and write the Python code inside the text editor:

Launch Python IDLE

Launch Python IDLE

Launch Python IDLE

New Python Shell window will display as follows:

Python IDLE

Python IDLE

Enter the following code and press Enterthe message “Hello World” will display in the next line.

Python IDLE Hello World

Python IDLE Hello World

In this tutorial, we have shown you how to develop the first simple Python program in Eclipse and in Python IDLE.

  • Was this tutorial helpful ?
  • YesNo