Python essential questions

Ace your homework & exams now with Quizwiz!

if vs while statement

The while loop is similar to an if statement: it executes the code inside of it if some condition is true. The difference is that the while loop will continue to execute as long as the condition is true. In other words, instead of executing if something is true, it executes while that thing is true.

Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are: (a) even numbers, and (b) from elements in the original list that had even indices For example, if list[2] contains a value that is even, that value should be included in the new list, since it is also at an even index (i.e., 2) in the original list. However, if list[3] contains an even number, that number should not be included in the new list since it is at an odd index (i.e., 3) in the original list.

A simple solution to this problem would be as follows [x for x in list[::2] if x%2 == 0] For example, given the following list: # 0 1 2 3 4 5 6 7 8 list = [ 1 , 3 , 5 , 8 , 10 , 13 , 18 , 36 , 78 ] the list comprehension [x for x in list[::2] if x%2 == 0] will evaluate to: [10, 18, 78] The expression works by first taking the numbers that are at the even indices, and then filtering out all the odd numbers.

What will be the output of the code below in Python 2? Explain your answer. def div1(x,y): print "%s/%s = %s" % (x, y, x/y) def div2(x,y): print "%s//%s = %s" % (x, y, x//y) div1(5,2) div1(5.,2) div2(5,2) div2(5.,2.) Also, how would the answer differ in Python 3 (assuming, of course, that the above print statements were converted to Python 3 syntax)?

In Python 2, the output of the above code will be: 5/2 = 2 5.0/2 = 2.5 5//2 = 2 5.0//2.0 = 2.0 By default, Python 2 automatically performs integer arithmetic if both operands are integers. As a result, 5/2 yields 2, while 5./2 yields 2.5. Note that you can override this behavior in Python 2 by adding the following import: from __future__ import division Also note that the "double-slash" (//) operator will always perform integer division, regardless of the operand types. That's why 5.0//2.0 yields 2.0 even in Python 2. Python 3, however, does not have this behavior; i.e., it does not perform integer arithmetic if both operands are integers. Therefore, in Python 3, the output will be as follows: 5/2 = 2.5 5.0/2 = 2.5 5//2 = 2 5.0//2.0 = 2.0

What will be the output of the code below? list = ['a', 'b', 'c', 'd', 'e'] print list[10:]

The above code will output [], and will not result in an IndexError. As one would expect, attempting to access a member of a list using an index that exceeds the number of members (e.g., attempting to access list[10] in the list above) results in an IndexError. However, attempting to access a slice of a list at a starting index that exceeds the number of members in the list will not result in an IndexError and will simply return an empty list. What makes this a particularly nasty gotcha is that it can lead to bugs that are really hard to track down since no error is raised at runtime.

1. list = [ [ ] ] * 5 2. list # output? 3. list[0].append(10) 4. list # output? 5. list[1].append(20) 6. list # output? 7. list.append(30) 8. list # output? What will be the ouput of lines 2, 4, 6, and 8? Explain your answer.

The output will be as follows: [[], [], [], [], []] [[10], [10], [10], [10], [10]] [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]] [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30] Here's why: The first line of output is presumably intuitive and easy to understand; i.e., list = [ [ ] ] * 5 simply creates a list of 5 lists. However, the key thing to understand here is that the statement list = [ [ ] ] * 5 does NOT create a list containing 5 distinct lists; rather, it creates a a list of 5 references to the same list. With this understanding, we can better understand the rest of the output. list[0].append(10) appends 10 to the first list. But since all 5 lists refer to the same list, the output is: [[10], [10], [10], [10], [10]]. Similarly, list[1].append(20) appends 20 to the second list. But again, since all 5 lists refer to the same list, the output is now: [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]]. In contrast, list.append(30) is appending an entirely new element to the "outer" list, which therefore yields the output: [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30].

Given the following subclass of dictionary: class DefaultDict(dict): def __missing__(self, key): return [] Will the code below work? Why or why not? d = DefaultDict() d['florp'] = 127

Yes, it will work. With this implementation of the DefaultDict class, whenever a key is missing, the instance of the dictionary will automatically be instantiated with a list.


Related study sets

BIOL 320-Aggie Honor System Rules- Definition

View Set

Elsevier adaptive quizzing- nutrition

View Set

Marketing Research Quizzes Test 2

View Set

Psych Chapter 24: Personality Disorders

View Set

Intellectual Property Rights: Copyrights

View Set

Boat Ed temporary boaters test answers

View Set