COURSE 2: GET STARTED WITH PYTHON

Module 2: Functions and Conditional Statements

GOOGLE ADVANCED DATA ANALYTICS PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

INTRODUCTION – Functions and Conditional Statements

Next, you’ll discover how to call functions to perform useful actions on your data. You’ll also learn how to write conditional statements to tell the computer how to make decisions based on your instructions. And you’ll practice writing clean code that can be easily understood and reused by other data professionals.

Learning Objectives

  • Explain the purpose and logic of conditional statements such as if, else, and elif
  • Use comparators and logical operators to compare values
  • List the benefits of commenting on code
  • Identify best practices for writing clean code such as reusability, modularity, and refactoring
  • Describe how to define Python functions using the def and return keywords
  • Compare values using quality operators and logical operators
  • Write comments and break code into blocks to reduce complexity
  • Refactor code to enhance code readability and reuse
  • Define and call functions utilizing parameters and return data
  • Use data type conversions
  • Differentiate and convert between different data types using variable expressions and built-in functions
  • Define variables, expressions, operators
  • Introduce basic Python syntax

PRACTICE QUIZ: TEST YOUR KNOWLEDGE: FUNCTIONS

1. A data professional wants to define their own Python function. What keyword should they use at the start of the function block?

  • Return
  • for
  • def (CORRECT)
  • with

Correct: To define a function, use the keyword def at the start of the function block.

2. Modularity is the ability to write code in separate components that work together.

  • True (CORRECT)
  • False

Correct: Modularity is the ability to write code in separate components that work together. Modularity allows data professionals to reuse code for other programs.

3. Why do data professionals use comments for their Python code? Select all that apply.

  • To execute code
  • To provide helpful explanations (CORRECT)
  • To outline the steps of a process (CORRECT)
  • To document their work for teammates (CORRECT)

Correct: Data professionals use comments for their Python code to provide helpful explanations, outline the steps of a process, and document their work for teammates.

PRACTICE QUIZ: Conditional Statements

1. Fill in the blank: Comparators are operators that compare two values and produce _____ values.

  • string
  • boolean (CORRECT)
  • float
  • integer

Correct: Comparators are operators that compare two values and produce boolean values. Boolean is a data type that has only two possible values, usually true or false.

2. When comparing two expressions, what logical operator requires both expressions to be true to return a True result?

  • with
  • and (CORRECT)
  • not
  • or

Correct: When comparing two expressions, the and logical operator requires both expressions to be true to return a True result. Logical operators can connect multiple statements and perform complex comparisons.

3. Which of the following is a reserved keyword that sets up a condition in Python?

  • As
  • else
  • with
  • if (CORRECT)

Correct: if is a reserved keyword that sets up a condition in Python.

4. The elif keyword only works when there are exactly two possible conditions in the code.

  • True
  • False (CORRECT)

Correct: The elif keyword allows for more than two possible conditions in the code—up to an unlimited number of comparison cases.

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

QUIZ: MODULE 2 CHALLENGE

1. A data professional wants to define a function to calculate the volume of a box. What code should they begin with?

  • return volume_box(length, width, height):
  • if volume_box(length, width, height):
  • else volume_box(length, width, height):
  • def volume_box(length, width, height): (CORRECT)

Correct!

2. Which Python keyword makes a function produce new results and save the results for later use?

  • def
  • if
  • return (CORRECT)
  • as

Correct!

3. What are best practices for writing clean code? Select all that apply.

  • Redundancy
  • Modularity (CORRECT)
  • Reusability (CORRECT)
  • Clarity (CORRECT)

Correct!

4. Fill in the blank: Refactoring is used to create _____, which is code that is written in a way that is readable and makes its purpose clear.

  • branching code
  • self-documenting code (CORRECT)
  • open source code
  • immutable code

Correct!

5. Fill in the blank: A data professional can add a _____ to the beginning of a function’s body to summarize the function’s behavior and explain its arguments and return values.

  • docstring (CORRECT)
  • logical operator
  • comparator
  • conditional statement

Correct!

6. What is the Python comparator for not equal to?

  • <=
  • >=
  • != (CORRECT)
  • ==

Correct!

7. A data professional writes the following code: print(33 > 12 or 9 < 7). What result will Python display when they run the code?

  • True (CORRECT)
  • Equal
  • False
  • Not equal

Correct!

8. Fill in the blank: In Python, the if statement branches the execution based on a specific condition being _____.

  • True (CORRECT)
  • false
  • equal
  • not equal

Correct!

9. Fill in the blank: In Python, else is a reserved keyword that executes when preceding conditions evaluate as _____.

  • true
  • not equal
  • false (CORRECT)
  • equal

Correct!

10. A data professional wants to define a function to calculate the square of a number. What code should they begin with?

  • return find_square(n):
  • def find_square(n): (CORRECT)
  • if find_square(n):
  • else find_square(n):

Correct!

11. A data professional wants to make a Python function produce new results and save the results for later use. What keyword should they use in their code?

  • and
  • if
  • else
  • return (CORRECT)

Correct!

12. What is the term for code that is written to be readable and to make its purpose clear?

  • Open source
  • Branching
  • Immutable
  • Self-documenting (CORRECT)

Correct!

13. A data professional adds a docstring to the beginning of a function’s body. What is the purpose of the docstring?

  • To define the function
  • To make the function produce new results and save the results for later use
  • To summarize the function’s behavior and explain its arguments and return values (CORRECT)
  • To run the function and print the results

Correct!

14. What is the Python comparator for equal to?

  • == (CORRECT)
  • <=
  • >=
  • !=

Correct!

15. A data professional writes the following code: print(not 4 == ‘Data’). What result will Python display when they run the code?

  • Not equal
  • Equal
  • True (CORRECT)
  • False

Correct!

16. In Python, when does an if statement execute a piece of code?

  • When the condition evaluates to not equal
  • When the condition evaluates to false
  • When the condition evaluates to equal
  • When the condition evaluates to true (CORRECT)

Correct!

17. In Python, when does an else statement execute a piece of code?

  • When the if statement contains a false condition (CORRECT)
  • When the if statement contains text data
  • When the if statement contains a true condition
  • When the if statement contains numeric data

Correct!

18. Fill in the blank: A data professional can use the _____ keyword to make a Python function produce new results and save the results for later use.

  • if
  • return (CORRECT)
  • def
  • else

Correct!

19. Fill in the blank: To reduce errors and enhance teamwork, data professionals employ clean code best practices such as _____ and modularity.

  • branching
  • redundancy
  • immutability
  • reusability (CORRECT)

Correct!

20. In Python, what is the process of restructuring code while maintaining its original functionality?

  • Refactoring (CORRECT)
  • Converting
  • Reprogramming
  • Branching

Correct!

21. A data professional wants to summarize a function’s behavior and explain its arguments and return values. What should they add to the beginning of the function’s body?

  • An algorithm
  • A docstring (CORRECT)
  • A comparator
  • A logical operator

Correct!

22. A data professional writes the following code: print(23 > 32 and 7 != 5). What result will Python display when they run the code?

  • True
  • Not equal
  • False (CORRECT)
  • Equal

Correct!

23. Fill in the blank: In Python, the _____ statement branches the execution based on a specific condition being true.

  • then
  • else
  • if (CORRECT)
  • elif

Correct!

24. Fill in the blank: A _____ is a body of reusable code for performing specific processes or tasks.

  • variable
  • data type
  • keyword
  • function (CORRECT)

Correct: A function is a body of reusable code for performing specific processes or tasks.

25. Which Python feature enables data professionals to define code once, then use it many times without having to rewrite it?

  • Dynamic typing
  • Reprogramming
  • Conversion
  • Reusability (CORRECT)

Correct: Python’s reusability feature enables data professionals to define code once, then use it many times without having to rewrite it.

26. Fill in the blank: Lines of code that begin with a _____ serve as comments and don’t get executed.

  • Hashtag (CORRECT)
  • Ampersand
  • parenthesis
  • question mark

Correct: Lines of code that begin with a hashtag serve as comments and don’t get executed.

27. Which of the following words are examples of logical operators? Select all that apply.

  • greater than
  • or (CORRECT)
  • and (CORRECT)
  • not (CORRECT)

Correct: Logical operators include the words and, or, and not. Logical operators allow you to connect multiple statements together and perform more complex comparisons.

28. Fill in the blank: _____ describes the ability of a program to alter its execution sequence.

  • Refactoring
  • Comparing
  • Commenting
  • Branching (CORRECT)

Correct: Branching describes the ability of a program to alter its execution sequence.

29. A data professional wants to define a function to calculate the area of a rectangle. What code should they begin with?

  • return area_rectangle(length, width):
  • def area_rectangle(length, width): (CORRECT)
  • else area_rectangle(length, width):
  • if area_rectangle(length, width):

Correct!

30. What is the Python comparator for less than or equal to?

  • >=
  • <= (CORRECT)
  • ==
  • !=

Correct!

31. Fill in the blank: In Python, the _____ statement sets a piece of code to run only when the condition of the if statement is false.

  • else (CORRECT)
  • if
  • then
  • return

Correct!

32. What are the benefits of using clean code for data work? Select all that apply.

  • Eliminate the need for hashtags when commenting
  • Reduce errors (CORRECT)
  • Enhance teamwork (CORRECT)
  • Save time and effort (CORRECT)

Correct!