COURSE 7 – AUTOMATE CYBERSECURITY TASKS WITH PYTHON

Module 1: Introduction to Python

GOOGLE CYBERSECURITY PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

INTRODUCTION – Introduction to Python

In this comprehensive overview, participants will embark on an illuminating journey into the realm of Python programming language and its indispensable role in the field of cybersecurity. The course offers a foundational introduction to Python, unraveling its relevance and applications within the intricate landscape of cybersecurity. Participants will delve into the fundamental concepts of Python, gaining insights into crucial aspects such as data types, variables, conditional statements, and iterative statements.

By exploring the symbiotic relationship between Python and cybersecurity, learners will grasp how this versatile programming language becomes a powerful tool for addressing and mitigating cyber threats. The foundational Python concepts covered in this module serve as building blocks, empowering participants with the skills necessary to navigate and leverage Python effectively in various cybersecurity scenarios. This comprehensive exploration ensures that participants not only comprehend the theoretical underpinnings of Python but also acquire practical proficiency, positioning them as adept practitioners in the dynamic field of cybersecurity.

Learning Objectives

  • Explain how the Python programming language is used in security.
  • Describe how various data types are handled in Python.
  • Incorporate variables into Python code.
  • Write conditional statements in Python.
  • Write iterative statements in Python.

TEST YOUR KNOWLEDGE: INTRODUCTION TO PYTHON PROGRAMMING IN CYBERSECURITY

1. What tasks would a security analyst most likely automate with Python? Select three answers.

  • Managing an access control list (CORRECT)
  • Analyzing network traffic (CORRECT)
  • Sorting through a log file (CORRECT)
  • Addressing an unusual cybersecurity concern

A security analyst would most likely automate the following tasks with Python: sorting through a log file, managing an access control list, and analyzing network traffic. Python is most commonly used in cybersecurity to automate common and repetitive tasks.

2. What are some benefits of using Python in security? Select all that apply.

  • Python reduces manual effort. (CORRECT)
  • Python can combine separate tasks into one workstream. (CORRECT)
  • Python is the only language that creates a specific set of instructions to execute tasks.
  • Python helps automate short, simple tasks. (CORRECT)

Python reduces the manual effort needed to perform common and repetitive tasks. It helps automate short, simple tasks and can combine separate tasks into one workstream.

3. Which of the following code blocks contains a valid Python comment?

This prints a “Try again” message

  • print(“Try again”)
  • : This prints a “Try again” message
  • print(“Try again”)
  • comment: This prints a “Try again” message
  • print(“Try again”)
  •  # This prints a “Try again” message
  • print(“Try again”) (CORRECT)

The following code block contains a valid Python comment:

  • # This prints a “Try again” message
  • print(“Try again”)

A comment is a note programmers make about the intention behind their code. Comments begin with the hash symbol (#).

4. Which line of code outputs the string “invalid username” to the screen?

  • print(invalid username)
  • # print(“invalid username”)
  • print(“invalid username”) (CORRECT)
  • print(#invalid username#)

The code print(“invalid username”) outputs the string “invalid username” to the screen. The print() function outputs the object specified inside the parentheses to the screen. To output a string, it must be placed in quotation marks.

5. Why might a security analyst choose Python to automate tasks? Select three answers.

  • Python programmers can follow standard guidelines. (CORRECT)
  • Python runs faster than other programming languages.
  • Python programmers can find a lot of support online. (CORRECT)
  • Python resembles human language and is easy to read. (CORRECT)

A security analyst might choose Python to automate tasks because they can find a lot of support online and follow standard guidelines. An analyst might also choose Python to automate tasks because it resembles human language and is easy to read.

6. Which of the following options is a Python comment?

  • % Display authorized users
  • print(“username”)
  • # Print authorized usernames (CORRECT)
  • “username authorized”

# Print authorized usernames is a Python comment. Comments are notes that programmers make about the intention behind their code, and they begin with the # symbol.

TEST YOUR KNOWLEDGE: CORE PYTHON COMPONENTS

1. Which of the following data items are float data? Select all that apply.

  • “5.2”
  • 8
  • 15.0 (CORRECT)
  • -2.11 (CORRECT)

15.0 and -2.11 are examples of float data. Float data is data consisting of a number with a decimal point.

2. What code displays the data type of the variable username?

username = ["elarson", "bmoreno", "tshah"]
type(username) = data_type
print(data_type)
username = ["elarson", "bmoreno", "tshah"]
data_type = type()
print(data_type)
username = ["elarson", "bmoreno", "tshah"]
data_type = username
print(data_type)
username = ["elarson", "bmoreno", "tshah"]
data_type = type(username)
print(data_type) (CORRECT)

The data type of the variable username is displayed by the following code:

username = ["elarson", "bmoreno", "tshah"]
data_type = type(username) (CORRECT)
print(data_type)

The type() function returns the data type of its input. In this case, that input is the username variable, which contains a list. This data type is assigned to the data_type variable and is displayed through the print() function.

3. In the following code, what is the data type of login_success?

login_success = ["success", "success", "fail", "success"]
  • String
  • Integer
  • List (CORRECT)
  • Boolean

List is the data type of login_success. List data is a data structure that consists of a collection of data in sequential form. Lists are placed in brackets.

4. What is the output of the following code?

failed_attempts = 3
failed_attempts = 4
print(failed_attempts)
  • 4 (CORRECT)
  • 7
  • 3, 4
  • 3

The output of the code is 4. This code initially assigns the value of failed_attempts to 3, but it then reassigns the value of this variable to 4 before printing it. The print() function is placed after this and displays this reassigned value of  4.

5. Which data type can only have a value of True or False?

  • Boolean (CORRECT)
  • Float
  • String
  • Integer

The Boolean data type can only have a value of True or False. Boolean data is data that can only be one of two values: either True or False.

6. Which of the following lines of code assigns the variable username a value of “jrafael”?

  • “jrafael” = username
  • print(“jrafael”, username)
  • print(username, “jrafael”)
  • username = “jrafael” (CORRECT)

The code username = “jrafael”  assigns the variable username a value of “jrafael”. The syntax for assigning a variable requires a name for the variable, then an equals sign (=), and finally the value for the variable.

TEST YOUR KNOWLEDGE: CONDITIONAL AND ITERATIVE STATEMENTS

1. What will the following code display?

ip_address = "192.168.183.51"
if ip_address == "192.168.183.51":
    print("You're logged in.")
else:
    print("Login failed, try again.")
  • Both “You’re logged in.” and “Login failed, try again.”
  • “Login failed, try again.”
  • Nothing
  • “You’re logged in.” (CORRECT)

The code will display “You’re logged in.” The condition in the if statement requires the ip_address variable to contain a value of “192.168.183.51”. Because this condition evaluates to True, Python will perform the action specified in the body of the if statement. In this case, it displays the message “You’re logged in.” The action specified in the body of the else statement will only execute when the condition in the if statement evaluates to False, so it will not print “Login failed, try again.”

2. Which conditional statement prints the message “account locked” when the value of failed_logins is 3 or higher?

if failed_login_count > 3:
    print("account locked")
if failed_login_count != 3:
    print("account locked")
if failed_logins >= 3:
    print("account locked") (CORRECT)
if failed_login_count == 3:
    print("account locked")

The following conditional statement prints the message “account locked” when the value of failed_logins is 3 or higher:

if failed_logins >= 3:
    print("account locked") 

This condition checks if failed_logins is assigned a value that is greater than or equal to 3. The operator >= represents greater than or equal to. When this condition is met, the body prints the “account locked” message. 

3. Which code prints all numbers from 3 to 7?

for i in range(3, 7):
    print(i)
for i in range(3, 8):
    print(i) (CORRECT)
for i in range(8):
    print(i)
for i in range(3, 4, 5, 6, 7):
    print(i)

The following code prints all numbers from 3 to 7:

for i in range(3, 8):
    print(i)

The range() function generates a sequence of numbers. With range(3, 8), the sequence will start at 3 and end at 7. This is because the number in the first position, 3, is included in the sequence, but the number in the second position, 8, is excluded. 

4. How many times does the following code print the “security alert” message?

count = 0
while count < 10:
    print("security alert")
    count = count + 1
  • 10 (CORRECT)
  • 5
  • 0
  • 9

This code will print “security alert” ten times. This is because the count variable is assigned an initial value of 0. It then increments by 1 with each iteration of the loop until the condition instructs it to stop at 10.

5. Which operator can be used in a condition to evaluate whether the value contained in a login_attempts variable matches a value of 5?

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

The == operator evaluates whether two objects match and can be used in a condition to evaluate whether the value contained in a login_attempts variable matches a value of 5. This condition can be placed in an if statement header as if login_attempts == 5.

MODULE 1 CHALLENGE

1. Fill in the blank: Automation is _____.

  • the use of technology to reduce human and manual effort to perform common and repetitive tasks
  • the replacement of existing technology (CORRECT)
  • the use of human and manual effort to reduce technological power consumption
  • the combination of technology and manual effort to complete a task

2. What is wrong with the following code?

for username in failed_login:
print(username)
  • The line with print(username) is not indented. (CORRECT)
  • The line with for username in failed_login: is not indented.
  • Both lines are not indented.
  • The first line should be split in two, and in failed_login: should be indented on the new line.

3. Fill in the blank: String data _____.

  • must be placed in brackets
  • must include a decimal point
  • must be placed in quotation marks (CORRECT)
  • must be placed in parentheses

4. Which data type always has a value of either True or False?

  • Boolean (CORRECT)
  • Float
  • List
  • String

5. Which line of code assigns the string “dtanaka” to a variable called username?

  • “dtanaka” = username
  • username(“dtanaka”)
  • username = dtanaka
  • username = “dtanaka” (CORRECT)

6. What will this code do when you run it?

var2 = ["a","b","c"]
var2_type = type(var2)
print(var2_type)
  • Change the data type of var2
  • Output the characters “a”, “b”, and “c” to the screen
  • Indicate that var2 contains list data (CORRECT)
  • Print the string “var2_type” to the screen

7. You are checking whether the string stored in a device_id variable matches to the correct device ID, the string “15hgu3769”. When it matches, you want to print, “Login successful!”. Which conditional statement has the correct syntax needed to do this?

if device_id == "15hgu3769":
    print("Login successful!") (CORRECT)
if "device_id" = "15hgu3769"
    print("Login successful!")
if device_id != "15hgu3769"
    print("Login successful!")
if "device_id == 15hgu3769"
    print("Login successful!")

8. Fill in the blank: An else statement _____.

  • contains its own unique condition
  • executes when the condition in the if statement preceding it evaluates to False (CORRECT)
  • executes when the condition in the if statement preceding it evaluates to True
  • is required after every if statement

9. What iterative statement should you use if you want to print the numbers 1, 2, and 3?

for i in [1,3]:
    print(i)
for i in range(0,3):
    print(i)
for i in [1, 2, 3]:
    print(i) (CORRECT)
for i in range(1,3):
    print(i)

10. You want to print all even numbers between 0 and 10 (in other words, 0, 2, 4, 6, 8, and 10). What should your next line of code be?

count = 0
while count <= 10:
    print(count)
  • count = count + 1
  • if count < 10:
  • count = count + 2 (CORRECT)
  • count = 1

11. Which of these are string data? Select all that apply.

  •  [100, 200, 300]
  • “100” (CORRECT)
  • 100
  • “user1” (CORRECT)

12. What are possible values for the Boolean data type? Select all that apply.

  • True (CORRECT)
  • !=
  • False (CORRECT)

13. You are implementing security measures on a server. If a user has more than 3 failed login attempts, the program should print “locked out”. The number of failed login attempts is stored in a variable called failed_attempts. Which conditional statement has the correct syntax needed to do this?

  • if failed_attempts >= 3
  •     print(“locked out”)
if failed_attempts >= 3
    print("locked out")
if failed_attempts < 3
    print("locked out")
if failed_attempts > 3:
    print("locked out") (CORRECT)
if failed_attempts <= 3:
    print("locked out")

14. In a cybersecurity setting, which of these tasks would it be common to apply Python to? Select all that apply.

  • Reducing the effort needed to manage an access control list (CORRECT)
  • Automating several tasks from a playbook into one workstream (CORRECT)
  • Automating how a log is read when responding to an incident (CORRECT)
  • Manually checking individual timestamps in a log

15. What data type requires quotation marks (” “)?

  • String (CORRECT)
  • Boolean
  • Integer
  • Float

16. Which line of Python code would create a Boolean value of True?

  • print(25<24)
  • print(“True”)
  • print(10<100) (CORRECT)
  • print([“Boolean”])

17. What are the variables in the following code? Select all that apply.

username = "kcarter"
attempts = 5
print(username)
print(attempts)
print("locked")
  • attempts (CORRECT)
  • username (CORRECT)
  • “kcarter”
  • “locked”

18. Fill in the blank: If you ran the following code, the output would _____.

var1 = 9.5
var1_type = type(var1)
print(var1_type)
  • reassign var1 as float data
  • indicate that var1 contains float data (CORRECT)
  • output 9.5 to the screen
  • reassign var1 as string data

19. You wrote the following code:

if attempts >= 5:
    print("locked")
else:
    print("try again")

If the value in the attempts variable is 3, what will Python do?

  • First output the message “try again” and then output the message “locked”
  • Output the message “try again” (CORRECT)
  • First output the message “locked” and then output the message “try again”
  • Output the message “locked”

20. What will this iterative statement do?

for i in [0, 5]:
    print(i)
  • Output the integers 0, 1, 2, 3, and 4
  • Output the integer 0
  • Output the integers 0 and 5 (CORRECT)
  • Output the integers 0, 1, 2, 3, 4, and 5

21. If you want to run a loop that repeats if a count variable is less than 50, what code should your loop header contain?

  • while count < 50: (CORRECT)
  • while count == 50:
  • print(50)
  • count = count + 50

22. Fill in the blank: If you use Python code to reduce the manual effort needed to manage an access control list, this is an example of _____.

  • data analysis
  • debugging
  • reassignment
  • automation (CORRECT)

23. What is the syntax problem in the following code?

if username == "aestrada":
print("username found")
  • The first line should be indented one space, and the second line should be indented two spaces.
  • The line with if username == “aestrada”: is not indented.
  • The line with print(“username found”) is not indented. (CORRECT)
  • Both lines are not indented.

24. How do you assign the string value “rtp3426” to a variable called device_id?

  • device_id = “rtp3426” (CORRECT)
  • device_id(rtp3426)
  • device_id = rtp3426
  • device_id(“rtp3426”)

25. What code can you use to return the data type of the value stored in the input variable?

  • print(“type”)
  • print(input)
  • type(“string”)
  • type(input) (CORRECT)

26. How many times will the following code print the “warning” message?

count = 1
while count < 5:
    print("warning")
    count = count + 1
  • 1
  • 0
  • 4 (CORRECT)
  • 5

27. The purpose of the following code is to print an “Attempting connection” message while the value of the count variable is less than 10. The value of count should increase by 1 with each iteration of the loop. What is wrong with the code? Select all that apply.

count = 1
while count < 10:
print("Attempting connection")
count = count + 1
  • The line with while count < 10: is not indented.
  • The line with count = 1 is not indented
  • The line with count = count + 1 is not indented. (CORRECT)
  • The line with print(“Attempting connection”) is not indented. (CORRECT)

28. You want to check the string stored in an update_status variable. When it contains a value of “incomplete”, you want to print a “schedule update” message. Right now, this conditional statement is not correct. What are the problems with this conditional statement? Select all that apply.

if update_status != "incomplete"
    print("schedule update")
  • The operator should not be !=. It should be ==.  (CORRECT)
  • A colon (:) is missing at the end of the conditional header. (CORRECT)
  • The line with print(“schedule update”) should not be indented.
  • There should be quotation marks around the variable update_status.

29. You have written the following code:

if operating_system == "OS 3":
    print("Updates needed")

You want to add to it so that it will print a “No updates needed” message whenever the value of operating_system is not “OS 3”. Which lines of code have the correct syntax to do this?

else
    print("No updates needed")
elif operating_system == "OS 3":
    print("No updates needed")
else:
    print("No updates needed") (CORRECT)
else operating_system != "OS 3":
    print("No updates needed")

CONCLUSION – Introduction to Python

In conclusion, this foundational course on Python programming within the cybersecurity domain provides participants with a robust understanding of essential concepts and their practical applications. As learners navigate through the intricacies of data types, variables, and programming structures, they acquire a solid foundation for leveraging Python in addressing cybersecurity challenges.

This comprehensive overview not only equips participants with theoretical knowledge but also cultivates practical skills, preparing them to apply Python effectively in real-world cybersecurity scenarios. With this newfound expertise, participants are well-positioned to contribute meaningfully to the ever-evolving landscape of cybersecurity through proficient Python programming.