Questions Python
Questions Python
Total points 5
1.
Question 1
What are functions in Python?
1 / 1 point
Correct
Right on! Python functions encapsulate a certain action, like outputting a message to the screen in the case of
print().
2.
Question 2
What are keywords in Python?
1 / 1 point
Keywords are used to print messages like "Hello World!" to the screen.
Correct
You got it! Using the reserved words provided by the language we can construct complex instructions that will
make our scripts.
3.
Question 3
What does the print function do in Python?
1 / 1 point
The print function generates PDFs and sends it to the nearest printer.
The print function stores values provided by the user.
Correct
You nailed it! Using the print() we can generate output for the user of our programs.
4.
Question 4
Output a message that says "Programming in Python is fun!" to the screen.
1 / 1 point
1
print("Programming in Python is fun!")
RunReset
Correct
5.
Question 5
Replace the ___ placeholder and calculate the Golden ratio: \frac{1+\sqrt{5}}{2}21+5.
Tip: to calculate the square root of a number xx, you can use x**(1/2).
1 / 1 point
1
print ((1+5**(1/2))/2)
RunReset
Correct
1.
Question 1
What is a computer program?
1 point
The overview of what the computer will have to do to solve some automation problem.
A step-by-step recipe of what needs to be done to complete a task, that gets executed by the computer.
2.
Question 2
What’s automation?
1 point
The process of replacing a manual step with one that happens automatically.
3.
Question 3
Which of the following tasks are good candidates for automation? Check all that apply.
1 point
Creating a report of how much each sales person has sold in the last month.
Setting the home directory and access permissions for new employees joining your company.
Populating your company's e-commerce site with the latest products in the catalog.
4.
Question 4
What are some characteristics of the Python programming language? Check all that apply.
1 point
The Python interpreter reads our code and transforms it into computer instructions.
We can practice Python using web interpreters or codepads as well as executing it locally.
5.
Question 5
How does Python compare to other programming languages?
1 point
It's always better to use an OS specific language like Bash or Powershell than using a generic language like
Python.
Programming languages are so different that learning a second one is harder than learning the first one.
6.
Question 6
Write a Python script that outputs "Automating with Python is fun!" to the screen.
1 point
1
print("Automating with Python is fun!")
RunReset
Automating with Python is fun!
7.
Question 7
Fill in the blanks so that the code prints "Yellow is the color of sunshine".
1 point
1
2
3
color = "Yellow"
thing = "sunshine"
print(color + " is the color of " + thing)
RunReset
Yellow is the color of sunshine
8.
Question 8
Keeping in mind there are 86400 seconds per day, write a program that calculates how many seconds there are
in a week, if a week is 7 days. Print the result on the screen.
Note: Your result should be in the format of just a number, not a sentence.
1 point
1
print(86400*7)
RunReset
604800
9.
Question 9
Use Python to calculate how many different passwords can be formed with 6 lower case English letters. For a 1
letter password, there would be 26 possibilities. For a 2 letter password, each letter is independent of the other,
so there would be 26 times 26 possibilities. Using this information, print the amount of possible passwords that
can be formed with 6 letters.
1 point
1
print(26**6)
RunReset
308915776
10.
Question 10
Most hard drives are divided into sectors of 512 bytes each. Our disk has a size of 16 GB. Fill in the blank to
calculate how many sectors the disk has.
Note: Your result should be in the format of just a number, not a sentence.
1 point
1
disk_size=16*1024*1024*1024
sector_size = 512
sector_amount =
(((16*1024*1024*1024)/512))
print(sector_amount)
31250000.0
1.
Question 1
In this scenario, two friends are eating dinner at a restaurant. The bill comes in the amount of 47.28 dollars. The
friends decide to split the bill evenly between them, after adding 15% tip for the service. Calculate the tip, the
total amount to pay, and each friend's share, then output a message saying "Each person needs to pay: "
followed by the resulting number.
1 point
4
5
bill = 47.28
tip = 0.15*47.28
share = total / 2
RunReset
2.
Question 2
This code is supposed to take two numbers, divide one by another so that the result is equal to 1, and display
the result on the screen. Unfortunately, there is an error in the code. Find the error and fix it, so that the output is
correct.
1 point
X = 5
Y = 5
result = X / Y
print(result)
RunReset
3.
Question 3
Combine the variables to display the sentence "How do you like Python so far?"
1 point
1
2
a = "How"
b = "do"
c = "you"
d = "like"
e = "Python"
f = "so"
g = "far?"
print(a,b,c,d,e,f,g)
RunReset
4.
Question 4
This code is supposed to display "2 + 2 = 4" on the screen, but there is an error. Find the error in the code and
fix it, so that the output is correct.
1 point
RunReset
2 + 2 = 4
5.
Question 5
What do you call a combination of numbers, symbols, or other values that produce a result when evaluated?
1 point
An explicit conversion
An expression
A variable
An implicit conversion
1.
Question 1
This function converts miles to kilometers (km).
2. Call the function to convert the trip distance from miles to kilometers
4. Calculate the round-trip in kilometers by doubling the result, and fill in the blank to print the result
1. def convert_distance(miles):
2. km = miles * 1.6
3. # approximately 1.6 km in 1 mile
4. return km
5. my_trip_miles = 55
6. # 2) Convert my_trip_miles to kilometers by calling the function above
7. my_trip_km = convert_distance(my_trip_miles)
8. # 3) Fill in the blank to print the result of the conversion
9. print("The distance in kilometers is " + str(my_trip_km))
10.# 4) Calculate the round-trip in kilometers by doubling the result
11.# and fill in the blank to print the result
12.print("The round-trip in kilometers is " + str(my_trip_km*2))
2.
Question 2
This function compares two numbers and returns them in increasing order.
1. Fill in the blanks, so the print statement displays the result of the function call in order.
Hint: if a function returns multiple values, don't forget to store these values in multiple variables
3.
Question 3
1 point
Variables
Return values
Parameters
Data types
4.
Question 4
Let's revisit our lucky_number function. We want to change it, so that instead of printing the message, it returns
the message. This way, the calling line can print the message, or do something else with it if needed. Fill in the
blanks to complete the code to make it work.
def lucky_number(name):
number = len(name) * 9
print("Hello " + name + ". Your lucky number is " + str(number))
(lucky_number("Kay"))
(lucky_number("Cameron"))
5.
Question 5
1 point
1.
Question 1
1 point
2**2
True
False
2.
Question 2
Complete the script by filling in the missing parts. The function receives a name, then returns a greeting based
on whether or not that name is "Taylor".
def greeting(name):
if name == "Taylor":
return "Welcome back Taylor!"
else:
return "Hello there, " + name
print(greeting("Taylor"))
print(greeting("John"))
4.
Question 4
Is "A dog" smaller or larger than "A mouse"? Is 9999+8888 smaller or larger than 100*100? Replace the plus
sign in the following code to let Python check it for you and then answe
"A dog" is larger than "A mouse" and 9999+8888 is larger than 100*100
"A dog" is smaller than "A mouse" and 9999+8888 is larger than 100*100
"A dog" is larger than "A mouse" and 9999+8888 is smaller than 100*100
"A dog" is smaller than "A mouse" and 9999+8888 is smaller than 100*100
5.
Question 5
If a filesystem has a block size of 4096 bytes, this means that a file comprised of only one byte will still use 4096
bytes of storage. A file made up of 4097 bytes will use 4096*2=8192 bytes of storage. Knowing this, can you fill
in the gaps in the calculate_storage function below, which calculates the total number of bytes needed to store a
file of a given size?
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = filesize//4096
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = filesize%4096
# Depending on whether there's a remainder or not, return
# the total number of bytes required to allocate enough blocks
# to store your data.
if partial_block_remainder > 0:
return 4096*(full_blocks+1)
return full_blocks*4096
4096
4096
8192
8192
1.
Question 1
Complete the function by filling in the missing parts. The color_translator function receives the name of a color,
then prints its hexadecimal value. Currently, it only supports the three additive primary colors (red, green, blue),
so it returns "unknown" for all other colors.
def color_translator(color):
if color == "red":
hex_color = "#ff0000"
elif color == "green":
hex_color = "#00ff00"
elif color == "blue":
hex_color = "#0000ff"
else:
hex_color = "unknown"
return hex_color
#0000ff
unknown
#ff0000
unknown
#00ff00
Unknown
4.
Question 4
Students in a class receive their grades as Pass/Fail. Scores of 60 or more (out of 100) mean that the grade is
"Pass". For lower scores, the grade is "Fail". In addition, scores above 95 (not included) are graded as "Top
Score". Fill in this function so that it returns the proper grade.
def exam_grade(score):
if score>95:
grade = "Top Score"
elif score>=60:
grade = "Pass"
else:
grade = "Fail"
return grade
Pass
Fail
Pass
Pass
Top Score
Fail
1 point
2.2
2
5. The longest_word function is used to compare 3 words. It should return the word with the most number
of characters (and the first in the list when they have the same length). Fill in the blank to make this
happen.
6. def longest_word(word1, word2, word3):
7. if len(word1) >= len(word2) and len(word1) >= len(word3):
8. word = word1
9. elif len(word2) >= len(word3):
10. word = word2
11. else:
12. word = word3
13. return(word)
14.
15.print(longest_word("chair", "couch", "table"))
16.print(longest_word("bed", "bath", "beyond"))
17.print(longest_word("laptop", "notebook", "desktop"))
10.
Question 10
The fractional_part function divides the numerator by the denominator, and returns just the fractional part (a
number between 0 and 1). Complete the body of the function so that it returns the right number. Note: Since
division by 0 produces an error, if the denominator is 0, the function should return 0 instead of attempting the
division.
0.0
0.25
0.6666666666666667
0.5
0
0.0
Week 3 Assignment Exam
Terms in this set (23)
How many times will "Not there yet" be printed?
x=0
while x < 5:
print("Not there yet, x=" + str(x))
x=x+1
print("x=" + str(x))
5 (The variable x starts at 0 and gets incremented once per iteration, so there are 5 iterations for which x is smaller
than 5.)
n this code, there's an initialization problem that's causing our function to behave incorrectly. Can you
find the problem and fix it?
def count_down(start_number):
while current > 0:
print (current)
current -= 1
print ("Zero!")
count_down(3)
def count_down(current):
while current > 0:
print (current)
current -= 1
print ("Zero!")
count_down(3)
The following code causes an infinite loop. Can you figure out what's missing and how to fix it?
Loop (cycle) begins from start number to the stop number. In example we have 1 and 5 respectively. Start = 1 to the
end 5. At the while-loop's body you can see print(n) function to print number, after printing number will increase to
the 1 and the loop will start again until the condition n<=end is met
While loops let the computer execute a set of instruction while the condition is true (Using while loops we can keep
executing the same group of instructions until the condition stops being true.)
Fill in the blanks to make the print_prime_factors function print all the prime factors of a number. A prime factor
is a number that is prime and divides another without a remainder.
def print_prime_factors(number):
# Start with two, which is the first prime
factor = 2
# Keep going until the factor is larger than the number
while factor <= number:
# Check if factor is a divisor of number
if number % factor == 0:
# If it is, print it and divide the original number
print(factor)
number = number / factor
else:
# If it's not, increment the factor by one
factor += 1
return "Done"
The following code can lead to an infinite loop. Fix the code so that it can finish
successfully for all numbers.
Note: Try running your function with the number 0 as the input, and see what you get!
def is_power_of_two(n):
# Check if the number can be divided by two without a remainder
while n % 2 == 0 and n != 0:
n=n/2
# If after dividing by two the number is 1, it's a power of two
if n == 1:
return True
n += 1
return False
Fill in the empty function so that it returns the sum of all the divisors of a number, without including it. A divisor is a
number that divides into another without a remainder.
def sum_divisors(n):
i=1
sum = 0
# Return the sum of all divisors of n, not including n
while i < n:
if n % i == 0:
sum += i
i +=1
else:
i+=1
return sum
The multiplication_table function prints the results of a number passed to it multiplied by 1 through 5.
An additional requirement is that the result is not to exceed 25, which is done with the break statement.
Fill in the blanks to complete the function to satisfy these conditions.
def multiplication_table(number):
# Initialize the starting point of the multiplication table
multiplier = 1
# Only want to loop through 5
while multiplier <= 5:
result = multiplier * number
# What is the additional condition to exit out of the loop?
if result > 25:
break
print (str(number) + "x" + str(multiplier) + "=" + str(result))
# Increment the variable for the loop
multiplier += 1
Fill in the gaps of the sum_squares function, so that it returns the sum of all the squares of numbers
between 0 and x (not included). Remember that you can use the range(x) function to generate a
sequence of numbers from 0 to x (not included).
def square(n):
return n*n
def sum_squares(x):
sum = 0
for n in range(x):
sum += square(n)
return sum
In math, the factorial of a number is defined as the product of an integer and all the integers below it. For example,
the factorial of four (4!) is equal to 123*4=24. Fill in the blanks to make the factorial function return the right
number.
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
1) Python is a general purpose programming language?
Python is a general-purpose language, meaning it can be used to create a variety of different programs
and isn't specialized for any specific problems.
A general-purpose language is a computer language that is broadly applicable across application
domains, and lacks specialized features for a particular domain. This is in contrast to a domain-
specific language (DSL), which is specialized to a particular application domain.
Python is a very high-level programming language because its syntax so closely resembles the
English language. Higher-level means it's more readable to humans and less readable to computers.
Likewise, Lower-level means less readable for humans and more readable for computers.
It being an interpreted language (Python is called an interpreted language because it goes through
an interpreter, which turns code you write into the language understood by your computer's processor)
which is not subject to processor, makes Python a high-level language