In: Computer Science
Question 17
Demonstrate your knowledge of if, elif and else:
Create a variable gpa. Set it to a number between 0 and 4.0
Write python statements which produce one line of output -just one of the below
You are a great student!
You are a good student!
You really need to study more!
You choose the thresholds between 0 and 4.0 which determine the outputs.
Question 18 (Multiple Choice)
pizza_toppings = []
if pizza_toppings:
(a)This evaluates to true because there is a variable pizza_toppings
(b)This evaluates to false because there is not a variable pizza_toppings
(c)This evaluates to true because the list contains the null element.
(d)This evaluates to false because the list is empty.
Question 20
Write a list comprehension which will do the same work as the below 3 statements:
squares = []
for i in range(10):
squares.append(i * i)
Question 18:
Source Code:
Output:
Code in text format (See above image of code for indentation):
#set variable gpa to 2.0
gpa=2.0
#choose the thresholds between 0 and 4.0
if(gpa<=2.0):
print("You really need to study more!")
#print statement based on condition
elif(gpa>3.0):
print("You are a great student!")
else:
print("You are a good student!")
Question 19:
(b)This evaluates to false because there is not a variable pizza_toppings
The given if condition raises an EOF parsing error because if condition checks for a variable pizza_toppings but it is a list so the above is the correct one.
Question20 :
Source Code:
Output:
Code in text format (See above image of code for indentation):
#using list comprehension
squares=[x*x for x in range(10)]
#print list
print (squares)