Uploaded by Hannah.carnevale

Data Analytics Python Quizzes

advertisement
1) NOT OPEN
2) QUIZ 2
a) To display a floating point variable x rounded to two decimal places, you would use;
print(“{ :.2f}”.format(x))
b) If you would like to ask for a number with a prompt, you would use; number =
input(“Enter a number”)
c) False is not a valid variable name.
d) Delta is a valid variable name
e) x+1=x generates an error (the variable must be on the left side of the equal sign)
f) a semantic error deal with a problem with what you meant to do
g) The difference between a syntax error and a semantic error is a syntax error deals with
the rules of the language. A semantic error deals with the meaning.
h) Why are function useful?
i) Dividing a long program into functions allows you to debug the parts one at a time
and then assemble them into a working whole.
ii) Functions can make a program smaller by eliminating repetitive code. Later, if you
make a change, you only have to make it in one place.
iii) Well-designed functions are often useful for many programs. Once you write and
debug one, you can reuse it.
iv) Creating a new function gives you an opportunity to name a group of statements,
which makes your program easier to read, understand, and debug.
i) The functions for generating random numbers are imported from the random module
j) Parameters are the way you send the values of variables into a function
k) Casting is converting from one data type into another
l) Class is a valid variable name.
m) MyData32 is a valid variable name.
n) The operator for modulus (finding the remainder) is %
o) A syntax error deals with the way you typed the program
p) The functions for numerical analysis are imported from the numpy module
q) To include functions from a module, you use import
r) To differentiate between namespaces with functions that have the same name, you use the
format module.function()
3) QUIZ 3
a) The expression used to assign the value of B to the variable A in Python is A=B
b) The expression used to check if A is less than or equal to B in Python is a<=B
c) A variable that is not equal to zero or "" is considered true
d) What is the following code an example of?
i)
ii) A nested conditional
e) What is wrong with the following code?
i)
ii) Syntax error (else case must be the last one)
f) What is an infinite loop?
i) A set of instructions that continue over and over again without stopping
g) Can you replace a for statement with an equivalent set of Python statements with
a while command? True
h) What does the range function do?
i) Generates a list of numbers
i) What does the for statement do?
i) Execute a block of commands using each element of a list, one element at a time
j) Can you generate an infinite loop with a for statement? False
4) QUIZ 4
a) Which of the following removes all leading whitespace in string?
i) lstrip()
b) Which of the following flips the case of a string?
i) Swapcase()
c) Which of the following convert an object to a expression in Python?
i) Repr(x)
d) Which of the following convert an integer to hexadecimal string in Python?
i) Hex(x)
e) Which of the following converts a string to all lowercase?
i) Lower()
f) Given X="ABCDEFGHIJKLMNOPQRSTUVWXYZ", X[12:4:-3] is?
i) MJG
g) Given X="ABCDEFGHIJKLMNOPQRSTUVWXYZ", X[11:13] is?
i) LM
h) What gets printed
i)
ii) 2
i) What sequence of numbers is printed?
i)
ii) 49416
j) What gets printed?
i)
ii) 5
k) What is the following returns item with the minimum value from the list?
i) min(list)
l) What gets printed?
i)
ii) <type 'list'>
m) What is the following inserts an object at given index in a list?
i) list.insert(index, obj)
n) Given X=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25], X[6:8:2] is?
i) [7]
o) Given X=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25], X[14:16] is?
i) [15,16]
p) In a dictionary, you cannot set multiple values to the same key
5) QUIZ 5
a) For a Python program to prepare to put data into an existing file, you would use
something like outfile = open("newfile.txt", "a")
b) What Python programming construct could be used to cover a failure when a file doesn't
exist?
i) Try/except
c) When you are finished with using a file handle called output in Python, you should call
the function output.close()
d) To put data into a file that has been opened as "output", you would use the statement like
output.write("This is data going into the file")
e) Before you can use a file in Python, you must open it
f) For a Python program to prepare to put data into a new empty file, you would use
something like outfile = open("newfile.txt", "w")
g) Your "home directory" refers to the directory you start when you turn on your computer
(or log in)
h) The location of a file is known as the pathname
i) To copy a file, you would use the function shutil.copy()
j) To find the current working directory using the os library module, you would use
os.getcwd()
k) To create a compressed archive of files, you can use the zipfile library
l) On a Macintosh system where your login is "myId", your home directory is /Users/myId
m) To find out where a program is operating, you can use the the following function from
the pathlib Path object Path.cwd()
n) To get a list of sheets in an Excel workbook called "wb", you would use wb.sheetnames
o) To get the most recently opened sheet in the workbook "wb", you would use wb.active
p) To write the workbook "wb" to disk as "new.xlsx", you would use wb.save("new.xlsx")
q) To open an Excel workbook with a Python program, you would use the function
openpyxl.load_workbook()
r) The extension of Excel spreadsheets is xlsx
s) The Cell object is defined by the attributes row, column, coordniates, value
t) The worksheet that is currently (or most recently) viewed is called the active sheet
u) To go through the content of a file one line at a time, you would use a line like for line in
fhandle:
v) The Python object for working with a file is called a file handle
w) To bring all the content of a file into a Python program, you would use a Python
statement like content = fhandle.read()
x) To move or rename a file or folder, you would use the function shutil.move()
y) If you were running a Python program with the current working directory of
/Users/myId/Documents and you downloaded a data file called "data.csv" in your
Downloads folder, the absolute file name you would use is
/Users/myId/Downloads/data.csv
z) The suffix of a filename is called a extension
aa) The worksheet that is currently (or most recently) viewed is called the active sheet
bb) An Excel spreadsheet document is called a workbook
cc) To open an Excel workbook with a Python program, you would use the function
openpyxl.load_workbook()
6) QUIZ 6
a) To sort a dataframe df by its content, you use df.sort_values()
b) To get the labels of a pandas Series called s, you can use s.index
c) To pick all the data from the DataFrame df with rows where values in column 'x' is
greater than 10, you can use df[df['x']>10]
d) To compute a function f on a column 'a' in a DataFrame df, you use df['a'].apply(f)
e) Slicing with pandas labels is not the same as Python string or list slicing.
f) To delete columns 'a' and 'b' from a DataFrame called df, you can use
df.drop(['a','b'],axis="columns")
g) To get the elements of a pandas Series called s, you can use s.values
h) f = lambda x: x.max() - x.min() defines a function called f that calculates the range
i) A one-dimensional array-like object in pandas is called a Series
j) To change the index on a Series or DataFrame, you use reindex()
k) To select a column by label 'a' from a DataFrame df, you use df.loc[:,'a']
l) To access just columns 'a', 'b', 'c' from DataFrame df, you use df[['a','b','c']]
m) To sort a dataframe df by its content, you use df.sort_values()
n) A rectangular table of data object in pandas is called a DataFrame
o) To select a row by label 'a' from a DataFrame df, you use df.loc['a']
p) To delete rows 'a' and 'b' from a DataFrame called df, you can use df.drop(['a','b'])
q) To caluclate the averages of a DataFrane df, you use df.mean()
r) To find the distribution of values of column 'a' in dataframe df, you use
df["a"].value_counts()
s) To select a row by label 'a' from a DataFrame df, you use df.loc['a']
7) QUIZ 7
a) To ignore column names in a data file, you would add the following option in the
read_csv() statement.
i) header=None
b) To save a pandas dataframe called df as "data.xlsx", you can use df.to_excel("data.xlsx")
c) To extract data from a web page, you can use
bats=pd.read_html("http://www.thebaseballcube.com/mlb/teams/stats.asp?Y=2021&T=2
")
d) For reading dates in European format (dd-mm-yyyy), you would add the option to
read_csv()
i) dayfirst=True
e) To read data from comma delmited values from a file called "data.dat" into pandas, you
would use df = pd.read_csv("data.dat")
f) To separate a Series into a set of buckets, you can use the method cut()
g) To perform a calculation on every element of a Series, you can use map()
h) To denote missing data, you use np.nan
i) To set missing values to a set value, you can use the method fillna()
j) To remove replicated data, you can use the method drop_duplicates()
k) To connect rows of two DataFrames (df1 and df2) based on one or more keys, you can
use pd.merge(df1,df2)
l) To add dataframes df1 and df2 in a "wide" format, you would use
pd.concat([df1,df2],axis=1)
m) The "on" option of pd.merge determines the column names to use as the keys for merging
n) To sort the "df" dataframe on the primary column key "ID" and second column key
"Date", you can use df.sort_values(by=["ID","Date"])
o) To stack two DataFrames (df1 and df2), you can use pd.concat([df1,df2])
p) To read data from a data file "data.xlsx" into pandas, you would use df =
pd.read_excel("data.xlsx")
q) To interact with a SQLite database, you can use import sqlite3
r) To save a pandas data frame called df as "data.dat" comma separated value file, you can
use df.to_csv("data.dat")
s) To pick a random number of rows of a dataframe without replacement, you can use the
method sample()
t) To identify data that is missing, you can use the method isnull()
u) To include rows from both DataFrames (df1 and df2), no matter which dataframe the data
is in, you use pd.merge(df1,df2,how="outer")
v) The "on" option of pd.merge determines the column names to use as the keys for merging
w) To add dataframes df1 and df2 in a "long" format, you would use pd.concat([df1,df2])
8) QUIZ 8
a) Which type of chart would be best for displaying a variable over time?
i) Line graph
b) What chart type would be best for showing the distribution of a variable?
i) Density
c) Scatterplots are good for displaying the Correlation between two variables
d) Chord diagrams are good for displaying the Connections between items
e) Histograms are good for displaying the Distribution of a variable
f) What chart type would be best for showing how the ranking of a variable over time?
i) Slope
g) What chart type would be best for showing the relationship between variables?
i) Correlogram
h) Which type of chart would be best for displaying the relationship between two variables?
i) Scatterplot
i) Which type of chart would be best for displaying the distribution of a ratio variable?
i) Box plot
j) Which type of chart would be best for displaying a categorical variable?
i) Bar chart
Download