Book Appointment Now
Python Programming Essentials Coursera Quiz Answers
All Weeks Python Programming Essentials Coursera Quiz Answers
Table of Contents
Python Programming Essentials Coursera Quiz Answers
Week 1: Basic Python Syntax
Q1. In the following piece of code, there is one line starting with \color{red}{\verb|#|}#. What does this line mean in Python?
1. tax_rate = 0.15
2. income = 40000
3. deduction = 10000
4.
5. # Calculate income taxes
6. tax = (income - deduction) * tax_rate
7. print(tax)
- This text is printed on the console.
- This is a comment aimed at the human reader. Python ignores such comments.
- This text is used as a file name for the code.
- This is a syntax error.
Q2. Which of the following are syntactically correct strings?
Try each of them in CodeSkulptor3.
- [Hello]
- “Hello, world.”
- “This course is great!”
- “Goodbye’
- “’ello”
Q3. Which of the following statements uses correct Python 3 syntax to print \color{red}{\verb|”Hello world.”|}”Hello world.” in the console?
1. print("Hello world.")
1. print Hello world.
1. print "Hello world."
1. print(Hello world.)
Q4. Which of the following arithmetic expressions are syntactically correct?
Try each of them in CodeSkulptor3.
- 3 * ((2 – 9) + 4)) * (2 + (1 – 3))|}3 * ((2 – 9) + 4)) * (2 + (1 – 3))
- (8 + (1 + (2 * 4) – 3))|}(8 + (1 + (2 * 4) – 3))
- (7 – 2) / (3 ** 2)|}(7 – 2) / (3 ** 2)
- 7 / +4|}7 / +4
- 9 – (2 – (4 * 3)|}9 – (2 – (4 * 3)
Q5. Which of the following can be used as a variable name?
Try using each in CodeSkulptor3.
- MYnumber
- number123
- 16ounces
- my-number
Q6. You would like to make it so that the variable \color{red}{\verb|ounces|}ounces has the value 16, thus representing one pound. What simple Python statement will accomplish this?
- \color{red}{\verb|ounces := 16|}ounces := 16
- \color{red}{\verb|16 = ounces|}16 = ounces
- \color{red}{\verb|ounces == 16|}ounces == 16
- \color{red}{\verb|ounces = 16|}ounces = 16
Q7. A gram is equal to 0.035274 ounces. Assume that the variable \color{red}{\verb|mass_in_ounces|}mass_in_ounces has a value representing a given mass in ounces. Which Python statement below uses the variable \color{red}{\verb|mass_in_ounces|}mass_in_ounces to compute an equivalent mass \color{red}{\verb|mass_in_grams|}mass_in_grams expressed in grams?
Think about it mathematically, but also test these expressions in CodeSkulptor3. If you are still confused, you might check out this student tutorial video by Kelly on unit conversions.
1. mass_in_grams = mass_in_ounces / 0.035274
1. mass_in_grams = mass_in_ounces * 0.035274
1. mass_in_grams = 0.035274 / mass_in_ounces
1. mass_in_ounces = 0.035274 * mass_in_grams
Week 2: Functions
Q1. Consider the following Python function definition:
1 def cube_root(val):
2 """
3 Given number, return the cube root of the number
4 """
5 return val ** (1 / 3)
Which of the expressions below is a valid call to the function \color{red}{\verb|cube_root|}cube_root?
- cube_root(8.0)
- cube_root = 8.0
- cube_root[8.0]
- cube root(8.0)
Q2. Running the following program results in the error
SyntaxError: bad input on line 5 (’return’).
Which of the following describes the problem?
1 def max_of_2(val1, val2):
2 if val1 > val2:
3 return val1
4 else:
5 return val2
6
7 def max_of_3(val1, val2, val3):
8 return max_of_2(val1, max_of_2(val2, val3))
- Wrong number of arguments in function call
- Extra parenthesis
- Missing colon
- Missing parenthesis
- Misspelled function name
- Misspelled variable name
- Incorrect indentation
- Misspelled keyword
Q3. The following code has a number of syntactic errors in it. The intended math calculations are correct, so the only errors are syntactic. Fix these errors.
Once the code has been fully corrected, it should print out two numbers. The first should be 1.09888451159. Submit the second number printed in CodeSkulptor3. Make sure that you enter at least four digits after the decimal point.
1 define project_to_distance(point_x point_y distance):
2 dist_to_origin = (pointx ** 2 + pointy ** 2) ** 0.5
3 scale == distance / dist_to_origin
4 print point_x * scale, point_y * scale
5
6 project-to-distance(2, 7, 4)
Enter answer here
Q4. A common error for beginning programmers is to confuse the behavior of \color{red}{\verb|print|}print statements and \color{red}{\verb|return|}return statements.
print statements can appear anywhere in your program and print a specified value(s) in the console. Note that execution of your Python program continues onward to the following statement. Remember that executing a \color{red}{\verb|print|}print statement inside a function definition does not return a value from the function.
return statements appear inside functions. The value associated with the \color{red}{\verb|return|}return statement is substituted for the expression that called the function. Note that executing a \color{red}{\verb|return|}return statement terminates execution of the function definition immediately. Any statements in the function definition following the \color{red}{\verb|return|}return statement are ignored. Execution of your Python code resumes with the execution of the statement after the function call.
As an example to illustrate these points, consider the following piece of code:
1 def do_stuff():
2 """
3 Example of print vs. return
4 """
5 print("Hello world")
6 return "Is it over yet?"
7 print("Goodbye cruel world!")
8
9 print(do_stuff())
Note that this code calls the function \color{red}{\verb|do_stuff|}do_stuff in the last \color{red}{\verb|print|}print statement. The definition of \color{red}{\verb|do_stuff|}do_stuff includes two \color{red}{\verb|print|}print statements and one \color{red}{\verb|return|}return statement.
Which of the following is the console output that results from executing this piece of code? While it is trivial to solve this question by cutting and pasting this code into CodeSkulptor, we suggest that you first attempt this question by attempting to execute this code in your mind.
1 Hello world
2 Is it over yet?
3 Goodbye cruel world!
1 Hello world
2 Is it over yet?
1 Hello world
2 Is it over yet?
3 Goodbye cruel world!
4 Is it over yet?
1 Hello world
Q5. Implement the mathematical function f(x) = -5 x^5 + 67 x^2 – 47 f(x) as a Python function. Then use Python to compute the function values f(0), f(1), f(2), and f(3). Enter the maximum (largest) of these four values calculated below.
A common error for this question is to fail to read the directions above carefully and submit your answer in the incorrect form. As a coder, always remember to note exactly what answers your code (and quiz questions) should produce.
Enter answer here
Q6. When investing money, an important concept to know is compound interest.
The equation FV = PV (1+rate)^{periods} relates the following four quantities.
- The present value (PV) of your money is how much money you have now.
- The future value (FV) of your money is how much money you will have in the future.
- The nominal interest rate per period (rate) is how much interest you earn during a particular length of time, before accounting for compounding. This is typically expressed as a percentage.
- The number of periods (periods) is how many periods in the future this calculation is for.
Finish the following code, run it, and submit the printed number. Provide at least four digits of precision after the decimal point.
1 def future_value(present_value, annual_rate, periods_per_year, years):
2 """
3 Input: the numbers present_value, annual_rate, periods_per_year, years
4 Output: future value based on formula given in question
5 """
6 rate_per_period = annual_rate / periods_per_year
7 periods = periods_per_year * years
8
9 # Put your code here.
10
11
12 Before submitting your answer, test your function on the following example.
future_value(500, .04, 10, 10)|}future_value(500, .04, 10, 10) should return 745.317442824
Hint: If you are stuck on this question, try working problem 7 of the Practice Exercises for Functions.
Enter answer here
Q7. For this final question, your task is to find the formula for a simple geometric problem using Google and then implement that formula in Python. While you may think that it is silly that we don’t just give you the formula, scripting in Python often requires one to do a substantial amount of searching for information. This question requires you to practice this important task.
Write a Python function that computes the area of an equilateral triangle given the length of one of its sides. Search for a mathematical formula that specifies this relation and translate that formula into Python. Hint: The desired formula involves taking a square root. Remember that you compute a square root of a number in Python by raising that number to the 0.50.5 power using the \color{red}{\verb||} operator.
As a test, your area function should return an area of 1.73205080757 for an equilateral triangle with sides of length 2. Now, use this function to compute the area of equilateral triangle with sides of length 5. Enter this area as a number (and not the units) with at least four digits of precision after the decimal point.
Enter answer here
Week 3: Logic and Conditionals
Q1. Which of the following are Boolean values in Python?
- False
- “True”
- 0
- True
Q2. Consider the Boolean expression \color{red}{\verb|not (p or not q)|}not (p or not q). Give the four following values in order, separated only by spaces:
the value of the expression when p is True, and q is True,
the value of the expression when p isTrue, and q is False,
the value of the expression when p False, and q is True,
the value of the expression when p is False, and q is False,
Remember, each of the four results you provide should be True or False with the proper capitalization.
Answers:
Q3. Given the following initialization:
1 bool1 = True
2 bool2 = False
Which of the expressions below evaluate to True?
- not False
- not (bool1 == bool2)
- bool1 != False
- bool2 == True
Q4. Two expressions are logically equivalent if they have the same value for all possible values of the variables that comprise the expression.
Given two numbers num1 and num2, which one of the expressions below is logically equivalent to the following arithmetic comparison:
1 num1 >= num2
1 num2 < num1
1 (num1 > num2) or (num1 == num2)
1 (num1 > num2) and (num1 != num2)
1 not (num1 <= num2)
- An if statement can have how many \color{red}{\verb|else|}else parts?
- Unlimited, i.e., 0 or more
- 1
- 0
Q6. In Python, conditional statements may be nested. Consider the following function that takes two Boolean values as input and returns a Boolean value.
1 def nand(bool1, bool2):
2 """
3 Take two Boolean values bool1 and bool2
4 and return the specified Boolean values
5 """
6
7 if bool1:
8 if bool2:
9 return False
10 else:
11 return True
12 else:
13 return True
Which Boolean expression below is logically equivalent to the function call \color{red}{\verb|nand(bool1, bool2)|}nand(bool1, bool2) where \color{red}{\verb|bool1|}bool1 and \color{red}{\verb|bool2|}bool2 are Boolean variables?
- (bool1 and bool2)
- not (bool1 or bool2)
- not (bool1 and bool2)
- (bool1 or bool2)
Q7. The Collatz conjecture is an example of a simple computational process whose behavior is so unpredictable that the world’s best mathematicians still don’t understand it.
Consider the simple function f(n)f(n) (as defined in the Wikipedia page above) that takes an integer nn and divides it by two if nn is even and multiplies nn by 33 and then adds one to the result if nn is odd. The conjecture involves studying the value of expressions of the form f(f(f(…f(f(n)))))f(f(f(…f(f(n))))) as the number of calls to the function ff increases. The conjecture is that, for any non-negative integer nn, repeated application of ff to nn yields a sequence of integers that always includes 11.
Your task for this question is to implement the Collatz function ff in Python. The key to your implementation is to build a test that determines whether nn is even or odd by checking whether the remainder when nn is divided by 22 is either zero or one. Hint: You can compute this remainder in Python using the remainder opertor \color{red}{\verb|%|}% via the expression \color{red}{\verb|n % 2|}n % 2. Note you will also need to use integer division \color{red}{\verb|//|}// when computing ff.
Once you have implemented ff, test the your implementation on the expression f(f(f(f(f(f(f(674)))))))f(f(f(f(f(f(f(674))))))). This expression should evaluate to 190190. Finally, compute the value of the expression f(f(f(f(f(f(f(f(f(f(f(f(f(f(1071))))))))))))))f(f(f(f(f(f(f(f(f(f(f(f(f(f(1071)))))))))))))) and enter the result below as an integer. Remember to use copy and paste when moving the expressions above into your Python environment. Never try to retype expressions by hand.
Answers:
Get All Course Quiz Answers of Introduction to Scripting in Python Specialization
Python Programming Essentials Coursera Quiz Answers
Python Data Representations Coursera Quiz Answers
Python Data Analysis Coursera Quiz Answers
Python Data Visualization Coursera Quiz Answers