S364 FINAL EXAM
a = "True" is an example of what datatype
string
You have the following data in student_df: To get the average age of students by program and gender, which of the following code would work?
student_df.groupby(['Program', 'Gender'])['Age'].mean()
1. Date 2. Time 3. DateTime 4. TimeDelta
1. date info (year month day) not hour min sec 2. time info (hour min sec) not year month day 3. date and time info 4. difference between two dates or times
Use a Pandas function learned to find out the number of passengers in the 2nd class cabin. How many are they? (Use the Pclass columns)
184
How many timew Hello world! will be printed out? for i in range(1, 7, 3): print("Hello world!")
2
What do you get from this code? 10/5
2.0
Result of the following code: num = 3 while num > 0: print(num) num - num - 1
3 2 1
num = 3 while num > 0: print(num)
3 3 3 3 3 3 forever
What happens after groupby() is called on a DataFrame?
A DataFrameGroupBY object is created and returned
To remove the "Name" and "Sex" columsn completely from titanic_df (meaning that the dataframe itself is changed), what should be done ot arguments in the drop() method? HINT: Select ALL CORRECT; this questions assumes that we will type one line of code (not two separate lines)
"Name" and "Sex" must be passed in as a list axis must be set to be 1 inplace must be set to be True
Which of the following operator in Python joins strings together? For example: "Hello " operator "World!" result in "Hello World!"
+
What is the result of 10%3
1
Which of the following is true of the following code: x = "4" x = float(x)
After the execution, x now contains 4.0
Pre-defined code module executed when needed
Function Control Flow
Execute certain code repeatedly for a set number of times
Loop Control Flow
Given list1 = ["H", "e", "l", "l", "o"] what does "&".join(list1) do?
Return a string: "&H&e&l&l&o"
Execute the code line by line in the order written. this is the default
Sequential Control Flow
What does the following code return? a = np.random.normal(0, 1, 9).reshape(3, 3)
a 3X3 array with random numbers from a standard normal distribution
Use the following code to create an array. Select the correct code that would change the array "a" to a 5 X 5 array. (Note: % is the modulo operator in Python. It returns the remainder after one number is divided by another.) Select ALL that are CORRECT. a = np.arange(25) for i in range(25): if i % 6 ==0: a[i] = 100
a = np.reshape(a, (5, -1)) a = np.reshape(a, (5, 5))
Which among the following options can be used to create a Data Frame in Pandas?
a NumPy array, a dictionary and a list
The following code creates what kind of datatype output? x = "I love data analytics" output = x.split(" ")
a list
In the following code the variable radians is referred to as what? x = math.cos(radians)
argument
match colors to their abbreviations used in matplotlib
blue = b green = g red = r black = k white = w yellow = y
For the numpy array c = [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] What is the code to get numbers 6 and 11? (select ALL that are correct)
c[1:, 1:2] c[1:3, 1:2]
Which of the following is NOT considered a data aggregation?
differences of each value from the mean
What is the output of the following code? tuple1=(5,1,7,2,2) tuple1.remove(2) print(tuple1)
error
To retrieve the "Service and phd" columns of the following Pandas Data Frame we would use the following code: Data Frame Name: df
filteredDf = df[['service','phd']]
How is missing values represented in Pandas dataframes and series?
np.nan or nan (sometimes printed out as NaN)
The correct way to use the reshape() function in Numpy for a 2 x 2 array (2 row, 2 column) import numpy as np array = np.array([1,2,3,4]) newArray = ????? print(newArray)
np.reshape(array,(2,2))
Which of the following code will create a pivot table like this
pd.crosstab([titanic_df['Survived'], titanic_df['Pclass']], [titanic_df.Sex])
Which of the following code creates a line that represents y = 3x+2? Select ALL correct ones.
plt.plot([0,1], [2,5]) plt.plot([10,100], [32, 302])
Which of the following code will generate a chart like this below using titanic data?
plt.scatter(titanic_df['Age'], titanic_df['Fare'], color='r', marker='v', alpha=0.5)plt.scatter(titanic_df['Siblings/Spouses Aboard'], titanic_df['Fare'], color='g', marker='o', alpha=0.3)
To create 4 charts in 2 rows and 2 columns and work on the chart on the lower right corner. What code should you write?
plt.subplot(2, 2, 4)
Which of the following code would add a x-axis label to your chart?
plt.xlabel('Age')
The following code would be used to reference a Row in a Pandas Data Frame by the name of the name of the Row data = np.random.normal(0, 1, (4, 4)) # mean = 0, std = 1, 3 x 3 x = ["N", "S", "E", "W"] y = ['A', 'B', 'C', 'D'] frame = pd.DataFrame(data, index=x, columns=y)
print(frame.loc["N"])
For the numpy array a = [1, 2, 3, 4], what does a[1:3] return?
[2, 3]
A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 46 to 50 - D d. 51 to 60 - C e. 61 to 80 - B f. Above 80 - A Which of the following is an CORRECT implementation?
if marks<25: print "F" elif marks>=25 and marks<=45: print "E" elif marks>45 and marks<=50: print "D" elif marks>50 and marks<=60: print "C" elif marks>60 and marks<=80: print "B" else: print "A"
Purposefully designed code executed when unexpected problems occur in normal execution
Exception Handling
If custom row indexes and customer column titles are not provided the index and column titles will defaults to "blanks" or Null values.
False
Pandas does not have it's own built in Statistical function and we have to use other libraries or modules to perform statistical analysis.
False
Python code can only be executed from the top to the bottom without any exceptions
False
The following code will print a Pandas Data Frame sorted by the column A data = np.random.normal(1, 1, (4, 4)) # mean = 0, std = 1, 3 x 3 row = ["N", "S", "E", "W"] col = ['A', 'B', 'C', 'D'] frame = pd.DataFrame(data, index=row, columns=col) frame.sort_values(by=['A']) print(frame)
False
Unlike a Pandas DataFrame a Pandas Series cannot use strings as the row index
False
Which of the following is true about value_counts(). Select ALL CORRECT answers. Note, a method is a function associated with an object. It is called using syntax object.methodname(). For example, list1.append(1), append() is a List method.
It is a method for series. It returns the frequency of unique values
str = "IU has over 40000 students"; str.isdigit() str.isalpha() str.isalnum() Which of the above method will return true and why?
none of the above will return true since it contains a combination of alphabetic characters, numbers and spaces
Which of the following are necessary for array calculation to be called vectorization? Select ALL CORRECT answers.
the calculation must be element-wise no loops are used
Which of the following variable name is valid in Python?
three_dots NOT - three dots - 3dots - three-dots
Which of the following code will return only the columns of Age and Fare from the titanic_df dataframe? Select all correct answers.
titanic_df[['Age', 'Fare']] titanic_df.iloc[:, [5, 9]]
______________ are used to store information that will be referenced and manipulated in a computer program. They label data with a descriptive name, so our programs can be understood more clearly by humans. They are containers that hold different types of data.
variables
Which of the following creates a tuple of six strings?
vehicles = ("sedan","SUV","motorcycle","bicycle","hatchback","truck")
Which of the following can execute code cells in Jupyter notebook?
- Shift + Enter - From the menu Cell - Run Cells - Ctrl + Enter
If you have a 3X3 array a: [[-0.01903999, 0.60462075, 0.76736504], [-0.21765911, -0.43010199, -0.53521371], [-0.47671668, 0.49679601, -0.52441588]] What of the follow code will return the middle row as a one-dimensional array. Select ALL CORRECT answers.
- a[1] - a[1, :] - a[-2, :]
When would you use a markdown cell?
- add heading to sections of a Jupyter notebook - add a title to a Jupyter notebook
To access the first three characters in a string that's stored in a variable named message, you can use this code:
- first_three = message[0:3] - first_three = message[:3]
If you have the following code fruit_list = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] Which of the following code will allow you retrieve kiwi and melon in a list? (Select ALL that works).
- fruit_list[-3:-1] - fruit_list[4,6]
If you have the following code fruit_list = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] Which of the following code will allow you retrieve orange? (Select ALL that works). It is recommended that you use Jupyter notebook to try it out before submitting your answer.
- fruit_list[3] - fruit_list[-4]
What are the reasons to use comments in code?
- hide some code from the interpreter so they won't be executed - describe or explain the code logic - provide information such as the author, version, date of the code
A Pandas Series is a one-dimensional array which is labeled and can hold any data type.
True
T/F: The following code will cause an error // Print("Hello world!")
True
To retrieve values from a Pandas Data Frame we can reference rows and columns by name or by index value.
True
following code: future_day = date(3089, 1, 1) future_day.weekday() You get 1 as the returned value. Which week day is this future day
Tuesday
When executing the following code, what error would be raised? (Be as precise as possible) x = 4 x + "USA"
TypeError, because x is an integer and "USA" is a string. The data types are different
You have the following code: fruit_list = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] fruit_list.remove('apple') fruit_list.remove('orange') fruit_list[0] = 'watermelon' f ruit_list[-2] = 'pineapple' if len(fruit_list) < 3: print("You have too few types of fruits") elif len(fruit_list) <= 5: print("You are doing OK") else: print("You have great fruit diversity") What will you get?
You are doing OK
What code is is missing from line 2 in order to run this program and out the following result. CODE: 1) age = int(input("Please enter your age:")) 2) 3) print("You are " + age + " years old.") OUTPUT: Please enter your age:22 You are 22 years old.
age = str(age)
With the following array arr [[256, 112, 158, 165, 139], [187, 146, 709, 181, 137], [125, 344, 172, 450, 120], [180, 169, 179, 890, 164]] Which of the following code block will get values between 200 and 500 (inclusive)?
arr[(arr>=200) & (arr<=500)]
A statement that assigns a value to a variable.
assignment
What will be the output of following code? list2 = ["one", "two", [1, 2, 3, 4], [1.5, True, "23"]] list2[3][1] = 9 print(list2)
["one", "two", [1, 2, 3, 4], [1.5, 9, "23"]]
For the following list: lst = [9, 18, -1, -31, 15] create a list that identifies each element as positive or negative. Which of the following is correct? Correct!
["positive" if ele > 0 else 'negative' for ele in lst]
You have the following code: fruit_list = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] fruit_list.remove('apple') fruit_list.remove('orange') fruit_list[0] = 'watermelon' fruit_list[-2] = 'pineapple' print(fruit_list) What will you get?
['watermelon', 'cherry', 'kiwi', 'pineapple', 'mango']
What will be the output of the following code: list1=[4, 10, 7] list2=list1*2 print(list2)
[4, 10, 7, 4, 10, 7]
You have 2 ndarrays arr1 and arr2 [[1, 2], [3, 4]] [[ 8, 6], [12, 9]] if you run the code: arr1 + arr2, what will you get?
[[ 9, 8], 15, 13]]
To be executed, which of the following does Python programming language use?
interpreter
which of the following is correct about Python?
it is a high-level language
In the following code the variable x is called a(n) ______________ variable. total = 0 for x in [3, 41, 12, 9, 74, 15]: total = total + x print('Total: ', total)
iteration
To determine the length of a string that's in a variable named city, you can use this code:
len(city)
Which of the following differences between lists and tuples are true. Select ALL that apply.
lists use brackets [] and tuples use parentheses () processing tuples is faster than processing lists
An operator, denoted with a percent sign (%), that works on integers and yields the remainder when one number is divided by another
modulus operator
To print the first 5 rows of the following Pandas Data Frame we use... df = pd.read_csv("Salaries.csv")
more than one of the options would work df.head() df.tail() df.head(5) df.tail(5)
Which of the following are assignment statements:
name = "tom" age = 65 today = date.today() NOT age == 65
Which Pandas function from the options given below can read a dataset from a large text file?
read_csv
Use titanic_df, which of the following code lines creates meaningful boxplots that shows the distribution of passenger age by their cabin class and gender? Select ALL correct ones.
sns.boxplot(x='Pclass', y='Age', data=titanic_df, hue='Sex', palette='Set3') sns.boxplot(x='Sex', y='Age', data=titanic_df, hue='Pclass', palette='Set3')
A section of code that represents a command or action. So far, the statements we have seen are assignments and print expression statement.
statement
If you have a string str = 'I love Python' which of the following will result in an error?
str[44] NOT -str[-4] str[:] str[2:44]
To find out the number of female passengers in the first class who are 20 year's old or younger, which of the follow code would work? Hint: There will be 14 rows
titanic_df.loc[(titanic_df['Sex'] == 'female') and (titanic_df['Pclass']==1) and (titanic_df['Age']<=20)]
You have the following code: your_age = input("enter your age: ") friend_age = input("enter your friend's age: ") if your_age > friend_age: print('You are older than your friend') elif your_age < friend_age: print('You are younger than your friend') else: print("You two are of the same age") When entered 9 as your age and 11 as your friend's age. What would be the output of this program and why?
you are older than your friend
The code s1 = pd.Series([3, 0.5, 0.7, 0.9]) Will create a series of what datatype?
float
What data type does the value 5.3 have? [answer} (the answer must be exact as in Jupyter notebook to receive points)
float
What is the dtype of the numpy array if it is created with the code: np.array(((11, 2), (9.8, 19), (27, 6.9)))
float
A type that represents numbers with fractional parts.
floating point
You have 3 people's age stored in 3 variables first, second and third. Which of the following code will find the oldest properly? Select ALL CORRECT ones.
if first > second and first > third: print("Oldest is",first) elif second > first and second > third: print("Oldest is",second) elif third > first and third > second: print("Oldest is",third) else: print("All are equal") and another one - not sure if this one is even right
What code is missing from line 2 in order to have the following output given someone types in the word "Jane" CODE: 1) fname = input("What is your name? ") 2) 3) print("Your name is not Bob.") 4) else: 5) print("Your name is Bob.") OUTPUT: What is your name? Jane Your name is not Bob.
if fname != "Bob":
Retype the incorrect line of code below in order to produce the output. CODE: x = 3 if x < 10 print('Small') print('Done') OUTPUT: Small Done
if x<10:
Which of the following methods or attributes of a dataframe can tell you the data type of each column in a dataframe? Note, a method is a function associated with an object. It is called using syntax object.methodname(). For exmaple, list1.append(1), append() is a List method. A attribute is a property of an object. It is called using the syntax object.attributename. No parentheses). For example. arr.dim. ndim is an attribute of a numpy array's
info()
To remove all leading and trailing (beginning and ending) whitespaces in a String variable I can use what function?
.strip()
How many times Hello world! will be printed out? for i in range(1, 7, -3): print("Hello world!")
0
What do you get for these codes? 1. int(True) 2. int(False) 3. int(None)
1. 1 2. 0 3. TypeError
What will x be after the code is executed: x = 43 x = x + 1
44
Which of the following are advantages of using Jupyter notebook?
Code Development Communicate and share results documentation
At a decision point, the code execution could go through different branches that are mutually exclusive
Conditional Control Flow
Where in the computer is a variable such as "x" stored after the following Python line finishes? // x = 123 //
Main memory
Which of the following is NOT true about if statement?
NOT the code blocks in the branches must be indented i think its an if statement must be matched with an else branch
We use strptime() to convert string to a datetime object. Which of the following code does the proper conversion for the following date string: data_string = 'Wednesday, December 30, 2020 10:33'
NOT datetime.strptime(date_string '%A %B %d %Y %H:%M')
You have the following code: message = 'I love Python' message.replace('I', 'We') print(message) What do you get and why
NOT I love python bc the replace method was incorrectly called and did not work
I can use dt_now = datetime.now() to get the current time. Which of the following is correct?
NOT i can get the datetime of one month from now by running the code: dt_now + timedelta(month=1)
Which of the following statements about the data structures are true?
Pandas can store heterogeneous data, meaning different columns in a dataframe can have different data types. Numpy arrays can store homogeneous data only. That's one of the reasons why calculations with numpy arrays are so efficient.
What is the function of the secondary memory in a computer?
Store information for the long term, even beyond a power cycle
What is the output if the user types in "secret"? password = input("Please enter your passowrd\n") while passowrd ! = 'secret': >print("Try Again") >password = input() print("Welcome"
Welcome
For the array created below, what is the correct code to get the column means? Select ALL CORRECT answers. a = np.random.normal(0, 1, 9).reshape(3, 3)
a.mean(axis=0) np.mean(a, axis=0)