- Using
**IDLE** **Basics**: variables, operators and assignments**Python Programs**
IDLE is a tool for interacting with Python and writing Python programs. You will find IDLE convenient for a number of reasons: - it gives the appropriate indentation
- it uses colors for reserved words, "strings" etc..
- it is easy to create a .py file
- it runs on your own machine, can be downloaded from http://www.python.org/download/
- let's take a tour: Python was installed in our lab
*(programs->Programming->Python2.7->IDLE).*
>>>print "first Python lab" >>># display welcome message
>>>x=5
>>>print x >>>y = x+5 >>>print x,y >>>x = 8 >>>print x,y
>>>print x/y >>>z = 3 >>>print x/z >>>t = 3.0 >>>print x/t
for <variable> in <sequence>:
n = input("Enter an integer ") i = 1 for j in range(n): i = i * (j+1) print i What does the program compute?
To write and test the program, do the following: - Write the program in the editor window of IDLE (File->New).
- Save it as mypower.py (for 1) and change.py (for 2).
- Run it by using Run->Run Module or the F5 key. Try running the programs using different inputs to make sure that the programs work correctly.
1. For loop practice: refer to the for loop example above. Write a simple Python program that prompts the user for inputs x and y and computes "x to the power of y". Assume that we've lost the ** function so you cannot use it. Also assume that both inputs (x and y) are integers. 2. (optional) Write a simple program to solve a practical problem.Recall the changeAdder example shown in lecture. We will do the reverse in this lab: given an amount in cents that is less than a dollar, determines the method of giving the amount using the fewest coins. The result should be given in the form shown below (pennies first, then nickels, dimes, and quarters, each on a separate line). Enter the total amount in cents (< 100): 33 Your program should use the input function to get the total amount of change from the user, then perform the necessary computations and output the result as shown above. Your program does not need to handle problematic inputs; it may assume that the number entered by the user is a positive integer less than 100. You may first use some examples to figure out the algorithm, i.e. if this problem were in your mathematics homework instead of computer science, what would you do? Then you can write pseudocode for the algorithm, and translate it to a Python program. |
||