10/14/2014

Getting started with Python programming

Python is a powerful, high-level, object-oriented programming language. It's free to download and install because it's open-source software. In comparison with other programming languages, it has a simpler, more-intuitive syntax. Therefore, as this document shows, Python is easy to learn and fun to use. For more information, see the following Python tutorials:
You can choose either of two Python versions, 2 or 3. Python 2 is primarily a legacy version. Therefore, you should probably choose version 3 unless you specifically need Python 2. For more information, see Should I use Python 2 or Python 3?

Python is probably already installed on your computer. If not, you can install it according to information in Python Setup and Usage. Currently, in October, 2014, Python 3.4 is the newest version.

Note: Though tested on Xubuntu 14.04, the procedures in this document generally apply to Python on all PC operating systems; Linux, OS X, and Windows. Xubuntu is an XFCE-desktop distribution of the Ubuntu Linux operating system.

This document can help you:
  • Install IDLE, which is an Integrated Development Environment (IDE) for Python.
  • Write your first Python program.
  • Run your first Python program.
  • Run example Python programs that demonstrate:
    • Math operations
    • While loops
    • Nested while loops
Installing IDLE
Although you can program Python through your command-line terminal, programming through IDLE is easier. Install IDLE according to your particular operating system. For example, to install IDLE on Xubuntu 14.04, do the following:
  1. Click Applications Menu (a white-mouse icon at the upper left of the screen) to display the Applications Menu.
  2. Click Ubuntu Software Center to display its window, type python into the search field, and then select IDLE (using Python 3.4).
  3. Click Install to display the Authenticate window, type your password, and then click Authenticate.
  4. After you have installed IDLE, close the Ubuntu Software Center window.
Note: The Ubuntu Software Center has a nice feature that can display the path to a software package you have just installed. For example, after you install IDLE (using Python-3.4), you can click More info to display Applications > Programming > IDLE (using Python-3.4). Of course, this applies only to software that you can launch through the Applications Menu.

Writing your first Python program
To write your first Python program through IDLE, do the following:
  1. Install IDLE according to the section above.
  2. Create a folder in which to store your Python programs. For example, you can create a MyPython folder within your home folder.
  3. As shown in Figure 1, click the white-mouse Applications Menu, select Development, and then click IDLE (using Python 3.4) to display a Python Shell window as shown in Figure 2.
    Figure 1 - Starting IDLE
    Figure 2 - Python Shell
  4. Click File, and then click New Window to display an Untitled program window.
  5. Type a Python program, such as the Example Python Hello code shown in Figure 3. Note: To enter a comment into a Python program, type # before the comment text. Computers do not execute comments. The Python interpreter ignores all comments while converting your program statements to machine code.
    Figure 3 - Writing your first Python program
  6. Click File, click Save as, select your MyPython folder, type a file name such as hello.py, and then click Save.
  7. Close both IDLE windows, which are the Python Shell and the program window.
  8. Test your program by running it according to the section below.
Running your first Python Program
To run your first Python program through Idle, do the following:
  1. Click Applications, select Programming, and then click IDLE (using Python 3.4) as shown in Figure 1.
  2. In the Python Shell, click File, click Open, select your MyPython folder, select the program you want to run, such as hello.py, and then click Open to display the program window.
  3. Click Run, and then click Run Module to run your program. It displays its output in the Python Shell as shown in Figure 4.
    Figure 4 - Testing your first Python program
  4. Close both IDLE windows, which are the Python Shell and the program window.
Running example Python programs
After writing and running your first Python program according to the sections above, you can copy and run other example Python programs, such as those that demonstrate:
  • Math operations
  • While loops
  • Nested while loops
Running example Python math operations - The Example Python program in Figure 5 demonstrates a few Python math operations and functions:
  • Included math operations let you enter two variables for:
    • Addition
    • Subtraction
    • Multiplication
    • Division
    • Raising a value by an exponent
    • Finding the root of a value
  • Included functions are:
    • float(), which defines a decimal value function
    • input(), which lets a user enter a value
    • print(), which displays data on a computer screen
Figure 5 - Example Python math operations
To enter a value for a variable, you can use an input() function nested within a float() function. An input() function can contain text in quotes to prompt data entry. The nested float(input()) function defines its data as a decimal value. For example, if you type 3, the data is 3.0.

The print() function displays data in a window, it does not print on paper. The print() function can display text in quotes, one or more variables, or both. Additionally, the print() function can contain and display a math operation, such as x+y.

To write and run the Example Python Math Operations program, do the following:
  1. As shown in Figure 1, click the white-mouse Applications Menu, select Development, and then click IDLE (using Python 3.4) to display a Python Shell window as shown in Figure 2.
  2. Click File, and then click New Window to display an Untitled program window.
  3. Type the Example Python Math Operations code shown in Figure 5. Note: As a new programmer, you can learn more by typing all this code. However, as an alternative, you can copy the code from XmplMathOperations and then paste it into the program window.
  4. To save your program for later use, click File, click Save as, select your MyPython folder, type a file name, such as xmplmath.py, and then click Save.
  5. In the program window, click Run, and then click Run Module to run your program in the Python Shell window. While your program is running, follow its prompts to enter data and/or make selections.
  6. Run the program multiple times, experimenting with different values for the x and y variables.
Running an example Python while loop - The Example Python program in Figure 6 demonstrates the Python while and if statements.
Figure 6 - Example Python while loop
Variable t is the total. Variable m is maximum total limit. The while statement defines the start of the while loop, which loops while t<=m (the total is less than the limit). Each time the loop runs, you can add a value to the total. As soon as the total exceeds its limit, the if statement ends the loop, and stops printing the Current Total.

To write and run the Example While Loop program, do the following:
  1. As shown in Figure 1, click the white-mouse Applications Menu, select Development, and then click IDLE (using Python 3.4) to display a Python Shell window as shown in Figure 2.
  2. Click File, and then click New Window to display an Untitled program window.
  3. Type the Example Python While Loop code shown in Figure 6. Note: As an alternative, you can copy the code from XmplWhileLoop and then paste it into the program window.
  4. To save your program for later use, click File, click Save as, select your MyPython folder, type a file name, such as xmplwhile.py, and then click Save.
  5. In the program window, click Run, and then click Run Module to run your program in the Python Shell window. While your program is running, follow its prompts to enter data and/or make selections.
  6. Run the program multiple times, experimenting with different values for the t, m, and v variables.
Running example Python nested while loops - The Example Python program in Figure 7 demonstrates how to run one while loop within another. The outer while loop is new. The inner while loop is the same as the Example While Loop in the section above.
Figure 7 - Example Python nested while loops
The variable i counts how many passes the outer loop has made. The variable p defines how many passes the outer loop can make. The int() function defines p as an integer value (no decimal point). Within each outer loop pass, the inner loop runs while t<=m (the total is less than the limit). Each time the inner loop runs, you can add value to the total. As soon as the total exceeds its limit, the if statement ends the inner loop, and stops printing the Current Total. The outer loop stops running after i is equal to or greater than p.

To write and run the Example Nested While Loops program, do the following:
  1. As shown in Figure 1, click the white-mouse Applications Menu, select Development, and then click IDLE (using Python 3.4) to display a Python Shell window as shown in Figure 2.
  2. Click File, and then click New Window to display an Untitled program window.
  3. Type the Example Python While Loop code shown in Figure 7. Note: As an alternative, you can copy the code from XmplNestedWhileLoops and then paste it into the program window.
  4. To save your program for later use, click File, click Save as, select your MyPython folder, type a file name, such as xmplnestedwhile.py, and then click Save.
  5. In the program window, click Run, and then click Run Module to run your program in the Python Shell window. While your program is running, follow its prompts to enter data and/or make selections.
  6. Run the program multiple times, experimenting with different values for the i, p, t, m, and v variables.

















No comments :

Post a Comment