Exam II Review


Here are a collection of questions that are representative of the type of questions that may appear on the exam.  This  should not be all you study.  Go over the twenty questions we used for the quiz program.  You need to go over the powerpoints and homeworks as well. You may bring a printed copy of the Python Book to the exam if you like.  You could print only the relevant chapters if you care to. Any handwritten scribbling that you have added to the book is OK.  Practice the reviewed python operations on the Computer so you KNOW that they work.

Python Iteration needs to practice.  Be sure you can write a while statement and a loop statement.  I will ask you to convert one to the other.

0.  Review the meaning of range(). Remember you can use range(3), range(1,10)  and range(1,30,2).  It returns a list!
1.  Write a single short piece of python code that will do the following. 
Do it first using a while loop and then using a for loop.
a) print the even numbers from 2 thru 100
b) print the characters in a string s='Reality, what a concept' , one per line.
c) print the characters in a string s, backwards on the same line separated by a space.
d) add the numbers from 1 to 100 and print out the answer.

Stings are important.  Review string access and slicing.
2.  If s ="Now is the time for all good men to come to the aid of their country"
  a) print the text 'good men' using slicing of s.
  b) print the text from 'time' till the end of the string, using slicing
  c) create the string 'for all men' from s using slicing and concatenation.
  d) use split() to break apart s and print the words, one per line.

3. write a program that will read a number from the keyboard and if it is even  print out the number squared otherwise print the number cubed.

4. write a program that will first read in a sentence as a string and then second read in a word as a string.  If the word is in the sentence print 'The word is in the sentence' otherwise print 'Word Hello is not in the sentence'.  Print the word entered in the place of Hello.

5. Write a program that reads in an integer N from the keyboard and then loops thru the values 0 ,1,..N printing whether or not each number is divisible by 2, divisable by 3 or both.  Example output format if 10 is entered.  Use for, if - else and % to do this one
   0   Divisible by 2 and 3
   1   Not Divisible
   2   Divisible by 2
   3   Divisible by 3
   4   Divisible by 2
   5   Not Divisible
   6   Divisible by 2 and 3
   . . .
   10  Divisible by 2

6.  What does the following print?
    x = 1
    y = 1
    s = 2
    print 1,1,
    while s <= 15:
      print s,
      s = x + y

7. Write a program that will read in two positive integers that represent the lengths of the two legs of a right triangle.  Print out the length of the hypotenuse.

8. Write a program that print reads in a name of the form lastname firstname and prints out
    Firstname LASTNAME where the last name is all caps and the first name is capitalized. It should work for any name typed in. For example if s ='simpson richard' then print out Richard SIMPSON.
    HINT: split the string and use methods .upcase() and .capitalize()

9.  Recall that split() returns a list.  Suppose that s='2 3 5 1 7 6 3'
     Write a program that starts with s='1 5 4 3 7 6 10 23' then splits it into a list of number strings, converts these to integers and then finds and prints the sum. 

11.  Write a program that will read in integers from the keyboard and put them into a list using .append(). Stop when the user inters a 0.  Now write a loop that traverses the numbers in the list and adds up the odd numbers.  Print out that sum.

12.  Write a short program that graphs the curve y=x**3-10*x**2+5*x+2 from -10 to 10 in steps of 1 using matplotlib. Use import matplotlib.pyplot as plt to begin the program.

Comments are closed.