Intro to Python
Given the following list, what is spam[1:3]? spam = ['eggs', 'salt', 'butter', 'pan'] 'salt', 'butter' 'eggs', 'salt', 'butter', 'pan' 'butter', 'pan' 'salt', 'eggs', 'pan'
'salt', 'butter'
Which of the following can be sliced? (1, 4, 6) 'Have a great day!' ['eggs', 'salt', 'butter', 'pan'] {'name': 'billy', 'age': 12, 'height': 42}
(1, 4, 6) 'Have a great day!' ['eggs', 'salt', 'butter', 'pan']
Given the following code snippet, what will the output on the console be? spam = [{'animal': 'cougar', 'color': 'sandy', 'legs': 4},['duck', 42, 1.9873, 'color'],24, 49, 'animal'] newList = [] for k in spam:newList.append(k) def myFunction(newList):if not newList:print('Your missing a list!') else:print('It\'s alive!') (no console output) It's alive It\'s alive Your missing a list!
(no console output)
What operator is used to do string replication? * = - +
*
What operator is used to concatenate two strings together?
+
The range() method can have three integers passed to it such as range(1,2,3). Which slot is referred to as the stepping argument?
3
What operator is used to do assignments? + = - *
=
Match the meaning to the operator (there are more answers than questions): QuestionCorrect MatchSelected Match A. Less than or equal to B. Less than C. Greater than or equal to D. Equal to E. Not equal to F. Greater than
== D. Equal to != E. Not equal to <= A. Less than or equal to >= C. Greater than or equal to
What is the name of the Interactive Shell included with Python?
IDLE
Given the following code snippet, what will be returned to the user? spam = {'sport': 'basketball', 'team': 'bluejays', 'league': 'of their own'} spam['color'] OutOfRangeError IndexError TypeError KeyError
KeyError
In a WHILE loop, which keyword will get you out of the loop?
break
Given the following list, what is the spam[-2]? spam = ['eggs', 'salt', 'butter', 'pan'] pan eggs butter salt
butter
Given the following code snippet, what would be printed to the console screen? Think carefully! spam = {'animal': 'elephant', 'color': 'gray', 'size': 'large'} for item in spam.values(): print(item) animal color size elephant, gray, large animal, color, size elephant gray large
elephant gray large
In an IF statement, which of the following is NOT a possible keyword? elif else if except
except
Given the following code snippet, what should be returned? spam = {'kid': 'daughter', 'ball': 'round', 'pet': 'dog'} 'daughter' in spam.keys() true false
false
To calculate the length of a variable, what function is used?
len()
Given the following list, what is the 3rd index? spam = ['eggs', 'salt', 'butter', 'pan'] salt eggs pan butter
pan
To display output on the console, what function is used?
print()
Given the following list, what is the cleanest, correct way to change 'salt' to 'pepper', and keep it's place in the list? spam = ['eggs', 'salt', 'butter', 'pan'] del(spam['salt']) spam.append('pepper') spam['salt'] = 'pepper' spam.remove('salt') spam.append('pepper') spam.remove('salt') spam.insert(1, 'pepper')
spam.remove('salt') spam.insert(1, 'pepper')
Which of the following are immutable? dictionary list string tuple
string tuple
Write a function named printTable() that takes a list of lists of strings and displays it in a well-organized table with each column right-justified. Assume that all the inner lists will contain the same number of strings. For example, the value could look like this: tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']] Your printTable() function would print the following: apples Alice dogs oranges Bob cats cherries Carol moose banana David goose Hint: Your code will first have to find the longest string in each of the inner lists so that the whole column can be wide enough to fit all the strings. You can store the maximum width of each column as a list of integers. The printTable() function can begin with colWidths = [0] * len(tableData), which will create a list containing the same number of 0 values as the number of inner lists in tableData. That way, colWidths[0] can store the width of the longest string in tableData[0], colWidths[1] can store the width of the longest string in tableData[1], and so on. You can then find the largest value in the colWidths list to find out what integer width to pass to the rjust() string method.
tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']] def printTable(tableData): """ Print table neatly formatted: e.g: [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']] becomes: apples Alice dogs oranges Bob cats cherries Carol moose banana David goose """ # make list of ints to store later max len element of each list colWidths = [0] * len(tableData) # Store maxlen of each list i = 0 while i < len(tableData): colWidths[i] = len(max(tableData[i], key=len)) i = i + 1 # Print formatted for x in range(len(tableData[0])): for y in range(len(colWidths)): print(tableData[y][x].rjust(colWidths[y]), end=' ') print(end='\n') printTable(tableData)