110 likes | 248 Views
CSC 131 - Introduction to Computer Science I. Review Question for test 3. Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403 simmondsd@uncw.edu http://www.uncw.edu/people/simmondsd/
E N D
CSC 131 - Introduction to Computer Science I Review Question for test 3 Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403 simmondsd@uncw.edu http://www.uncw.edu/people/simmondsd/ _____________________________________________________________ ..
Strings • Answer the following questions. Where an error occurs, state how the error can be fixed. • Assume: ch = “TEST”, ch1 = “A”, num1 = 7 • What does this code produce: print(chr(Ord(ch1))) • Which of the following is not an escape sequence: • \\ • \t • \r • \p • None of the above
Strings • Assume: num1 = 7 five = '5' • What does this code produce: print("There are " + num1 + " days in a week") • What does this code produce: print("I am multiplying 5 by 7 to get: ", five * num1) • What does this code produce: • num = input("Enter two numbers").split() • #The user enters: 1.5 4.0 • print(num[0] * num[1])
Strings • What does this code produce: • a = "1234" • b = "4cats" • c = 12345 • x = "123".isdigit() • print(x) • y = a.isdigit() • print(y) • z = b.isdigit() • print(z) • w = c.isdigit() • print(w)
Strings • 1. What does this code produce: • print("JohnGov".isalpha()) • print("John Gov".isalpha()) • print("John4Gov".isalpha()) • 2. What does this code produce: • x = "In the beginning God created the heavens and the earth." • print(x.startswith ("In the beginning")) • print(x.startswith ("in")) • print(x.endswith ("Earth")) • 3. What does this code produce: • print("To be or not to be".replace("be", "was")) • print("To be or not to be".replace("be", "was", 1))
Strings • 1. What does this code produce: • x = "In the beginning God created the heavens and the earth." • print(x.find ("In the beginning")) • print(x.find ("in")) • print(x.find ("Earth")) • 2. What does this code produce: • x = "In the beginning God created the heavens and the earth. And the earth was …" • print(x.rfind ("In the beginning")) • print(x.rfind ("in")) • print(x.rfind ("earth")) • 3. What does this code produce: • x = "In the beginning God created the heavens and the earth." • print(x.count ("In the beginning")) • print(x.count ("in")) • print(x.count ("earth"))
Lists • Write code to show four different ways to create a list of ten values. • Write code to search the list numbers for the value of variable x and the print a message indicating found or not found. • What does this code produce: x = [1,3,2,3,4,5,4,6,7,8,9] x.insert(2, 44) print(x) x.remove(4) print(x) print(x.pop(1), "---", x) print("To be or not to be, or not to be to be.".split("to"))
Lists • Other topics • List slicing • Copying lists
Lists • Default arguments: How do these two functions differ? def add(x, lst = []): if not(x in lst): lst.append(x) return lst def add(x, lst= None): if(lst == None): lst = [] if not(x in lst): lst.append(x) return lst
Files • How these functions work • open close, read, readline, readlines, write
Qu es ti ons? The End ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________