CSI TUESDAY!!!!!

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is a function in Python?

. A named sequence of statements.

What is printed by the following statements? alist = [1, 3, 5] print(alist * 3)

. [1, 3, 5, 1, 3, 5, 1, 3, 5]

What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist.append(True) alist.append(False) print(alist)

. [4, 2, 8, 6, 5, True, False]

list-4-1: What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[5])

3.14

What is printed by the following statements? alist = [3, 67, "cat", 3.14, False] print(len(alist))

5

What is a local variable?

A. A temporary variable that is only used inside a function: Yes, a local variable is a temporary variable that is only known (only exists) in the function it is defined in.

Consider the following code: def square(x): runningtotal = 0 for counter in range(x): runningtotal = runningtotal + x return runningtotal What happens if you put the initialization of runningtotal (the line runningtotal = 0) inside the for loop as the first instruction in the loop?

A. The square function will return x instead of x * x: The variable runningtotal will be reset to 0 each time through the loop. However because this assignment happens as the first instruction, the next instruction in the loop will set it back to x. When the loop finishes, it will have the value x, which is what is returned.

What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[4:])

A. [ [ ], 3.14, False] Yes, the slice starts at index 4 and goes up to and including the last item.

Which of the following is a valid function header (first line of a function definition)? A. def drawCircle(t): B. def drawCircle: C. drawCircle(t, sz): D. def drawCircle(t, sz)

A. def drawCircle(t):

What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist = alist + 999 print(alist)

B. Error, you cannot concatenate a list with an integer. Yes, in order to perform concatenation you would need to write alist+[999]. You must have two lists.

What is a variable's scope?

B. The range of statements in the code where a variable can be accessed.

Can you use the same name for a local variable as a global variable?

B. Yes, but it is considered bad form.

What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist[2] = True print(alist)

B. [4, 2, True, 6, 5]

What is the name of the following function? def drawSquare(t, sz): """Make turtle t draw a square of with side sz.""" for i in range(4): t.forward(sz) t.left(90)

B. drawSquare

What is printed by the following statements? alist = [4, 2, 8, 6, 5] temp = alist.pop(2) temp = alist.pop() print(alist)

C. [4, 2, 6] Yes, first the 8 was removed, then the last item, which was 5. pop() removes the last item, not the first.

What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = alist * 2 blist[3] = 999 print(alist)

C. [4, 2, 8, 6, 5] Yes, alist was unchanged by the assignment statement. blist was a copy of the references in alist.

What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = [ ] for item in alist: blist.append(item+5) print(blist)

C. [9, 7, 13, 11, 10] Yes, the for loop processes each item of the list. 5 is added before it is appended to blist.

What are the parameters of the following function? def drawSquare(t, sz): """Make turtle t draw a square of with side sz.""" for i in range(4): t.forward(sz) t.left(90)

C. t, sz

What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[2].upper())

CAT

What is printed by the following statements? alist = [4,2,8,6,5] blist = [num*2 for num in alist if num%2==1] print(blist)

D. [10]. Check MeCompare me Yes, 5 is the only odd number in alist. It is doubled before being placed in blist.

Considering the function below, which of the following statements correctly invokes, or calls, this function (i.e., causes it to run)? Assume we already have a turtle named alex. def drawSquare(t, sz): """Make turtle t draw a square of with side sz.""" for i in range(4): t.forward(sz) t.left(90)

E. drawSquare(alex, 10)

What is printed by the following statements? myname = "Edgar Allan Poe" namelist = myname.split() init = "" for aname in namelist: init = init + aname[0] print(init)

EAP Yes, split creates a list of the three names. The for loop iterates through the names and creates a string from the first characters.

func-1-2: What is one main purpose of a function?

To help the programmer organize programs into chunks that match how they think about the solution to the problem.

What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist.insert(2, True) alist.insert(0, False) print(alist)

[False, 4, 2, True, 8, 6, 5]

What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[2][0])

c Yes, the first character of the string at index 2 is c

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} keylist = list(mydict.keys()) keylist.sort() print(keylist[3])

elephant Yes, the list of keys is sorted and the item at index 3 is printed.

open: open(filename,'r'): Open a file called filename and use it for reading. This will return a reference to a file object. open: open(filename,'w'): Open a file called filename and use it for writing. This will also return a reference to a file object. close: filevariable.close(): File use is complete.

qbfile = open("qbdata.txt", "r") for aline in qbfile: values = aline.split() print('QB ', values[0], values[1], 'had a rating of ', values[10] ) qbfile.close()

What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = alist blist[3] = 999 print(alist)

. [4, 2, 8, 999, 5]

Consider the following Python code. Note that line numbers are included on the left. 1. def pow(b, p): 2. y = b ** p 3. return y 4 5. def square(x): 6. a = pow(x, 2) 7. return a 8. 9. n = 5 10. result = square(n) 11. print(result) Which of the following best reflects the order in which these lines of code are processed in Python?

1, 5, 9, 10, 5, 6, 1, 2, 3, 6, 7, 10, 11

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23} mydict["mouse"] = mydict["cat"] + mydict["dog"] print(mydict["mouse"])

18

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} answer = mydict.get("cat") // mydict.get("dog") print(answer)

2

Consider the following Python code. Note that line numbers are included on the left. 1. def pow(b, p): 2. y = b ** p 3. return y 4. 5 .def square(x): 6. a = pow(x, 2) 7. return a 8. 9. n = 5 10. result = square(n) 11. print(result) What does this function print?

25

What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist = alist.pop(0) print(alist)

4 Yes, first the 4 was removed from the list, then returned and assigned to alist. The list is lost.

What is printed by the following statements? total = 0 mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} for akey in mydict: if len(akey) > 3: total = total + mydict[akey] print(total)

43 Yes, the for statement iterates over the keys. It adds the values of the keys that have length greater than 3.

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23} print(mydict["dog"])

6

list-3-2: What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(len(alist))

7

What is printed by the following statements? alist = [ [4, [True, False], 6, 8], [888, 999] ] if alist[0][1][0]: print(alist[1][0]) else: print(alist[1][1])

888 Yes, alist[0][1][0] is True and alist[1] is the second list, the first item is 888.

: What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} yourdict = mydict yourdict["elephant"] = 999 print(mydict["elephant"])

999 Yes, since yourdict is an alias for mydict, the value for the key elephant has been changed.

infile = open("qbdata.txt", "r") line = infile.readline() while line: values = line.split() print('QB ', values[0], values[1], 'had a rating of ', values[10] ) line = infile.readline() infile.close()

Examle

list-2-1: A list can contain only integer items.

FALSE

What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(57 in alist)

FALSE: Yes, 57 is not a top level item in alist. It is in a sublist.

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} print(23 in mydict)

False Yes, the in operator returns True if a key is in the dictionary, False otherwise.

What will the following function return? def addEm(x, y, z): print(x + y + z)

None: We have accidentally used print where we mean return. Therefore, the function will return the value None by default. This is a VERY COMMON mistake so watch out! This mistake is also particularly difficult to find because when you run the function the output looks the same. It is not until you try to assign its value to a variable that you can notice a difference

True or false: A function can be called several times by placing a function call in the body of a loop.

TRUE

What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(3.14 in alist)

TRUE Yes, 3.14 is an item in the list alist.

A dictionary is an unordered collection of key-value pairs.

TRUE Yes, dictionaries are associative collections meaning that they store key-value pairs.

What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} print("dog" in mydict)

True Yes, dog is a key in the dictionary.

What is wrong with the following function definition: def addEm(x, y, z): return x + y + z print('the answer is', x + y + z)

You should not have any statements in a function after the return statement. Once the function gets to the return statement it will immediately stop executing the function.

What is printed by the following statements? alist = [1, 3, 5] blist = [2, 4, 6] print(alist + blist)

[1, 3, 5, 2, 4, 6]

What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = [alist] * 2 alist[3] = 999 print(blist)

[[4, 2, 8, 999, 5], [4, 2, 8, 999, 5]] Yes, blist contains two references, both to alist. [alist] * 2 creates a list containing alist repeated 2 times


Kaugnay na mga set ng pag-aaral

Chapter 14: Shock and Multiple Organ Dysfunction Syndrome

View Set

chapter 2 Concepts of Health, Illness, Stress, and Health Promotion

View Set

Ch 13: Personal Selling and Sales Promotion DSM

View Set

algebra 2b - unit 5: more than one function

View Set

Chapter 2 Section 2-2 Vocab & Review Questions

View Set

Chapter 10 Intellectual Property Law

View Set

Pharm Ch. 43 Anticoagulants, Antiplatelets, and Thrombolytics TB

View Set

Porth's CH 41 Disorders of Renal Function

View Set

ISSA Unit 14 TRAINING PRINCIPLES (Paul Taylor's)

View Set

Membrane Structure/Function AP Bio

View Set