Home
21.
Why might you use a dictionary instead of a list?

Dictionaries are generally better when the data is labeled (a record with field names, for example); lists are best suited to collections of unlabeled items (such as all the files in a directory). Dictionary lookup is also usually quicker than searching a list, though this might vary per program

32.
How can you determine how large a tuple is? Why is this tool located where it is?

The built-in len function returns the length (number of contained items) for any container object in Python, including tuples. It is a built-in function instead of a type method because it applies to many different types of objects. In general, builtin functions and expressions may span many object types; methods are specific to a single object type, though some may be available on more than one type (index, for example, works on lists and tuples).

33.
What is the default for the processing mode argument in a file open call?

The default for the processing mode argument in a file open call is 'r', for reading text input. For input text files, simply pass in the external file’s name.

34.
What module might you use to store Python objects in a file without converting them to strings yourself?

The pickle module can be used to store Python objects in a file without explicitly converting them to strings. The struct module is related, but it assumes the data is to be in packed binary format in the file; json similarly converts a limited set of Python objects to and from strings per the JSON format.

35.
How might you go about copying all parts of a nested structure at once?

Import the copy module, and call copy.deepcopy(X) if you need to copy all parts of a nested structure X. This is also rarely seen in practice; references are usually the desired behavior, and shallow copies (e.g., aList[:], aDict.copy(), set(aSet)) usually suffice for most copies.

36.
When does Python consider an object true?

An object is considered true if it is either a nonzero number or a nonempty collection object. The built-in words True and False are essentially predefined to have the same meanings as integer 1 and 0, respectively.

37.
How is a statement normally terminated in Python?

The end of a line terminates the statement that appears on that line. Alternatively, if more than one statement appears on the same line, they can be terminated with semicolons; similarly, if a statement spans many lines, you must terminate it by closing a bracketed syntactic pair.

38.
How are the statements in a nested block of code normally associated in Python?

The statements in a nested block are all indented the same number of tabs or spaces.

39.
How can you make a single statement span multiple lines?

You can make a statement span many lines by enclosing part of it in parentheses, square brackets, or curly braces; the statement ends when Python sees a line that contains the closing part of the pair.

40.
How can you code a compound statement on a single line?

The body of a compound statement can be moved to the header line after the colon, but only if the body consists of only noncompound statements.