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:
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:
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:
Writing your first Python program
To write your first Python program through IDLE, do the following:
To run your first Python program through Idle, do the following:
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:
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:
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:
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:
- Python 3.4.1 Tutorial
- Loyola University Chicago: Hands-on Python Tutorial
- Wikibooks: Non-Programmers Python Tutorial
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.
- 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
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:
- Click Applications Menu (a white-mouse icon at the upper left of the screen) to display the Applications Menu.
- Click Ubuntu Software Center to display its window, type python into the search field, and then select IDLE (using Python 3.4).
- Click Install to display the Authenticate window, type your password, and then click Authenticate.
- After you have installed IDLE, close the Ubuntu Software Center window.
Writing your first Python program
To write your first Python program through IDLE, do the following:
- Install IDLE according to the section above.
- Create a folder in which to store your Python programs. For example, you can create a MyPython folder within your home folder.
- 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 - Click File, and then click New Window to display an Untitled program window.
- 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 - Click File, click Save as, select your MyPython folder, type a file name such as hello.py, and then click Save.
- Close both IDLE windows, which are the Python Shell and the program window.
- Test your program by running it according to the section below.
To run your first Python program through Idle, do the following:
- Click Applications, select Programming, and then click IDLE (using Python 3.4) as shown in Figure 1.
- 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.
- 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 - Close both IDLE windows, which are the Python Shell and the program window.
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
- 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 |
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:
- 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.
- Click File, and then click New Window to display an Untitled program window.
- 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.
- 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.
- 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.
- Run the program multiple times, experimenting with different values for the x and y variables.
Figure 6 - Example Python while loop |
To write and run the Example While Loop program, do the following:
- 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.
- Click File, and then click New Window to display an Untitled program window.
- 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.
- 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.
- 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.
- Run the program multiple times, experimenting with different values for the t, m, and v variables.
Figure 7 - Example Python nested while loops |
To write and run the Example Nested While Loops program, do the following:
- 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.
- Click File, and then click New Window to display an Untitled program window.
- 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.
- 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.
- 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.
- Run the program multiple times, experimenting with different values for the i, p, t, m, and v variables.
No comments :
Post a Comment