Lab 5 - Python Fundamentals


Objectives
  1. Using IDLE
  2. Basics: variables, operators and assignments
  3. Python Programs

IDLE

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).

Variables, Operators and Assignment Statements

>>>print "first Python lab"

>>># display welcome message

This was a comment, which 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.

>>>x=5

x is a variable.. We assigned it a value. Please read the following lines and try to figure out the outputs without typing them in.

>>>print x

>>>y = x+5

>>>print x,y

>>>x = 8

>>>print x,y

Now check your answers above. What does an assignment statement do?

>>>print x/y

>>>z = 3

>>>print x/z

>>>t = 3.0

>>>print x/t

 


For Loops

for <variable> in <sequence>:

<statements>

Example:

n = input("Enter an integer ") 
i = 1 
for j in range(n):
    i = i * (j+1)
print i
What does the program compute?  

Python Programs

To write and test the program, do the following:

  1. Write the program in the editor window of IDLE (File->New).
  2. Save it as mypower.py (for 1) and change.py (for 2).
  3. 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
Here's the change that uses the fewest coins:
    pennies: 3
    nickels: 1
    dimes: 0
    quarters: 1

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.


CS105
CS105 Labs