-->

Your First Program With Python

While the interactive shell is good for running Python instructions one at a time, to write entire Python programs, you’ll type the instructions into the file editor. The file editor is similar to text editors such as Notepad or TextMate, but it has some specific features for typing in source code. To
open the file editor in IDLE, select File4New Window.

Your First Program with Python
Your First Program with Python

The window that appears should contain a cursor awaiting your input, but it’s different from the interactive shell, which runs Python instructions as soon as you press enter. The file editor lets you type in many instructions, save the file, and run the program. Here’s how you can tell the difference
between the two:

  • The interactive shell window will always be the one with the >>> prompt.
  • The file editor window will not have the >>> prompt.


Now it’s time to create your first program! When the file editor window opens, type the following into it:

  1. # This program says hello and asks for my name.
  2. v print('Hello world!')
  3. print('What is your name?') # ask for their name
  4. w myName = input()
  5. x print('It is good to meet you, ' + myName)
  6. y print('The length of your name is:')
  7. print(len(myName))
  8.  
  9. print('What is your age?') # ask for their age
  10. myAge = input()
  11. print('You will be ' + str(int(myAge) + 1) + ' in a year.')


Once you’ve entered your source code, save it so that you won’t have to retype it each time you start IDLE. From the menu at the top of the file editor window, select File4Save As. In the Save As window, enter hello.py in the File Name field and then click Save.

You should save your programs every once in a while as you type them. That way, if the computer crashes or you accidentally exit from IDLE, you won’t lose the code. As a shortcut, you can press ctrl-S on Windows and Linux or z-S on OS X to save your file.

Once you’ve saved, let’s run our program. Select Run4Run Module or just press the F5 key. Your program should run in the interactive shell window that appeared when you first started IDLE. Remember, you have to press F5 from the file editor window, not the interactive shell window.

Enter your name when your program asks for it. The program’s output in the interactive shell should look something like this:

  1. Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit
  2. (AMD64)] on win32
  3. Type "copyright", "credits" or "license()" for more information.
  4. >>> ================================ RESTART ================================
  5. >>>
  6. Hello world!
  7. What is your name?
  8. Al
  9. It is good to meet you, Al
  10. The length of your name is:
  11. 2
  12. What is your age?
  13. 4
  14. You will be 5 in a year.
  15. >>>

When there are no more lines of code to execute, the Python program terminates; that is, it stops running. (You can also say that the Python program exits.)

You can close the file editor by clicking the X at the top of the window. To reload a saved program, select File4Open from the menu. Do that now, and in the window that appears, choose hello.py, and click the Open button.

Your previously saved hello.py program should open in the file editor window.


0 Response to "Your First Program With Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel