CSCI 110, Spring 2011
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. Introduction to Computer Science Imperative vs. Declarative knowledge Abstraction (Black box abstraction, Layers of Abstraction) Modularity Problem solving: Divide and conquer Machine language vs. High level languages (Compiler) Programming Life Cycle Definition of an Algorithm 2. 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, long integer, floating point, string) Arithmetic expressions (including integer division) Strings Classes and objects Object properties Object methods 3. 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) 4. 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) 5. Text manipulation Concatenating strings Finding the length of a string Accessing a character at a given position in the string. Accessing a substring of a string 6. 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 7. Arithmetic expressions Precedence rules for +, - , * , / Integer division The MOD operator for integers Formatting floating point numbers 8. Strings Operations with strings string methods 9. Lists Accessing values in the list Operations with lists List methods 10. 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 for loop Using for loops with lists Changing 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)Consider a program that does the following: Prompt the user to input a number between 0 and 100. Input the number from the keyboard If the number is greater than 50 and less than 100 write: XX is in the top 50 percent. where XX refers to the number inputted by the user. Otherwise, if the number is less than or equal to 50 write: XX is in the bottom 50 percent. Otherwise, write Illegal number entered. a) Draw a decision tree for the program. b) Write a Python program to execute the program 3) Compute the value of the following expressions: a) 6 / 3 * 2 b) (17 - 11) / 3 + 3 c) Add parentheses to part b so that the addition is performed before the division. d) What is the value of c) ?
4) What is the value of sum after the execution of the following code?
sum = 0 for counter in range(1, 5): sum = sum + 2 * counter
5) 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 partial6) 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 = "csci110" 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
7) Consider the following while loop: sum = 0 count = 5 while count <= 10: sum = sum + count count = count + 1
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.
8) 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?
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 1000 deposits made.
#Program deposit.py depositList = [ ] sum = 0 #your code goes here print "The sum of the deposits: $%6.2f"% sum