CS105
LAB 5: Python


Objectives
  1. Using IDLE
  2. Basics: output, variables, operators
  3. Python Programs, including the use of for loops


IDLE

IDLE is a tool for interacting with Python and writing Python programs. You will find IDLE convenient for a number of reasons:

 


Output - Variables - Operators

>>>print "best course: cs105 "

>>>#print "best course: cs105"

this was a comment, it is always ignored by the interpreter..

a program will be most commonly ignored by people if it has no comments that explain what it is that it does

>>>print "i can do fast math!"

>>>print 10 **10

>>>c=5

c is a variable.. we assigned it a value

>>>print c

>>>c= c+1

>>>d=c

d is another variable, we assigned it the value of c.. what happens if we change the value of c; does d change?

 

together with 6 friends of yours you decided to buy a car. Your name is 0, and your friends are named 1,2,3,4,5 and 6.

You made a deal: each person gets the car for a day in a round-robin fashion... that is:

0 gets the car in day 0 (the day it was bought..)

1 gets the car in day 1

...

6 gets the car in day 6

0 gets the car in day 7

...

Who gets the car after 23984 days? (if it is still working..)


Python Programs

Here is a simple program:

print "Hello"
print "How are you?"
  1. Write this program in the editor window of IDLE (File->New).
  2. Save it as greet.py.
  3. Run it by using Run->Run Module or the F5 key.

Here's is a pseudo-code for calculating the Future Value of an investment principal at interest rate apr:


get principal
get apr
get years
for i = 1..years
    principal = principal * (1+apr)
output the future value 


Convert the above pseudo-code to a python program and run using IDLE


Exercise

We lost the ** function...

Write a simple Python program that prompts the user for inputs x and y and computes "x to the power of y". Make sure your program is user friendly! We've lost the ** function so you cannot use it ... but at least we still have for loops!

To get started, first define the inputs and the outputs of your program and then start implementing. Use the program above as an example!


CS105