META MARKETING ANALYTICS PROFESSIONAL CERTIFICATE

Course 2: Introduction to Data Analytics

Week 2: Python for Data Analysis

Coursera Study Guide

Click to Enroll in Coursera Meta Marketing Analytics Professional Certificate

CONTENT

This week you will be introduced to programming in Python. You will learn foundational programming concepts such as variables, data types, and functions.

Learning Objectives

  • Compare and contrast the different formats and use cases of different kinds of data
  • Identify gaps in data collected and describe the strengths and weaknesses in the available data
  • Demonstrate basic proficiency in Python with variables, control flow, loops, and basic data structures

GRADED QUIZ: LOADING AND RUNNING A PYTHON PROGRAM

1. JSON files are structured in a nested structure. True or False?

  • True (CORRECT)
  • False

Correct: This is correct! JSON files store data in a semi-structured format through a nesting structure.

2. JSON files are tab delimited. True or False?

  • True
  • False (CORRECT)

Correct: Correct. JSON files are not actually delimited, because they use a nested structure to hold data.

3. It is important to memorize all of the syntax and every function. True or False?

  • True
  • False (CORRECT)

Correct: This is correct! There are many resources you can use instead of memorizing everything.

4. Python is Object-Oriented. True or False?

  • True (CORRECT)
  • False

Correct: Python allows you to create data objects that might be used over and over again.

5. Python is only used for data analytics. True or False?

  • False (CORRECT)
  • True

Correct: Python is used in many fields for many reasons.

6. When writing code in a Jupyter notebook, you should separate your code into different cells into logical chunks.

  • False
  • True (CORRECT)

Correct: Correct! Although you can write all of your code in one cell, it is usually best to separate the code into different cells by logical chunks.

7. Sometimes you need a little help while writing code in a Jupyter notebook. You can use a question mark before or after a function and then execute the cell to learn more about the function.

  • True (CORRECT)
  • False

Correct: Correct! You can write a question mark before or after the function to learn more about how to use a function.

8. Which of the following data types store tabular data? Choose all that apply.

  • JSON
  • CSV (CORRECT)
  • XLSX (CORRECT)
  • TXT (CORRECT)

Correct: This is correct! CSV files store tabular data with values separated by commas.

Correct: This is correct! XLSX files store tabular data; they’re basically spreadsheets!

Correct: This is correct! TXT files can hold both tabular data and semi-structured data.

9. Logical thinking is a skill that must be practiced like any other. True or False?

  • True (CORRECT)
  • False

Correct: Thinking logically is one of the most important skills a programmer can have.

10. Which of the following describes Python? Check all that apply.

  • Low level
  • General Purpose (CORRECT)
  • Open Source (CORRECT)
  • Object-Oriented (CORRECT)

Correct: Python is General Purpose!

Correct: Python is open source!

Correct: Python is Object-Oriented!

11. Being good at math is more important than being able to think logically. True or False?

  • False (CORRECT)
  • True

Correct: The ability to think logically is much more important than being good at math for programmers.

12. It is okay to keep code references, look things up, and to copy and paste code you have used in the past instead of memorizing everything. True or False?

  • False
  • True (CORRECT)

Correct: There is no need to memorize everything about a programming language.

13. When writing code in a Jupyter notebook, you should separate your code into different cells so there is only one line per cell. True or False?

  • True
  • False (CORRECT)

Correct: Correct! Writing one line per cell isn’t always possible. Even when it is possible, it is usually best to separate the code into different cells by logical chunks.

14. Which of the following data types typically have delimited data? Select all that apply.

  • XLSX
  • JSON
  • TXT (CORRECT)
  • CSV (CORRECT)

Correct: This is correct! Typically in data analysis, TXT files hold delimited data like with tabs.

Correct: This is correct! CSV files are comma delimited.

15. It is okay to keep code references, look things up, and to copy and paste code you have used in the past instead of memorizing everything. True or False?

  • False
  • T​rue (CORRECT)

Correct: There is no need to memorize everything about a programming language.

PRACTICE QUIZ: PYTHON

1. What does the following code output?

my_list = [10,30,40]

if my_list[1] < 20:
   print(“first”)
elif len(my_list) == 2:
   print(“second”)
elif my_list[0] + 25 > my_list[1]:
   print(“third”)
elif len(my_list) > 1:
   print(“fourth”)
  • “second”
  • “first”
  • “third” (CORRECT)
  • “fourth”

Correct: That’s right! The second elif-clause (third clause overall) is the first ‘True’ statement and thus the code is the only one to get executed.

2. Which are legitimate Python variable names?

  • in
  • 1_is_the_lonliest_number
  • my_cool_variable (CORRECT)
  • MyCoolVariable (CORRECT)
  • dog_8_my_homework (CORRECT)

Correct: This variable name follows the conventions in Python by using A-Z (uppercase or lowercase) and underscores. This is a good example of “snake case” which is typically used in Python.

Correct: This variable name follows the conventions in Python by using A-Z (uppercase or lowercase). This is an example of “camel case”. However, know that usually “snake case” is used over “camel case” in Python variable names.

Correct: This variable name follows the conventions in Python by using A-Z (uppercase or lowercase) and underscores and numbers (that aren’t at the beginning of the variable name).

3. Which of the following are built-in types?

  • Number
  • Array
  • Text
  • Dictionary (CORRECT)
  • Boolean (CORRECT)
  • Float (CORRECT)
  • Integer (CORRECT)

Correct: The ‘dictionary’ Python type allows us to hold data using key-value pairs.

Correct: The Boolean Python type can only be two values, ‘True’ or ‘False’.

Correct: The ‘float’ Python type allows us to hold numerical data that can contain a decimal point (floating point numbers).

Correct: The ‘integer’ Python type allows us to hold numerical data that are positive or negative numbers.

4. What is the output of this code?

for count in range(1,5):
  print(count)
    if count > 1:
      print("Hello")
    elif count <= 3:
        print("Hi")
    else:
       print("*")

 print("All Done")
  • 1
  • Hi
  • 2
  • Hi
  • 3
  • Hi
  • 4
  • Hi
  • All Done
  • 1 (CORRECT)
  • Hi
  • 2
  • Hello
  • 3
  • Hello
  • 4
  • Hello
  • 5
  • Hello
  • All Done
  • 1
  • Hi
  • All Done
  • 2
  • Hello
  • All Done
  • 3
  • Hello
  • All Done
  • 4
  • Hello
  • All Done
  • 1
  • Hi
  • 2
  • Hi
  • 3
  • Hi
  • 4
  • Hi
  • All Done

Correct: That’s right! ‘count’ started at 1 and went up to 4. (Second options)

5. Which of the following restrictions do Python dictionaries have?

  • Dictionary values must be unique.
  • Dictionary values cannot be lists or dictionaries.
  • Dictionary keys must be unique. (CORRECT)
  • Dictionary keys cannot be lists or dictionaries. (CORRECT)

Correct: That’s right. Dictionary keys must be unique, otherwise Python wouldn’t know what value to reference if duplicate keys were allowed. Trying to create a dictionary with duplicate keys will leave only one of the values accessible with that key.

Correct: That’s right!

GRADED QUIZ: PYTHON FOR DATA ANALYSIS

1. Which built-in function would you use to find the smallest number in a list?

  • min() (CORRECT)
  • small()
  • max()
  • smallest()

Correct: This is correct! The min() function will return the smallest number in a series.

2. If the function you’re writing isn’t returning anything, you should NEVER include a `return` statement in the function. True or False?

  • False (CORRECT)
  • True

Correct: That’s correct! If your function isn’t returning a value, you can either exclude the `return` statement or you can place a `return` statement with no value following it at the end of your function.

3. Consider the following code:

total = 0
for n in range(5):
 total = total + n
print(total)

If we run the code, what will the output of the above code be?

  • 20
  • 15
  • 10 (CORRECT)
  • 5

Correct: Correct! The total begins at 0 and then increases by the next number provided by range(), starting with 0.

4. Variables in Python can have numbers in the variable names but only if you separate the numbers with underscores ( _ ) in the name.

  • False (CORRECT)
  • True

Correct: Correct! You can have numbers in the variable name as long as the variable doesn’t start with a number. You don’t need an underscore to include a number in the variable name.

5. You are given a string variable defined like so, `my_str = “5”`. How would you cast (change the variable type) the variable to a float type?

  • int(my_str) (CORRECT)
  • cast(my_str, ‘float’)
  • my_str.float()
  • float(my_str)

6. What would the conditional statement evaluate to? `”3” == “3.0”`

  • True
  • False (CORRECT)

Correct: Correct! These are considered different values since they are both strings.

7. You are going through someone else’s code, when you find a built-in function you do not know.

Which built-in function would you use to look up the definition of the other function?

  • look()
  • library()
  • def()
  • help() (CORRECT)

Correct: This is correct! The help() function is useful for looking up things you do not know.

8. When you put a `return` statement with no value after it, it’ll return the same thing as if you didn’t have a `return` statement. True or False?

  • False
  • True (CORRECT)

Correct: That’s right! Including a `return` statement with no value after it returns `None` which would be the same if you exclude `return` in your function.

9. What would the conditional statement evaluate to? `not (“Hello” != “Good-bye”)`

  • False (CORRECT)
  • True

Correct: Correct!

10. Variables in Python can have underscores ( _ ) in the variable names.

  • True (CORRECT)
  • False

Correct: Correct! You can have underscores anywhere in a Python variable name.

11. Every function you write must have a `return`, even if it’s just to return “nothing”. True or False?

  • False (CORRECT)
  • True

Correct: That’s right! In Python, the `return` statement is optional. But you can include it at the end of your function and it will work the same.

12. Which file type always stores data in a nested, semi-structured data format?

  • TXT
  • XLSX
  • CSV
  • JSON (CORRECT)

Correct: Correct! JSON files store data in a nested, semi-structure data format. They can sometimes be interpreted as tabular data but typically aren’t.

13. Which of the following describe Python? Please select all that apply.

  • Low-Level
  • General Purpose (CORRECT)
  • Open Source (CORRECT)
  • Object-Oriented (CORRECT)

Correct: Correct! Its ability to do anything is one of its greatest strengths.

Correct: Correct! Python is free for the world.

Correct: Correct! Python allows you to make data objects, which you can use as many times as you need.

14. Why is it important to add comments to your code?

  • Adding comments is the only way to segment code in Jupyter.
  • It makes it easier for everyone to read your code and understand what is going on. (CORRECT)
  • The code will not run if you don’t add comments.
  • It is not important to add comments to your code.

Correct: Correct! Being able to understand your code is essential to programming.

15. What type would “15.0” be?

  • Float
  • Boolean
  • Integer
  • String (CORRECT)

Correct: The quotation marks turn this into a string, no matter what data is between them.

16. 1. my_list = [ 4, 8, 15, 16, 23, 42 ]

Given the following variable, what will my_list[2] return?

  • 4
  • 16
  • 8
  • 15 (CORRECT)

Correct: Exactly! As the third item in the list, 13 is in position 2.

17. Consider the program below:

 total = 0
 for v in range(4):
    total = total + v

 print(total)

If we run the program, what will the output of the above code be?

  • 6 (CORRECT)
  • 10
  • 0, 1, 2, 3
  • 7

Correct: Correct! Each time we go through the loop, we add the value ‘v’ to ‘total’.

18. What built-in function would you use to find the biggest number in a list?

  • largest()
  • max() (CORRECT)
  • big()
  • upper()

Correct: The max() function returns the largest number in a list.

19. True or False?

 def doubler(n):
 result = n * 2
 return result

In the code above, the “result” variable is a global variable.

  • False (CORRECT)
  • True

Correct: Since “result” is defined inside of a function, it is, by default, a local variable.

Subscribe to our site

Get new content delivered directly to your inbox.

Liking our content? Then, don’t forget to ad us to your BOOKMARKS so you can find us easily!