Programming for Everybody (Getting Started with Python) Answers

Stuck on your journey to learn Python? Get your questions answered in our “Programming for Everybody (Getting Started with Python) Answers” post! We tackle common roadblocks for beginners, making coding accessible for everyone.

What you will learn in the course “Programming for Everybody (Getting Started with Python)?

  • Install Python and write your first program
  • Describe the basics of the Python programming language
  • Use variables to store, retrieve, and calculate information
  • Utilize core programming tools such as functions and loops
Programming for Everybody (Getting Started with Python) Answers
Programming for Everybody (Getting Started with Python) – Coursera

Week 3 – Programming for Everybody (Getting Started with Python)

Week 3 Programming for Everybody – Assignment:

Write a program that uses a print statement to say ‘hello world’ as shown in ‘Desired Output’.

Code:

print("hello world")

Week 3 Programming for Everybody – Quiz:

1. When Python is running in the interactive mode and displaying the chevron prompt (>>>) – what question is Python asking you?

Answer – What would you like to do?

2. What will the following program print out:
>>> x = 15;
>>> x = x + 5;
>>> print x

Answer – 20

3. Python scripts (files) have names that end with:

Answer – .py

4. Which of these words are reserved words in Python?

Answer – if
break

5. What is the proper way to say “good-bye” to Python?

Answer – quit()

6. Which of the parts of a computer actually executes the program instructions?

Answer – Central Processing Unit

7. What is “code” in the context of this course?

Answer – A sequence of instructions in a programming language

8. A USB memory stick is an example of which of the following components of computer architecture?

Answer – Secondary Memory

9. What is the best way to think about a “Syntax Error” while programming?

Answer – The computer did not understand the statement that you entered

10. Which of the following is not one of the programming patterns covered in Chapter 1?

Answer – Random steps

Week 4 – Programming for Everybody (Getting Started with Python)

Week 4 Assignment 2.2:

Write a program that uses input to prompt a user for their name and then welcomes them. Note that input will pop up a dialog box.
Enter Sarah in the pop-up box when you are prompted so your output will match the desired output.

Code:

name = input("Enter your name")
print ("Welcome",name)

Week 4 Assignment 2.3:

Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Use 35 hours and a rate of 2.75 per hour to test the program (the pay should be 96.25). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking or bad user data.

Code:

hrs = float(input("Enter Hours:"))
rate = float(input("Enter Rate:"))
pay= hrs*rate
print ("You need to pay: ",pay)

Week 4 Programming for Everybody – Quiz:

1. In the following code, print 98.6. What is “98.6”?

Answer – A constant

2. In the following code, x = 42. What is “x”?

Answer – A variable

3. Which of the following variables is the “most mnemonic”?

Answer – hours

4. Which of the following is not a Python reserved word?

Answer – speed

5. Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following statement do? x = x + 2.

Answer – Retrieve the current value for x, add two to it and put the sum back into x

6. Which of the following elements of a mathematical expression in Python is evaluated first?

Answer – Parenthesis()

7. What is the value of the following expression

Answer – 2

8. What will be the value of x after the following statement executes: x = 1 + 2 * 3 – 8 / 4

Answer – 5

9. What will be the value of x when the following statement is executed: x = int(98.6)

Answer – 98

10. What does the Python raw_input() function do?

Answer – Pause the program and read data from the user

Week 5 – Programming for Everybody (Getting Started with Python)

Week 5 Assignment 3.1:

3.1 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number.

Do not worry about error checking the user input – assume the user types numbers properly.

Code:

hrs = input("Enter Hours:")
h = float(hrs)
rate1 = float(input("Enter Rate:"))
if h > 40 :
    rate2 = (rate1 * 1.5) * (h-40)
    
pay = ((h-5)*rate1) + rate2
print (pay)

Week 5 Assignment 3.3:

Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:
Score Grade

= 0.9 A
= 0.8 B
= 0.7 C
= 0.6 D
< 0.6 F
If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.

Code:

score = float(raw_input("Enter Score: "))
if(score>1.0):
    print ("out of range")   
elif score >= 0.9:
    print ("A")
elif score >= 0.8:
    print ("B")
elif score >= 0.7:
    print ("C")
elif score >= 0.6:
    print ("D")
elif score < 0.6:
    print ("F")

Week 5 Programming for Everybody – Quiz:

1. What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?

Answer – Indent the line below the if statement

2. Which of these operators is not a comparison / logical operator?

Answer – =

3. What is true about the following code segment: if x == 5 : print ‘Is 5’ print ‘Is Still 5’ print ‘Third 5’

Answer – Depending on the value of x, either all three of the print statements will execute or none of the statements will execute

4. When you have multiple lines in an if block, how do you indicate the end of the if block?

Answer – You de-indent the next line past the if block to the same level of indent as the original if statement

5. You look at the following text: if x == 6 : print ‘Is 6’ print ‘Is Still 6’ print ‘Third 6’. It looks perfect but Python is giving you an ‘Indentation Error’ on the second print statement. What is the most likely reason?

Answer – You have mixed tabs and spaces in the file

6. What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?

Answer – else

7. What will the following code print out? x = 0 if x < 2 : print ‘Small’ elif x < 10 : print ‘Medium’ else : print ‘LARGE’ print ‘All done’

Answer – Small All done

8. For the following code, if x < 2: print ‘Below 2’ elif x >= 2: print ‘Two or more’ else: print ‘Something else’. What value of ‘x’ will cause ‘Something else’ to print out?

Answer – This code will never print ‘Something else’ regardless of the value for ‘x’

9. In the following code (numbers added) – which will be the last line to execute successfully? (1) astr = ‘Hello Bob’; (2) istr = int(astr); (3) print ‘First’, istr; (4) astr = ‘123’; (5) istr = int(astr); (6) print ‘Second’, istr

Answer – 1

10. For the following code: astr = ‘Hello Bob’ istr = 0 try: istr = int(astr) except: istr = -1. What will the value be for istr after this code executes?

Answer – 1

Week 6 – Programming for Everybody (Getting Started with Python)

Week 6 Programming for Everybody – Assignment 4.6:

Write a program to prompt the user for hours and rate per hour using input to compute gross pay.
Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours.

Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation.
The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75).
You should use input to read a string and float() to convert the string to a number.
Do not worry about error checking the user input unless you want to – you can assume the user types numbers properly.
Do not name your variable sum or use the sum() function.

def computepay(h,r):
    if h>40:
        rate1 = (r*1.5)*(h-40)
    return ((h-5)*r)+rate1
hrs = float(raw_input("Enter Hours:"))
rate = float(raw_input("Enter Rate:"))
p = computepay(hrs,rate)
print (p)

Week 6 Programming for Everybody – Quiz:

1. Which Python keyword indicates the start of a function definition?

Answer – def

2. In Python, how do you indicate the end of the block of code that makes up the function?

Answer – You de-indent a line of code to the same indent level as the def keyword

3. In Python what is the raw_input() feature best described as?

Answer – A built-in function

4. What does the following code print out? def thing(): print ‘Hello’; print ‘There’

Answer – There

5. In the following Python code, which of the following is an “argument” to a function? x = ‘banana’; y = max(x); print y

Answer – x

6. What will the following Python code print out? def func(x) : print x; func(10); func(20)

Answer – 10, 20

7. Which line of the following Python program is useless? def stuff(): print ‘Hello’ return print ‘World’ stuff()

Answer – print ‘World’

8. What will the following Python program print out? def greet(lang): if lang == ‘es’: return ‘Hola’ elif lang == ‘fr’: return ‘Bonjour’ else: return ‘Hello’ print greet(‘fr’),’Michael’

Answer – Bonjour Michael

9. What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug – so read carefully). def addtwo(a, b): added = a + b return a; x = addtwo(2, 7); print x

Answer – 2

10. What is the most important benefit of writing your own functions?

Answer – Avoiding writing the same non-trivial code more than once in your program

Week 7 – Programming for Everybody (Getting Started with Python)

Week 7 Programming for Everybody – Assignment 5.2:

Write a program that repeatedly prompts a user for integer numbers until the user enters ‘done’.
Once ‘done’ is entered, print out the largest and smallest of the numbers.
If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message
and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.

Code:

largest = None
smallest = None
while True:
    num = raw_input("Enter a number: ")
    if num == "done": break
    try:
        num = int(num)
        if largest is None or largest < num: largest = num
        if smallest is None or smallest > num: smallest = num
    except:
        print ("Invalid input")
        continue
print ("The maximum number is ",largest)
print ("The minimum number is ",smallest)

Week 7 Programming for Everybody – Quiz:

1. What is wrong with this Python loop: n = 5; while n > 0 : print n; print ‘All done’

Answer – This loop will run forever.

2. What does the break statement do?

Answer – Exits the currently executing loop.

3. What does the continue statement do?

Answer – Jumps to the “top” of the loop and starts the next iteration.

4. What does the following Python program print out? tot = 0; for i in [5, 4, 3, 2, 1] : tot = tot + 1; print tot

Answer – 5

5. What is the iteration variable in the following Python code: friends = [‘Joseph’, ‘Glenn’, ‘Sally’]; for friend in friends : print ‘Happy New Year:’, friend; print ‘Done!’

Answer – friend

6. What is a good description of the following bit of Python code?; zork = 0; for thing in [9, 41, 12, 3, 74, 15] : zork = zork + thing; print ‘After’, zork

Answer – Sum all the elements of a list

7. What will the following code print out? smallest_so_far = -1; for the_num in [9, 41, 12, 3, 74, 15] : if the_num < smallest_so_far : smallest_so_far = the_num; print smallest_so_far

Answer – 1

8. What is a good statement to describe the is operator as used in the following if statement: if smallest is None : smallest = value

Answer – matches both type and value

9. Which reserved word indicates the start of an “indefinite” loop in Python?

Answer – while

10. How many times will the body of the following loop be executed? n = 0; while n > 0 : print ‘Lather’ print ‘Rinse’; print ‘Dry off!’

Answer – 0

Must Read:

Google and the Department of Defense Build AI-Powered Microscope to Aid Cancer Detection
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sonu
Sonu
25 days ago

Thank you! I received my certificate.