COURSE 7 – AUTOMATE CYBERSECURITY TASKS WITH PYTHON

Module 2: Write Effective Code Python

GOOGLE CYBERSECURITY PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

INTRODUCTION – Write Effective Code Python

In this comprehensive overview, participants will significantly enhance their proficiency in Python programming by delving into advanced topics that amplify their capabilities. The course focuses on the mastery of pre-built and user-defined Python functions, empowering learners to create modular and efficient code. The exploration of modules as a mechanism for accessing reusable code further elevates participants’ programming skills, enabling them to streamline development processes and enhance code organization.

A crucial aspect of this course is the emphasis on code readability, recognizing its paramount importance in collaborative projects and maintenance. Participants will learn best practices for writing clear and understandable code, fostering effective communication within development teams. By the conclusion of this course, learners will not only have expanded their technical expertise in Python but will also possess the skills needed to create efficient, modular, and comprehensible code, ensuring their readiness for complex programming challenges in diverse cybersecurity scenarios.

Learning Objectives

  • Incorporate pre-built functions into code.
  • Create new, user-defined Python functions.
  • Explain how modules are used in Python.
  • Identify best practices to improve code readability.

TEST YOUR KNOWLEDGE: INTRODUCTION TO FUNCTIONS

1. In Python, what is a function?

  • A section of code that contains an iterative statement
  • A section of code that can be reused in a program (CORRECT)
  • A section of code that exists directly in Python
  • A section of code that contains a conditional

A Python function is a section of code that can be reused in a program.

2. Which of the following keywords is essential when defining a function?

  • for
  • while
  • if
  • def (CORRECT)

The def keyword is essential when defining a function. It is placed before a function name to define it.

3. You want to define a function that performs a status check. Which of the following is a valid header for the function definition?

  • def status_check
  • def status_check(): (CORRECT)
  • def status_check()
  • def status_check:

A valid header for the function definition is def status_check():. Headers should include the def keyword, the name of the function followed by parentheses, and a colon (:).

4. You are responsible for defining a function alert() that prints out the statement “Security issue detected.” Which of the following blocks of code represent the correct indentation for defining and then calling the function?

def alert():
print("Security issue detected.")
alert()
def alert():
    print("Security issue detected.")
alert()     (CORRECT)
def alert():
    print("Security issue detected")
    alert()
    def alert():
print("Security issue detected")
alert()

When defining and then calling a function alert() that prints out the statement “Security issue detected.”, the following block of code demonstrates correct indentation:

def alert():
    print("Security issue detected.")
alert()     (CORRECT)

The only part that should be indented is the body of the function definition. In this case, the body is the line with the print() function.

TEST YOUR KNOWLEDGE: ARGUMENTS, PARAMETERS, AND RETURN STATEMENTS

1. Fill in the blank: In the following code, the integers 5 and 12 are _____:

for i in range(5, 12):
    print(i)
  • functions
  • return statements
  • arguments (CORRECT)
  • parameters

The integers 5 and 12 are arguments in the following code:

for i in range(5, 12):
    print(i)

An argument is the data brought into a function when it is called. In this case, 5 and 12 are brought into the range() function when it is called.

2. What is the correct way to define the function addition() if it requires the two parameters num1 and num2?

  • def addition(num1)(num2):
  • def addition(num1 and num2):
  • def addition(num1 num2):
  • def addition(num1, num2): (CORRECT)

The correct way to define the function addition() if it requires the two parameters num1 and num2 is def addition(num1, num2):. If a function requires multiple parameters, you should place them in parentheses and separate them with commas when defining the function.

3. Which of the following lines of code has correct syntax for printing the data type of the string “elarson”?

  • print(“elarson”, type)
  • print(type(“elarson”)) (CORRECT)
  • type(print(“elarson”))
  • print(type, “elarson”)

The code print(type(“elarson”)) has correct syntax for printing the data type of the string “elarson”. The inner function is processed first, and then its returned value is passed to the outer function. The argument “elarson” is first passed into the type() function. It returns its data type, and this is passed into the print() function.

4. Which function definition includes the correct syntax for returning the value of the result variable from the doubles() function?

def doubles(num):
    result = num * 2
    return = result
def doubles(num):
    result = num * 2
    result return
def doubles(num):
    result = num * 2
    return "result"
def doubles(num):
    result = num * 2
    return result     (CORRECT)

The following block of code demonstrates the correct syntax for returning the value of the result variable from the doubles() function:

def doubles(num):
    result = num * 2
    return result     (CORRECT)

The return keyword is used to return information from a function. It is placed before the information that you want to return. In this case, that is the result variable.

5. The following block of code defines and calls a function. Which component of the code is an argument?

def display_username(username):
    print("Username is", username)
display_username("bmoreno")
  • username
  • “bmoreno” (CORRECT)
  • print()
  • display_username()

TEST YOUR KNOWLEDGE: LEARN FROM THE PYTHON COMMUNITY

1. Which of these is not included in the Python Standard Library?

  • csv
  • time
  • re
  • NumPy (CORRECT)

The NumPy library is not included in the Python Standard Library. It is an external library that must be downloaded.

2. Which of the following resources provides recommendations about including comments in your code?

  • Python Standard Library
  • re
  • csv
  • PEP 8 (CORRECT)

The PEP 8 style guide is a resource that provides stylistic guidelines for programmers working in Python, including recommendations about comments. This includes guidelines such as making comments clear and keeping them up-to-date when code changes.

3. Which of the following code blocks have correct indentation?

if username == "elarson":
    print("Welcome, elarson!") (CORRECT)
if username == "elarson":
print("Welcome, elarson!")
if username == "elarson":
print("Welcome, elarson!")
if username == "elarson":
    print("Welcome, elarson!")

The following block of code demonstrates correct indentation:

if username == "elarson":
    print("Welcome, elarson!") (CORRECT)

The body of a conditional statement, which is the print() function in this case, must be indented for the code to execute properly.

4. What is a Python module?

  • A resource that provides stylistic guidelines for programmers working in Python
  • A text file that contains cybersecurity-related data
  • A Python file that contains additional functions, variables, and any kind of runnable code (CORRECT)
  • A Python function that exists within Python and can be called directly

A Python module is a Python file that contains additional functions, variables, and any kind of runnable code.

5. What is the difference between a module and a library in Python?

  • A library is a Python file that contains additional functions, variables, and other kinds of runnable code. A Python module is a collection of libraries.
  • Python libraries contain variables, but Python modules do not.
  • A module is a Python file that contains additional functions, variables, and other kinds of runnable code. A Python library is a collection of modules. (CORRECT)
  • Python libraries contain functions, but Python modules do not.

A module is a Python file that contains additional functions, variables, and other kinds of runnable code. A Python library is a collection of modules.

MODULE 2 CHALLENGE

1. Which of the following choices is a valid header in a function definition?

  • def remove_user(username): (CORRECT)
  • def remove_user(username)
  • def (remove_user(username))
  • remove_user(username):

2. Which of the following calls to the type() function uses correct syntax?

  • type[81, 55, 17]
  • type[(81, 17)]
  • type([55, 81, 17]) (CORRECT)
  • type([17, 81]):

3. What is a parameter?

  • A variable returned from a function
  • The name of a function that is being defined
  • An object that is included in a function definition for use in that function (CORRECT)
  • The data brought into a function when it is called

4. When working in Python, what is a library?

  • A collection of stylistic guidelines for working with Python
  • A collection of modules that provide code users can access in their programs (CORRECT)
  • A Python file that contains additional functions, variables, classes, and any kind of runnable code
  • A module that allows you to work with a particular type of file

5. What does this line of code return?

print(max(1,3,7))
  • 7 (CORRECT)
  • 1
  • 3
  • 11

6. What is returned from the following user-defined function if you pass it the arguments 2 and 3?

def add(num1, num2):
    result = num1 + num2
    return result
add(2, 3)
  • 1
  • 3
  • 2
  • 5 (CORRECT)

7. Which of the following choices is a resource that provides stylistic guidelines for programmers working in Python?

  • PEP 8 (CORRECT)
  • re
  • Python Standard Library
  • glob

8. What should you do when writing comments? Select all that apply.

  • Make them clear. (CORRECT)
  • Place them before every line of code.
  • Only place them at the beginning of a program.
  • Keep them up-to-date. (CORRECT)

9. What are built-in functions?

  • Functions that take parameters
  • Functions that exist with Python and can be called directly (CORRECT)
  • Functions that return information
  • Functions that a programmer builds for their specific needs

10. Fill in the blank: A Python file that contains additional functions, variables, classes, and any kind of runnable code is called a _____.

  • module (CORRECT)
  • library
  • parameter
  • built-in function

11. Fill in the blank: The re, csv, glob, and time modules are all _____.

  • built-in functions
  • part of the Python Standard Library (CORRECT)
  • keywords in a function header
  • part of PEP 8

12. What does this line of code return?

print(sorted(["h32rb17", "p52jb81", "k11ry83"]))
  •  [“p52jb81”, “k11ry83”, “h32rb17”]
  •  [“h32rb17”]
  •  [“p52jb81”]
  •  [“h32rb17”, “k11ry83”, “p52jb81”] (CORRECT)

13. What is returned from the following user-defined function if you pass it the argument 9?

def subtract(num):
    total = 100 - num
    return total
subtract(9)
  • 91 (CORRECT)
  • 9
  • 9.0
  • 100

14. What does PEP 8 contain?

  • A collection of modules that users can access in their programs
  • Suggestions for making Python easier to learn
  • Stylistic guidelines for programmers working in Python (CORRECT)
  • Files with additional functions users can use in their code

15. What is an advantage of including this comment in the following code? Select all that apply.

# For loop iterates to print an alert message 5 times
for i in range(5):
    print("alert")
  • It can help you understand the code if you revisit it in the future. (CORRECT)
  • It can help other programmers understand the purpose of this loop. (CORRECT)
  • It is displayed in the output when the code is run in Python.
  • It ensures the loop will function when the code is run in Python.

16. Which of the following statements accurately describe functions? Select all that apply.

  • When functions are updated, the changes are applied everywhere they are used. (CORRECT)
  • Functions are useful for automation. (CORRECT)
  • Functions can be used no more than 10 times from within a single program.
  • Functions can be reused throughout a program. (CORRECT)

17. Fill in the blank: A Python file that contains additional functions, variables, classes, and any kind of runnable code is called a _____.

  • parameter
  • library
  • module (CORRECT)
  • built-in function

18. Which of the following components are part of the header in a function definition? Select all that apply.

  • The parameters used in a function (CORRECT)
  • The name of the function (CORRECT)
  • The keyword return
  • The keyword def (CORRECT)

19. In the following code, what is the argument?

def welcome_user(name):
    print("Welcome," name)
username="elarson"
welcome_user(username)
  • welcome_user
  • username (CORRECT)
  • name
  • def

20. Fill in the blank: A collection of modules that users can access in their programs is a _____.

  • style guide
  • library
  • user-defined function
  • built-in function

21. Why are comments useful? Select three answers.

  • They explain the code to other programmers. (CORRECT)
  • They make the code run faster.
  • They provide insight on what the code does. (CORRECT)
  • They make debugging easier later on. (CORRECT)

22. What is a function?

  • A reusable section of code
  • A downloadable resource with code instructions
  • A set of stylistic guidelines for working in Python
  • A Python file that contains runnable code

23. You imported a Python module, what do you now have access to in Python?

  • A function that exists within Python and can be called directly
  • A manual that informs the writing, formatting, and design of documents
  • Additional functions, variables, classes, and other kinds of runnable code (CORRECT)
  • A list of comments that you have included in previous code

24. What can a style guide help you with when working with Python? Select two answers.

  • Making your code more consistent (CORRECT)
  • Making it easier for other programmers to understand your code (CORRECT)
  • Finding ways to make your code more complex
  • Finding new modules you can incorporate into your code

25. Which of the following calls to the sorted() function uses correct syntax?

  • sorted([532, 73, 85]) (CORRECT)
  • sorted[(85, 523, 73)]
  • sorted():
  • sorted[73, 85, 532]

26. Review the following code. Which of these statements accurately describes name?

def echo(name):
    return name * 3
  • It is an argument because it is used in a return statement.
  • It is a parameter because it is used in a return statement.
  • It is an argument because it is included in the function call.
  • It is a parameter because it is included in the function definition. (CORRECT)

27. Fill in the blank: Python modules are files that _____.

  • contain libraries
  • only contain built-in functions
  • contain any kind of runnable code (CORRECT)
  • only contain new variables

28. What does this line of code return?

print(type("h32rb17"))
  • h32rb17
  • str (CORRECT)
  • “h32rb17”
  • int

CONCLUSION – Write Effective Code Python

In conclusion, this advanced Python programming course equips participants with a robust set of skills essential for navigating intricate cybersecurity challenges. By delving into advanced Python concepts such as pre-built and user-defined functions, module utilization, and code readability, learners have fortified their programming capabilities.

The emphasis on writing clear and comprehensible code not only enhances individual proficiency but also fosters collaborative development practices crucial in real-world cybersecurity scenarios. As participants conclude this course, they stand well-prepared to apply their refined Python skills to address complex issues, contributing effectively to cybersecurity initiatives and projects.