Docsity

Python Programming Practice: Decision Logic, Loops, and DataFrames, Exams of Computer Science

Toggle sidebar
bg1
1
Python Programming – Decision Logic, Loops,
DataFrames & Common Errors Practice – Expert
Strategies| Accurate Verified Questions &Answer
true or false: all operators produce an "int" output
Solution
false
Which of the following operators represent proper means of
comparison? select all that apply
=>
==
!=
>=
<=
=!
>
=<
<
Solution
==
!=
>=
<=
>
<
When nesting if statements, how many spaces do you need to
ensure proper indentation?
Solution
4
Let's turn this quote into code using an if statement:
'Remember...there are no points for second place"
?=2
if place ? 2:
points = 0
print(?)
Solution
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python Programming Practice: Decision Logic, Loops, and DataFrames and more Exams Computer Science in PDF only on Docsity!

Python Programming – Decision Logic, Loops,

DataFrames & Common Errors Practice – Expert

Strategies| Accurate Verified Questions &Answer

true or false: all operators produce an "int" output Solution false Which of the following operators represent proper means of comparison? select all that apply => == !=

= <= =!

=< < Solution == !=

= <=

< When nesting if statements, how many spaces do you need to ensure proper indentation? Solution 4 Let's turn this quote into code using an if statement: 'Remember...there are no points for second place" ?= if place? 2: points = 0 print(?) Solution

place, ==, points Assume this is a complete code chunk, and we expect to see output printed after running this, why is the below code incorrect? if tom_brady == the_goat: print ("The Bucs just won another Super Bowl") a. the variables are not defined b. the code is correct c. == is used to operate a null covenant between two variables d. more than one of the answers is correct Solution a. the variables are not defined Steve Jobs wrote some Python code to determine if Apple met its quota for the Apple Newton. What does his code below output? quota= netwon= print("Welcome to Mac") if newton < quota: print ("Apple did not meet its quota of", quota, "Netwons") print("Thank you for using Mac") Solution Welcome to Mac Apple did not meet its quota of 1000000 Newtons Thank you for using Mac What type of error will you receive if you forget to indent your nested 'if' statement? a. RuntimeError b. IndexError c. SpacingError d. IdenetationError e. SyntaxError Solution d. IdentationError What is the best purpose of an 'else' statement? a. none of these are correct

Solution Iteration variable, Iterables, Statement UF Florida President wanted to define a function to count the student enrollment of the top three colleges: Liberal Arts, Engineering, and Business. What two parts are missing in his code? ? college_count(liberal_arts, engineering, business): enrollment = liberal_arts + engineering + business ? enrollment a. 'for' and 'return' b. 'for' and 'in' c. 'def' and 'for' c. none e. 'def' and 'return' Solution e. 'def' and 'return' There are several people stuck in Meta's metaverse. The bizarre nature of the metaverse has left them stacked in a pyramid. No one is sure why. But for fun you've decided to calculate just how many people are stacked in the pyramid. You've decided to use a 'for' loop to do the counting. What is the missing row in the middle of the 'for' loop? total= for metas in range (1,5): ? print (total) a. total += metas b. total = metas c. total = meta + 1 d. metas +=metas(sum) Solution a. total += metas Imagine we have a dataframe, df. What would be the purpose for running code like below? df.loc[1]

a. to rename df as loc b. to locate all missing values and replace with 'missing' c. there is no method called .loc d. to look for, retrieve a value from df Solution d. to look for, retrieve a value from df Imagine we have a pandas dataframe we have named 'df.' The dataframe consists of 2 columns 'col1' is 3o values long and is a random mix of the letters 'a', 'b', 'c' 'num1' is also 30 values long, and is a random set of numerical data (all int) Which of the following would give you the mean of the numerical (num1) column, grouped by values from column 'col1'? a. df[num1].groupby('num1').mean() b. df.groupby('num1').mean(numeric_only=True) c. none d. df.groupby[num1].mean() e. df.groupby('col1').mean() d. groupby[num1] on [num2] Solution e. df.groupby('col1').mean() What is the following numPy array? And how do you load numPy? np.randomseed(1955) x=np.random.randn(2,2,2,2) print(x.shape) x Solution import numpy as np (2,2,2,2) What is the output of the following code? import numpy as np list1 = [5,5,5] list2 = [10,10,10] np_list1=np.array(list1) np_list2=np.array(list2) np_list1/np_list

Look at the below code carefully. It is not at all uncommon to see errors of omission in code chunks like this. How can you fix it so that is products the output 'arrary([50, 50, 100]) import numpy as pd list1 = [5, 5, 5] list2 = [10, 10, 10] np_list1=np.array(list1) nplist2=np.array(list2) np_list1*np_list Solution rewrite nplist2 to np_list True or False: pandas has functionality to work with complicated dates. Solution True What would be returned by the following code? Assume this is the only code in the workbook, nothing else is loaded or present import pandas as pd today=datetime.datetime.now() print(now) a. an error b. variable recycle matrix c. the date and time right now d. the data saved as "now" Solution an error Imagine you have a dataframe called 'tickets' with 4 columns: ('name', 'address', 'parking_spot', 'numer_of_tickets'). If you wanted to subset out 2 columns, what code could you use? (There's two answers) a. tickets.loc[:,['name', 'number_of_tickets']] b. tickets.loc([['name', 'number_of_tickest']]) c. tickets[['name', 'number_of_tickets']] d. tickets[0:4] Solution a. tickets.loc[:,['name', 'number_of_tickets']]

c. tickets[['name', 'number_of_tickets']] True or False: Datasets to be joined generally need something in common, like a customer ID. The relationship does not need to be 1 to 1. Solution True True or False: Pandas can be used to join two dataframes together. Solution True What is the purpose of np.arrary in the below code? a=[6.1, 5.8, 5.97, 7.34...] b=[2.5, 3.19, 2.26...] np_a = arrary(a) np_b = array(b) a. to move data from a to be b. to measure the length of two lists c. to change the code from wide to long d. convert the lists to a numpy arrary Solution d. convert the lists to a numpy arrary Imagine we create a pandas series using the below code. What is one simple way to retrieve the value 0.5 from the series? import pandas as pd df=pd.Series([0.74, 0.25, 1.00, .50] index= ['a', 'b', 'c', 'd'] df a. df['loc'] b. df[iloc=.5] c. df['c'] d. df.find[0.5] e. df['d'] Solution e. df['d']

impute the median impute the most common value impute the mean Solution all of the above Assume all packages are loaded that need to be to make the code run successfully. Assume the test data ('test.csv') is loaded into your environment and named df. What would produce a table with the first 7 rows from a dataframe? Solution df.head(7) Price is a variable we are interested in building a model on that makes missing values and outliers particularly important to address. If price has an outlier variable that is really really extreme, what should we do? a. delete the rows b. do nothing Solution delete the rows What is the purpose of the code below? %matplotlib inline a. none/error b. make the plots show up inline c. change the shape of a matplotlib chart d. load the matlputlib as plt e. run code to show a chart after creating a graphic Solution b. make the plots show up inline Company is a list containing 4 strings, sales is a list with 4 integers. Which of these code snippets would create a bar chart? a. import matplotlib as plt plt.barv(company ,sales, color='grey') b. import matplotlib as plt plt.bar(sales ,company color='grey') c. import matplotlib as plt

plt.bar(company ,sales, color='grey') d. import matplotlib as plt plt.bar(company; sales; color=r) d. import seaborn as seaborn plt.bar(company, sales, color='grey') Solution c. import matplotlib as plt plt.bar(company ,sales, color='grey') True or False: When creating a chart using seaborn, it is possible to make formatting changes to the chart using matplotlib code Solution True True or False: Matplotlib is built on top of seaborn and uses seaborn code Solution False True or False: When using matplotlib, if the color is the only part of the format string, you can use any matplotlib colors spec or hex strings. Solution True True or False: Heatmaps can be used quickly to understand correlated variables in a dataset. Solution True When working with a pandas dataframe, what is one advantage seaborn has over using native matplotlib to visualize two of the columns. Note the question does not ask about using matplotlib in pandas. a. seaborn can use columns from pandas. Matplotlib requires additional formatting of data b. you cannot graph data from pandas in matplotlib, even after extracting it as a list c. seaborn allows you to tweak axis, titles, and formatting of a graph d. none Solution