You are on page 1of 19

Practice Quiz: Hello World

Total points 5

1.
Question 1
What are functions in Python?

1 / 1 point

Functions let us to use Python as a calculator.

Functions are pieces of code that perform a unit of work.

Functions are only used to print messages to the screen.

Functions are how we tell if our program is functioning or not.

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 reserved words that are used to construct instructions.

Keywords are used to calculate mathematical operations.

Keywords are used to print messages like "Hello World!" to the screen.

Keywords are the words that we need to memorize to program in Python.

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.

The print function outputs messages to the screen

The print function calculates mathematical operations.

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

Great work! We're just starting but programming in Python

can indeed be a lot of fun.

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

Awesome job! See how we can use Python to calculate complex


values for us.

Module 1 Graded Assessment


Total points 10

1.
Question 1
What is a computer program?

1 point

A file that gets printed by the Python interpreter.

The syntax and semantics of Python.

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 inputs and outputs of a program.

The process of replacing a manual step with one that happens automatically.

The checkout processes at a grocery store.

The process of getting a haircut.

3.
Question 3
Which of the following tasks are good candidates for automation? Check all that apply.

1 point

Writing a computer program.

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.

Designing the new webpage for your company.

Taking pictures of friends and family at a wedding.

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

Python programs are easy to write and understand.

The Python interpreter reads our code and transforms it into computer instructions.

It's an outdated language that's barely in use anymore.

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

Python is the only programming language that is worth learning.

Each programming language has its advantages and disadvantages.

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

Practice Quiz: Expressions and Variables


Total points 5

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

total = bill + tip

share = total / 2

print ("Each person needs to pay: " + str(share))

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

print("2 + 2 = " + str(2 + 2))

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

Practice Quiz: Functions


Total points 5

1.
Question 1
This function converts miles to kilometers (km).

1. Complete the function to return the result of the conversion

2. Call the function to convert the trip distance from miles to kilometers

3. Fill in the blank to print the result of the conversion

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

def order_numbers(number1, number2):


if number2 > number1:
return number1, number2
else:
return number2, number1

# Fill in the blanks so the print statement displays the result


# of the function call
smaller,bigger = order_numbers(100, 99)
print(smaller,bigger)

3.
Question 3

What are the values passed into functions as input called?

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

What is the purpose of the def keyword?

1 point

Used to define a new function

Used to define a return value

Used to define a new variable

Used to define a new parameter

Practice Quiz: Conditionals


Total points 5

1.
Question 1

What's the value of this Python expression: (2**2) == 4?

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"))

3. What’s the output of this code if number equals 10?

if number > 11:


print(0)
elif number != 10:
print(1)
elif number >= 20 or number < 12:
print(2)
else:
print(3)

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

print("A dog" > "A mouse")


print(9999+8888 < 100*100)

"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

print(calculate_storage(1)) # Should be 4096


print(calculate_storage(4096)) # Should be 4096
print(calculate_storage(4097)) # Should be 8192
print(calculate_storage(6000)) # Should be 8192

4096
4096
8192
8192

Module 2 Graded Assessment


Total points 10

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

print(color_translator("blue")) # Should be #0000ff


print(color_translator("yellow")) # Should be unknown
print(color_translator("red")) # Should be #ff0000
print(color_translator("black")) # Should be unknown
print(color_translator("green")) # Should be #00ff00
print(color_translator("unknown")) # Should be unknown

#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

print(exam_grade(65)) # Should be Pass


print(exam_grade(55)) # Should be Fail
print(exam_grade(60)) # Should be Pass
print(exam_grade(95)) # Should be Pass
print(exam_grade(100)) # Should be Top Score
print(exam_grade(0)) # Should be Fail

Pass
Fail
Pass
Pass
Top Score
Fail

What's the value of this Python expression: 11 % 5?

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.

def fractional_part(numerator, denominator):


# Operate with numerator and denominator to
# keep just the fractional part of the quotient
if denominator==0:
return 0
else:
return (numerator/denominator)%1

print(fractional_part(5, 5)) # Should be 0


print(fractional_part(5, 4)) # Should be 0.25
print(fractional_part(5, 3)) # Should be 0.66...
print(fractional_part(5, 2)) # Should be 0.5
print(fractional_part(5, 0)) # Should be 0
print(fractional_part(0, 5)) # Should be 0

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?

def print_range(start, end):


# Loop through the numbers from start to end
n = start
while n <= end:
print(n)

def print_range(start, end):


# Loop through the numbers from start to end
n = start
while n <= end:
print(n)
n += 1

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

What are while loops in Python?

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.

2) Python is a high-level programming language?

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

3) Why python is called object-oriented language?


Python language views a problem in terms of objects involved rather than the procedure
for doing it. Python does have some features of a functional language. OOP's concepts like,
Classes, Encapsulation, Polymorphism, Inheritance etc.. in Python makes it as a object
oriented programming language. In Similar way we can created procedural program through
python using loops, for, while,etc and control structure.

You might also like