Python Final Review

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Given the code below, which line would you edit to change the csv which will be assigned to the dataframe? 1 import pandas 2 thanksdf = pandas.read_csv("thanksgiving.csv") 3 print(thanksdf) 4 sides = list(thanksdf.Sides) 5 print(sides) 6 for s in sides: 7 print(s) 8 tsorted = thanksdf.sort_values("Sides",ascending = False) 9 tsorted.to_csv("thanksgivingsorted.csv")

2

How many times will this for loop iterate when the user enters that he/she wants to enter 5 books iterateb = input("How many books? ") iterateb = int(iterateb) bookstring = ' ' for i in range(iterateb): booktitle = input("Book? ") bookstring = bookstring + ' ' + booktitle print("The books on your list are:"+bookstring)

5

What will be displayed on screen given the following code? user_name = 'Valerie' for name_letter in user_name: print(name_letter) print("The End!")

V a l e r i e The End!

What is the statement used to read a csv file using pandas? a)pandas.csv("filename") b)pandas.read_csv("filename") c)pandas.readcsv("filename") import d)pandas("filename")

b)pandas.read_csv("filename")

Which code should replace XXX to cause the program to output the message "Hello!"? def print_message():print('Hello!')XXX a) print_message b)print_message() c)print_message('Hello!') d)def print_message()

b)print_message()

Identify the correct syntax for importing modules from the script readFile.py? a)Import ReadFile b)import READFILE c) import readFile d) import readFile.py

c) import readFile

What is the statement used to read a csv file using pandas? a)import pandas("filename") b)pandas.readcsv("filename") c)pandas.read_csv("filename") d)pandas.csv("filename")

c)pandas.read_csv("filename")

Which loop is best to use when accessing the elements of a container, as when displaying every element in a list,

for loop

A company wants to send a reminder email to users who have not logged in for more than 10 days, but less than 20 days. Which expression can be used to decide if a user should get an email or not?

if days_since_login > 10 and days_since_login < 20:

What is the name of the function called in the following appJar code which determines the function triggered by each button? app.addButton("Button 1", press) app.addButton("Button 2", press) app.addButton("Button 3", press) app.addButton("Button 4", press) app.addButton("Button 5", press) app.addButton("Exit",press) app.go()

press

Which line is not part of the loop body? answer = 'y' while answer != 'n': s = input("Song?") print(s) answer = input("Continue (y/n)?") print("Thank you for playing")

print("Thank you for playing")

What is the final value of employee_bonus given value of num_sales is 3 if num_sales == 0: employee_bonus = 0 elif num_sales == 1: e employee_bonus = 2 elif num_sales == 2: employee_bonus = 5 else: employee_bonus = 10

10

What is the output after the following code is executed? def calc(num1, num2):print(1 + num1 + num2) calc(4, 5) calc(1, 2)

10 4

range(100,50,-10)

100 90 80 70 60

What sequences do the following statements generate? range(12,0,-2)

12 10 8 6 4 2

What is the value of test_val after the following code is executed? a = 12 test_val = 6 if a * 2 == test_val: a = a + 7 else: test_val = 2 * a test_val = a + 1

13

What user input would result in the following output, given the following if-elif-else statements: You are ok to view R movies! That's all folks! age2 = input() age2 = int(age2) if age2 <= 9: rating = 'G' elif age2 <= 12: rating = 'PG 'elif age2 <= 16: rating = 'PG-13 'else: rating = 'R' print("You are ok to view "+rating+" movies!") print("That's all folks!")

17

How many parameters are required by the draw_rect function?

2

What will be displayed on screen if the user input is 2008 user_num7 = input() user_num7 = int(user_num7) if user_num7 == 2001: print(user_num7,': Twin Towers') elif user_num7 == 2008: print(user_num7,': Great Recession') elif user_num7 == 2017: print(user_num7,': Hurricane Maria') else: print('No Disasters!')

2008: Great Recession

What is the ending value of x? x = 0 i = 5 while x <= 20: x = x + i i = i + 5

25

What user input will display only the following word: end x = int(input()) while x > 5: print(x) print("end")

5

What will be displayed by the following? for i3 in range(50,-21,-10): print(i3)

50 40 30 20 10 0 -10 -20

What user input assigned to x will cause an infinite loop? x = int(input()) while x != 0: x = x - 2 print(x)

7

What is the value of x after the following code is executed? x = 7 If x < 7 x = x + 1 x = x + 2

9

What should be in the print statement so that the output is: Favorite City: Sydney Favorite City: Paris Favorite City: New York Favorite City: Cairo Pre = "Favorite City" cities = ['Sydney', 'Paris', 'New York', 'Cairo'] for c in cities:print(__)

Pre+":", c

The full version of the range function has three arguments:

a starting value (start), ending value (stop) and the interval between numbers (interval). Only the ending value (stop) is required

How would one sort the Tops column of a dataframe named clothesdf in ascending order using pandas? a)clothesdf.sort_values("Tops",ascending=True) b)Tops.sort_values("clothesdf",ascending=True) c)clothesdf.sort_values("tops",ascending=True) d)clothesdf.sort_values("Tops",ascending=true)

a)clothesdf.sort_values("Tops",ascending=True)

Which is a statement that imports only the function factorial from my_funcs.py. a)import factorial b) import my_funcs c)import factorial from my_funcs d) from my_funcs import factorial

d) from my_funcs import factorial

What is the statement used to read a csv file using pandas? a)import pandas("filename") b)pandas.csv("filename") c)pandas.readcsv("filename") d)pandas.read_csv("filename")

d)pandas.read_csv("filename")

How would you display the the following numbers vertically on screen? 5 10 15 20 25 30

for i in range(5,35,5): print(i)

How would you display the the following numbers vertically on screen? 0 1 2 3 4 5 6 7

for i in range(8): print(i)

What will be displayed if the user enters the number 10 in response to the prompt: number? x = int(input('number?')) while x > 6: print(x) print("end")

generates infinite loop

Fill in the blank so that the loop displays all odd numbers from 1 to 100. i = 1 while i <= 100: print(i) i = _____

i + 2

Fill in the blank so that the loop displays all even numbers from 2 to 100 (including 2 and 100). i = 2 while i <= 100: print(i) i = _____

i +2

Branching statements

if if-else if-elif-else

Given a = 30, how would you write a condition to include the number 30 with all numbers less than 30 using <=

if a < = 30:

Given a = 0; how would you write conditions using >= to include zero with the set of positive numbers?

if a > = 0:

How would you write code for the following? Condition: age is greater than 65 Action: assign 20 to the variable name senior_discount

if age > 65 senior_discount = 20

How would you write code for the following? Condition: votes > 66 Action: display the word override Otherwise Action: display the word veto

if voters > 66: print ('override') else: print('veto')

How would you write code for the following? Condition: x is less than 20 Action: assign the result of x + 20 to variable name y

if x < 20: y = x + 20

What do the following statements generate? range(12)

0 1 2 3 4 5 6 7 8 9 10 11

What sequence is generated by range(4)?

0,1,2,3,4

How many times will the print statement be executed in the code below when the user enters 3 in response to the prompt: How many books? iterateb = input("How many books? ") iterateb = int(iterateb) bookstring = ' ' for i in range(iterateb): booktitle = input("Book? ") bookstring = bookstring + ' ' + booktitle print("The books on your list are:"+bookstring)

1

What is the value of x after the following code is executed? x = 17 if x * 2 <= 34: x = 0 else: x = x + 1 x = x + 1

1

What sequence is generated by range(1, 10, 3)

1,4,7

for num in range(5): print("The price is $%.2f." %num)

The price is $0.00. The price is $1.00. The price is $2.00. The price is $3.00. The price is $4.00.

What is displayed once the code below is executed? def change_value(x): print("The result is:",x + 2) change_value(4)

The result is: 6

Which line asks the user for the sentinel value? answer = 'y' while answer != 'n': s = input("Song?") print(s) answer = input("Continue (y/n)?") print("Thank you for playing")

answer = input("Continue (y/n)?")

Which has an error? Assume x = 10 and y = 20 a) if x < y: b) if x = y: c) if x!=y: d) if x > y:

b) if x = y:

Identify the correct syntax for importing modules from the script readFile.py? a) import READFILE b) import readFile c) Import ReadFile d) import readFile.py

b) import readFile

How would one assign the Tops column of a dataframe named clothesdf to the list named topslist? a)topslist = clothesdf.tops b) topslist = list(clothesdf.Tops) c) topslist = list(Tops.clothesdf) d)topslist = clothesdf.Tops

b) topslist = list(clothesdf.Tops)

How would one assign the Tops column of a dataframe named clothesdf to the list named topslist? a)topslist = list(Tops.clothesdf) b) topslist = list(clothesdf.Tops) c)topslist = clothesdf.Tops d)topslist = clothesdf.tops

b) topslist = list(clothesdf.Tops)

What python standard library module could be used to: Compute the mathematical cosine function of a user-entered number of degrees.

math

What would be displayed on screen, given the following code? user_name2 = 'erehwon' for name_letter2 in reversed(user_name2): print(name_letter2) print("The End!")

n o w h e r e The End!

What would be displayed on screen, given the following code? user_name2 = 'erehwon' for name_letter2 in reversed(user_name2): print(name_letter2) print("The End!")

nowhere The End!

Which range function will generate the sequence of numbers from zero to 99?

range(100)

Which range() function generates every even number between 20 and 30 (including both 20 and 30)?

range(20,31,2)

Which symbol is used to delineate docstrings for comments in code?

three consecutive "

==

values are equal

!=

values are not equal


Set pelajaran terkait

To Kill A Mockingbird: Chapters 8-14

View Set

Apparel and Accessories Marketing

View Set

PrepU Chapter 39: Oxygenation and Perfusion

View Set

Health unit 1 quiz 2 Body systems

View Set

Learning Curve: 13b Anxiety and Substance Use Disorders

View Set