Let’s write the following (terrifying, and quite horrifying!) Python program and try to execute it.
Notice: If you don’t know how to create a new Python project in Eclipse IDE, click here.
In window “test” type only the first letter from the print statement by hitting the “p” key on your keyboard. Automatically, a popup window appears, as shown in Figure 1. This window contains some of the available Python statements, variables, functions, and other items that begin with the letter “p.” Furthermore, by hitting the CTRL + Space key combination, you can get even more options!
Figure 1 The popup screen in the Source Editor window
You can highlight a selection by using the up and down arrow keys on your keyboard. Notice that each time you change a selection, a second window displays a small Help document about the item selected.
Type the second character, “r”, from the print statement. Now the options have become fewer, as shown here.
Select the “print – print (considers python 2 or 3)” option and hit the “Enter ↵” key. The statement is automatically entered into your program, as shown here!
Complete the statement by typing “Hello World” inside double quotes. Your Eclipse environment should look like this.
Figure 9–2 Entering a Python program in the “test.py” file
Now let’s try to execute the program! From the toolbar, click on the “Run As” icon. Alternatively, from the main menu, you can select “Run
→ Run” or even hit the CTRL + F11 key combination. The Python program executes and the output is displayed in the “Console” window as shown in Figure 9–3.
Figure 9–3 Viewing the results of the executed program in the Console window
Notice: If, while you are trying to execute your first program, a popup window asks you to select a way to run ‘test.py,’ select the “Python Run” option and click on the “OK” button.
Congratulations! You have just written and executed your first Python program!
Now let’s write another Python program, one that prompts the user to enter his or her name. Type the following Python statements into Eclipse and hit CTRL + F11 to execute the file.
name = input("Enter your name: ") print("Hello", name) print("Have a nice day!")
Remember! You can execute a program by clicking on the “Run As” toolbar icon, or by selecting “Run
→Run” from the main menu, or even by hitting the CTRL + F11 key combination.
Once you execute the program, the message “Enter your name: ” is displayed in the “Console” window. The program waits for you to enter your name, as shown in Figure 9–4.
Figure 9–4 Viewing a prompt in the Console window
To enter your name, however, you must place the cursor inside the “Console” window. Then you can type your name and hit the “Enter ↵” key. Once you do that, your computer continues executing the rest of the statements. When execution finishes, the final output is as shown in Figure 9–5.
Figure 9–5 Responding to the prompt in the Console window