Chapter 7, 8, 9, 11

Ace your homework & exams now with Quizwiz!

By default, the re module's sub function replaces only the first occurrence of a pattern with the replacement text you specify. The statement re.sub(r'\t', ', ', '1\t2\t3\t4') returns '1, 2\t3\t4' T/F

False

The following code creates a Series of student grades from a list of integers: Import pandas as pd grades = pd.Series([87, 100, 94]) by default, a Series has integer indexes numbered sequentially from 1, and the Series argument may be a tuple, a dictionary, an array, another Series or single value. T/F

False

The following code formats the float value 17.489 rounded to the hundredths position: '{:.3f}'.format(17.489) T/F

False

The following code replaces tab characters with commas: values = '1\t2\t3\t4\t5' values.replace('\t', ', ') T/F

False

Consider the following code: In [1]: s1 = 'happy' In [2]: s2 = 'birthday' In [3]: s1 += ' ' + s2 In [4]: s1 Out [4]: 'happy birthday' In [5]: symbol = '>' In [6]: symbol *= 5 In [7]: symbol Out[7]: '>>>>>' Which snippet(s) in this interactive session appear to modify existing strings, but actually create new string objects?

Both snippets [3] and [6]

Consider a series of hardware-related strings: Hardware = pd.series(['Hammer', 'Saw', 'Wrench"]) The following code calls a string method contains on each element to determine whether the value of each element contains a lowercase 'a': Hardware.str.contains('a') And returns a Series containing bool values indicating the contains method's result for each element. T/F

True

Assuming the following Dataframe grades: Wally Eva Sam Katie Bob Test1 87 100 94 100 83 Test2 96 87 77 81 65 Test3 70 90 90 82 85 To see the average of all the student's grades on each test, call mean on the T attribute: grades.T.mean() T/F

True

Assuming the following Dataframe grades: Wally Eva Sam Katie Bob Test1 87 100 94 100 83 Test2 96 87 77 81 65 Test3 70 90 90 82 85 Rather than getting the summary statistics by student, you can get them by test. Simply call describe on grades.T, as in: grades.T.describe() T/F

True

Based on the string: Sentence = '\t \n This is a test string. \t\t \n' The following code snippets first use method lstrip to remove only leading whitespace from sentence: Sentence.lstrip() Then use method rstrip to remove only trailing whitespace: Sentence.rstrip() T/F

True

Consider a Series of hardware-related strings: Hardware = pd.Series(['Hammer', 'Saw', 'Wrench']) The following code uses the Series str attribute to invoke string method upper on every Series element, producing a new series containing the uppercase versions of each element in hardware: hardware.str.upper() T/F

True

The following IPython session loads and displays the CSV file accounts.csv: in[1]: import pandas as pd in[2]: df = pd.read_csv('accounts.csv', names=['account', 'name', 'balance']) in[3]: df out[3]: account name balance 0 100 Jones 24.98 1 200 Doe 345.67 2 300 White 0.00 3 400 Stone -42.16 4 500 Rich 224.62 The names keyword argument specifies the DataFrame's column names. If you do not supply the names keyword argument, read_csv assumes that the CSV file's row is a comma-delimited list of column names. T/F

True

The following code creates and accounts.txt file and write five client records to the file. Generally, records in text files are stored one per line, so we end each record with a newline character: With open('accounts.txt', mode='w') as accounts: Accounts.write('100 jones 24.98\n') Accounts.write('200 Doe 345.67 \n') Accounts.write('300 White 0.00\n') Accounts.write('400 Stone -42.16\n') Accounts.write('500 Rich 224.62\n) You can also write to a file with print (which automatically outputs a \n), as in Print('100 Jones 24.98', file=accounts) T/F

True

In the following interactive sessions that compares the strings 'Orange' and 'orange': In [1]: 'Orange' == 'orange' Out[1]: False In[2]: 'Orange' != 'orange' Out[2]: ??? In [3]: 'Orange' < 'orange' Out[3]: True In [4]: 'Orange' <= 'orange' Out [4]: True In [5]: 'Orange' > 'orange' Out[5]: False In[6]: 'Orange' >= 'orange' Out[6]: ??? The outputs of snippets [2] and [6] (marked as ???) respectively are:

True and false

Which of the following statements is false? a. DataFrames have a describe method that calculates basic descriptive statistics for the data and returns them as a two-dimensional array. b. In a DataFrame, the statistics are calculated by column. c. Method describe nicely demonstrates the power of array-oriented programming with a functional-style call - Pandas handles internally all the details of calculating these statistics for each column. d. You can control the precision of floating-point values and other default settings with pandas' set_option function.

a. DataFrames have a describe method that calculates basic descriptive statistics for the data and returns them as a two-dimensional array.

Which of the following statements is false? a. For big data applications, you'll also want to choose algorithms that are easy to sequentialize - that will enable you to put lots of processors to work simultaneously. b. The simplest and most apparent algorithms often perform poorly - developing more sophisticated algorithms can lead to superior performance. c. All of the above statements are true. d. None of the above statements are true.

a. For big data applications, you'll also want to choose algorithms that are east to sequentialize - that will enable you to put lots of processors to work simultaneously.

Which of the following statements is false? a. JSON (JavaScript Object Notation) is a data-interchange format readable only by computers and used to represent objects (such as dictionaries, lists and more) as collections of name-value pairs. b. Many libraries you'll use to interact with cloud-based services such as Twitter, IBM Watson and others communicate with your applications via JSON objects. c. JSON can represent objects of custom classes. d. JSON has become the preferred data format for transmitting objects across platforms. This is especially true for invoking cloud-based web services, which are functions and methods that you call over the Internet.

a. JSON (JavaScript Object Notation) is a data-interchange format readable only by computers and used to represent objects (such as dictionaries, lists and more) as collections of name-value pairs.

Which of the following statements is false? a. The de presentation type in the following f-string formats strings as integer values: F'{10:d}' b. The integer presentation types b, o and x or X format integers using the binary, octal or hexadecimal number systems, respectively. c. The c presentation type in the following f-string formats an integer character code as the corresponding character: f'{65:c} {97:c} d. If you do not specify a presentation type, as in the second placeholder below, non-string values like the integer 7 are converted to strings: f'{"hello":s} {7}

a. The de presentation type in the following f-string formats strings as integer values: F'{10:d}'

Which of the following statements about sorting unique items is false? a. Sorting data (i.e., placing the data in a particular order - ascending or descending - is one of the most important types of computing applications. b. An important item to understand about sorting unique values is that the end result - the sorted array - will be the same no matter which algorithm you use to sort the array. The choice of algorithm affects only the run time of the program. c. Selection sort and insertion sort - are relatively simple to program but inefficient. Merge sort - is much faster than selection sort and insertion sort but harder to program. d. All of the above statements are true.

b. An important item to understand about sorting unique values is that the end result - the sorted array - will be the same no matter which algorithm you use to sort the array. The choice of algorithm affects only the run time of the program.

Which of the following statements is false? a. NumPy arrays use only zero-based integer indexes. b. Like arrays, Series use only zero-based integer indexes. c. Series may have missing data, and many Series operations ignore missing data by default. d. All of the above statements are true.

b. Like arrays, Series use only zero-based integer indexes.

If the contents of a file should not be modified, open the file for ___ -- another example of the principle of least privilege. This prevents the program from accidentally modifying the file. a. Writing only b. Reading only c. Reading and writing d. None of the above

b. Reading only

Consider this text from Shakespeare: Soliloquy = 'To be or not to be, that is the question' Which of the following statements is false? a. String method index searches for a substring within a string and returns the first index at which the substring is found; otherwise a ValueError occurs. The following code returns 3: soliloquy.index('be') b. String method rindex performs the same operation as index, but searches from the end of the string and returns the last index at which the substring is found; otherwise, a value-error occurs. The following code returns 3: soliloquy.rindex('be') c. String methods find and rfind perform the same tasks as index and rindex but, if the substring is not found, return -1 rather than causing a value-error. d. All of the above statements are true.

b. String method rindex performs the same operation as index, but searches from the end of the string and returns the last index at which the substring is found; otherwise, a Value-error occurs. The following code returns 3: soliloquy.rindex('be')

Which of the following statements is false? a. Searching data involves determining whether a value (referred to as the search key) is present in the data and, if so, finding its location. b. Two popular search algorithms are the simple binary search and the faster but more complex linear search. c. Sorting places data in ascending or descending order, based on one or more sort keys. d. Each of the above statements is true.

b. Two popular search algorithms are the simple binary search and the faster but more complex linear search.

1. What should the question mark (?) in the following for statement be replaced with, so that the statements will calculate 5!: In [1]: factorial = 1 In [2]: for number in range (5, 0, ?): Factorial *= number In[3]: factorial Out[3]: 120 a. 1 b. 0 c. -1 d. none of the above

c. -1

Which of the following statements is false? a. String method split with no arguments tokenizes a string by breaking it into substrings at each whitespace character, then returns a list of tokens. b. To tokenize a string at a custom delimiter (such as each comma-and-space pair), specify the delimiter string (such as, ', ') that split uses to tokenize the string, as in: Letters = 'A, B, C, D' Letters.split(', ') c. If you provide an integer as split's second argument, it specifies the maximum number of splits. The last token is the remainder of the string after the maximum number of splits. Assuming the string in Part (b), the code Letter.split(',', 1) Returns ['A', 'B', 'C , D'] d. There is also an rsplit method that performs the same task as split but processes the maximum number of splits from the end of the string toward the beginning.

c. If you provide an integer as split's second argument, it specifies the maximum number of splits. The last token is the remainder of the string after the maximum number of splits. Assuming the string in Part (b), the code Letter.split(',', 1) Returns ['A', 'B', 'C , D']

The following code implements a simple linear search. In [1]: def linear_search(data, search_key) For index, value in enumberate(data):: If value == search_key: Return ? Return -1 In the statement return ?, what should the ? be? a. Data b. Search_key c. Index d. None of the above

c. Index

The json module's ____ function reads the entire JSON contents of its file object argument and converts the JSON into a Python object. This is known as _____ the data. a. Read, serializing b. Load, serializing c. Load, deserializing d. Read, deserializing

c. Load, deserializing

Which of the following statements is false? a. Pandas is the most popular library for dealing with mixed data types, customized indexing, missing data, and data that needs to be manipulated into forms appropriate for the databases and data analysis packages. b. Data science presents unique demands for which more customized data structures are required. c. NumPy's array is optimized for heterogeneous numeric data that's accessed via integer indices. d. Pandas provides two key collections - Series for one-dimensional collections and DataFrames for two-dimensional collections.

c. NumPy's array is optimized for heterogeneous numeric data structures are required.

Which of the following statements about Dataframes is false? a. The index can be a slice. In the following slice containing, the range specified includes the high index ('Test3'): Grades.loc['Test1' : 'Test3'] b. When using slices containing integer indices with iloc, the range you specify excludes the high index (2): Grades.iloc[0:2] c. To select specific rows, use a tuple rather than slice notation with loc or iloc. d. All of the above statements are true.

c. To select specific rows, use a tuple rather than a slice notation with loc or iloc.

Which of the following would be accepted by a program strictly looking for four integers of data in CSV format? a. 100, 85 77,9 b. 100,85,,77,9 c. '100,85,77,9'=' d. 100,85, 77,9

d. 100,85, 77,9

Assuming the following Dataframe grades: Wally Eva Sam Katie Bob Test1 87 100 94 100 83 Test2 96 87 77 81 65 Test3 70 90 90 82 85 Which of the following statements is false? a. One benefit of pandas is that you can quickly and conveniently look at your data in may different ways, including selecting portions of the data. b. The following expression selects 'Eva' column and returns it as a Series: Grades['Eva'] c. If a DataFrame's column-name strings are valid Python identifiers, you can use them as attributes. The following code selects the 'Sam' column using the Sam attribute: Grades.Sam d. All of the above statements are true.

d. All of the above statements are true.


Related study sets

mental exam 3 practice questions

View Set

(I) Exercise 1.2 Recognizing Arguments

View Set

NCLEX-RN examination- Health Assessment

View Set

Ag Technology - Module 8, Ag Technology - Module 7, Module Five - Ag Tech, Module 4 - AG Tech, Ag Tech - Module 3, Module 2 - Ag tech - Measures and Horsepower, and Simple Machines, Ag Technology Quiz 1, Ag Technology Mastery Quiz 2, Module 6 - Ag te...

View Set

Chapter 5 Videos with Assessment

View Set

Water, Homeostasis, & Thermoregulation

View Set