MONT 105S, Spring 2009
Home | | Course Schedule | | Assignments | | Lecture NotesThe following topics have been covered in the first part of the course. Each of the following topics may appear on the exam.
1. Python programs General form of Python program Input and Output (print, input() and raw_input()) Variables Rules for naming variables Assigning values to variables Data types (integer, floating point, string) Arithmetic expressions (including integer division) Strings Classes and objects Object properties Object methods 2. Conditional Statements Decision Trees General form of conditional statement Using if ... elif ... else Nested conditionals Logical Expressions Relational operators (<, >, etc.) Logical operators (and, or, not) 3. Using Turtles to draw Naming a Turtle object Using Turtle methods: forward(distance) backward(distance) left(angle) right(angle) up( ) down( ) goto(x, y) circle(radius) 4. while loops General form of a while loop Using a counter in a while loop Keeping track of variable values in a loop Using compound conditions 5. Strings Concatenating strings Finding the length of a string Accessing a character at a given position in the string. Accessing a substring of a string Operations with strings string methods 6. Lists Accessing values in the list Operations with lists List methods 7. for loops General form of a for loop Execution of a for loop Using range( ) to create a list of integers Writing a count-controlled while loop as a for loop Using for loops with lists Changiing values of a list with a for loop
Practice Problems
The following problems are intended to help you study for the exam.
1) A boolean is a data type that has a value of True or False. Given these boolean variables: x = True, y = False, z = True What is the value (True/False) of each of the following? i) x and (y or z) ii) (not x) or (not(y and z)) iii) y or (x and not z)2)Write a program that does the following:
Prompt the user to input a number. Input the number from the keyboard If the number is less than 100, write: XX is a low number. where XX refers to the number inputted by the user. Otherwise, write: XX is a high number.
3) a) What is the value of sum after the execution of the following code?
sum = 0 counter = 1 while counter < 6: sum = sum + 2 * counter counter = counter + 1 print sum
b) Re-write the above code using a for loop instead of a while loop
4) What is the output of the following code fragment (mySentence and partial are both strings)?
mySentence = "It was a dark and stormy night" partial = mySentence[10:13] print partial5) What is wrong with the following fragments of Python?
#Program Error1 number = raw_input("Please enter a number: ") number = number/2.0 print number
#Program Error2 response = raw_input("Do you like chocolate? ") if response == "yes": print "Me too!" else: print "That's too bad."
#Program Error3 secret = "mont105S" response = raw_input("Enter the password: ") if response = secret: print "Access granted." else: print "Access denied.
#Program Error4 count = 0 while count < 10 print count count = count + 1
6) Consider the following while loop: sum = 0 count = 5 while count <= 12: sum = sum + count count = count + 2
a) Fill in the following table for the values of count and sum each time through the loop (there may be more rows than you need in the table):
b)Write a for loop that does the equivalent of the above loop.
7) Consider the following Python code:
sum = 0 for i in range(1, 5): sum = sum + i print sumWhat is value of sum printed on the screen?
8) Fill in/write the necessary code for the following decision tree.
#program catCheck.py #Your code goes here
9) Fill in the necessary code to fill in the depositList array with deposit amounts entered by the user. You should keep track of the sum of all the deposits made, so that the print statement at the end of the program makes sense. You can assume there are exactly 100 deposits made.
#Program deposit.py depositList = [ ] sum = 0 #your code goes here print "The sum of the deposits:", sum