This tutorial takes you through the beginning steps of programming using the Python programming language. You'll learn:
Applications | System | Terminal Program 2. There are two ways to use the Python interpreter. At the Linux prompt, you can enter: python somefile.py and this will run the program code in the file somefile.py. You can also just enter "python" at the prompt in the terminal window. This will start the Python interactive interpreter. With the interpreter, you can try individual Python commands and get feedback after each one. The interactive interpreter is used to try things out and learn Python. You can exit the Python interpreter, and get back to the Linux prompt, by entering control-D. 3. When you enter 'python' you should see the python prompt >>>. This is asking you to enter python commands. Type in the following lines to see what happens (hit return after each line) print hello 8*3 x = 7 y = 9 x*y 4. Now let's write a program in a file, and run the entire program at once. To do this, we'll use the Python interpreter, but not in interactive mode. Instead, when we run 'python', we'll provide it with the name of the file containing our program. First, open a text editor (e.g., on USF Computers, choose Applications | Accessories | Text Editor). In the new file, enter the following code all the way to the left (don't indent): principal = 3000 4. Save your program and name it interestRate.py. You must give the '.py' extension to your Python programs.interestRate = .1 oneYearInterest = interestRate*principal print 'interest is:', oneYearInterest 5. Go back to your Linux Terminal Window where you were playing around with the Python Interpreter. If that interpreter is still running, you'll see the >>> prompt. Stop the interpreter by pressing control-D. This should get you back to the Linux prompt. Now run your new python program by entering the following command: python interestRate.py This runs the Python interpreter and the program you just wrote. You should see the output: interest is 300.0 6. Now go back to the text editor and modify your program so that it asks the end-user for the principle and interestRate. You can ask the end-user of your program for data with the 'input' function. For example, principal = input('please enter the principal:') When an 'input' statement is executed, the user is prompted to enter a value, and that value is put in the variable on the left-hand side of the statement. You'll need a similar statement to get the interestRate from the user. 7. Go back to the terminal window and re-run your program. This time, the user (you) should be prompted to enter the principal and interest rate, and the one year interest printed should be dependent on what the user entered. 8. Let's make one more change to your program. Instead of printing out the interest, print out the principal that is accrued after one year and two years. You'll have to calculate these based on the original principal and the interest made each year. Make these changes in the editor, then re-run the program to test it. 9. If you've set-up a Google Sites portfolio page for Program Samples, document this lab there. Document it in this way:
|