diff --git a/Activity_Assign Python variables.ipynb b/Activity_Assign Python variables.ipynb new file mode 100644 index 0000000..570d309 --- /dev/null +++ b/Activity_Assign Python variables.ipynb @@ -0,0 +1,1057 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "ISr45HAPh1Xq" + }, + "source": [ + "# Activity: Assign Python variables" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ivt8p77hCKcO" + }, + "source": [ + "## Introduction\n", + "\n", + "Variables help security analysts to keep track of a variety of security-related information. For example, analysts may need to create Python variables for the users who are allowed to log in, the number of login attempts that they're permitted, and the current number of attempts that a user has made.\n", + "\n", + "In this lab, you'll practice assigning values to variables and determining their data types." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eW4pA4LsC-vb" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task.\n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook.\n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook.\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "A8q3M1L7isIj" + }, + "source": [ + "## Scenario\n", + "You are a security analyst who is responsible for writing code that will automate analysis of login attempts made to a specific device. As the first step, you'll need to create variables to keep track of information relevant to the login process. This information includes the device ID, list of approved usernames, maximum login attempts allowed per user, current login attempts made by a user, and login status.\n", + "\n", + "Throughout this lab, you'll assign these variables and check the data types of the variables." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4XkIkgiQpk34", + "tags": [] + }, + "source": [ + "## Task 1\n", + "In your work as an analyst, imagine there is a device only users specified on an allow list can access, and its device ID is `\"72e08x0\"`.\n", + "\n", + "In the following code cell, assign this value to a variable named `device_id`. Then, display the contents of the variable and observe the output.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "zAVwov6JJQS7" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "72e08x0\n" + ] + } + ], + "source": [ + "# MY RESPONSES ARE BELOW~!!!!!!\n", + "# Assign the `device_id` variable to the device ID that only specified users can access\n", + "\n", + "device_id = \"72e08x0\"\n", + "\n", + "# Display `device_id`\n", + "\n", + "print(device_id)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "F30VpnPtUcW5", + "tags": [] + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use quotation marks around the value that you assign to the `device_id` variable.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jj2jLLzLUcW5" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Assign the value `\"72e08x0\"` to the variable `device_id`. To do this, place the value to right of the `=` operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qpHMOi8vUcW5" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To display the contents of `device_id`, place the name of this variable inside the `print()` function.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Xky9BjthiYqm" + }, + "source": [ + "## Task 2\n", + "Now that the variable `device_id` is defined, you can return its data type.\n", + "\n", + "In this task, use a Python function to find the data type of the variable `device_id`. Store the data type in another variable called `device_id_type`. Then, display `device_id_type` to examine the output.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "id": "9w0g21J7i01H" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# Assign the `device_id` variable to the device ID that only specified users can access\n", + "\n", + "device_id = \"72e08x0\"\n", + "\n", + "# Assign `device_id_type` to the data type of `device_id`\n", + "\n", + "device_id_type = type(device_id)\n", + "\n", + "# Display `device_id_type`\n", + "\n", + "print(device_id_type)\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SF-DtKYyrLak", + "tags": [] + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `type()` function allows you to get the data type of a given value.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PEcJeExRrLat" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use `type(device_id)` to get the data type of the `device_id` variable.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fLVHruphrp49" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To display `device_id_type`, place the name of this variable inside the `print()` function.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "g_03rNzrd8F0" + }, + "source": [ + "#### **Question 1**\n", + "**Based on the output above, what do you observe about the data type of `device_id`?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yxv9ICX_egCB" + }, + "source": [ + "After puting type to device_id_type and then inserting the device_id to it, it outputs when device_id_type is called. This means it is a string type." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QP05AxAwi_vL" + }, + "source": [ + "## Task 3\n", + "As you continue your work, you're provided a list of usernames of users who are allowed to access the device. The usernames with this access are `\"madebowa\"`, `\"jnguyen\"`, `\"tbecker\"`, `\"nhersh\"`, and `\"redwards\"`.\n", + "\n", + "In this task, create a variable called `username_list`. Assign a list with the approved usernames to this variable. Then, display the value of the `username_list` variable.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "cgmlPvWLkBEM" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['madebowa', 'jnguyen', 'tbecker', 'nhersh', 'redwards']\n" + ] + } + ], + "source": [ + "# Assign `username_list` to the list of usernames who are allowed to access the device\n", + "\n", + "username_list = [\"madebowa\", \"jnguyen\", \"tbecker\", \"nhersh\", \"redwards\"]\n", + "\n", + "# Display `username_list`\n", + "\n", + "print(username_list)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9zon3fxlbZiG" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To create a list in Python, use square brackets. Inside the square brackets, write the elements of the list, with a comma between elements.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J73Rv91PUoES" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To assign a value to a variable in Python, place the name of the variable to the left of the `=` operator, and place the value to the right of the `=` operator.\n", + "\n", + "In this task, make sure to place `[\"madebowa\", \"jnguyen\", \"tbecker\", \"nhersh\", \"redwards\"]` to the right of the `=` operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OQTnnv6TbZiG" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To display `username_list`, place the name of this variable inside the `print()` function.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "89fOQheQ076R" + }, + "source": [ + "## Task 4\n", + "In this task, find the data type of the `username_list`. Store the type in a variable called `username_list_type`. Then, display `username_list_type` to examine the output.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "id": "Yzpvr9WJ1Pi8" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Answer:\n", + "\n", + "\n", + "Now for Alt (Specific) is below:\n", + "\n" + ] + } + ], + "source": [ + "# Assign `username_list` to the list of usernames who are allowed to access the device\n", + "\n", + "username_list = [\"madebowa\", \"jnguyen\", \"tbecker\", \"nhersh\", \"redwards\"]\n", + "\n", + "# Assign `username_list_type` to the data type of `username_list`\n", + "\n", + "username_list_type = type(username_list)\n", + "\n", + "# Display `username_list_type`\n", + "print(\"Answer:\")\n", + "print(username_list_type)\n", + "\n", + "# Optionla: Alternative method (Specific)\n", + "print()\n", + "print(\"Now for Alt (Specific) is below:\")\n", + "username_list_type = type(username_list[0])\n", + "print(username_list_type)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_A6GgpvQcEdv" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `type()` function allows you to get the data type of a given value.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sk0EVD3ecEdw" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To get the data type of `username_list`, call the `type()` function and pass in `username_list`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pEetodFVcEdw" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "In Python, you can use the `print()` function to display the value of a variable. To display `username_list_type`, call the `print()` function and pass in `username_list_type`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2Q916P--eiMt" + }, + "source": [ + "#### **Question 2**\n", + "**Based on the output above, what do you observe about the data type of `username_list`?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a__dHuhGeiMt" + }, + "source": [ + "After puting type to username_list_type and then inserting the username_list to it, it outputs when username_list_type is called. This means it is a list type. However, if you change the code as below the print to alternative method and be very specific of what is username_list like username_list[0], you will get the type of the first element in the list which is a string type." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cvV_L70okWNI" + }, + "source": [ + "## Task 5\n", + "Now, imagine that you've been informed that the previous list is not up-to-date and that there is another employee that now has access to the device. You're given the updated list of usernames with access, including the new employee, as follows: `\"madebowa\"`, `\"jnguyen\"`, `\"tbecker\"`, `\"nhersh\"`, `\"redwards\"`, and `\"lpope\"`.\n", + "\n", + "In this task, reassign the variable `username_list` to the new list. Run the code to display the list before and after it's been updated to observe the difference.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "fLt9BzUFkXmG" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['madebowa', 'jnguyen', 'tbecker', 'nhersh', 'redwards']\n", + "['madebowa', 'jnguyen', 'tbecker', 'nhersh', 'redwards', 'lpope']\n" + ] + } + ], + "source": [ + "# Assign `username_list` to the list of usernames who are allowed to access the device\n", + "\n", + "username_list = [\"madebowa\", \"jnguyen\", \"tbecker\", \"nhersh\", \"redwards\"]\n", + "\n", + "# Display `username_list`\n", + "\n", + "print(username_list)\n", + "\n", + "# Assign `username_list` to the updated list of usernames who are allowed to access the device\n", + "\n", + "username_list = [\"madebowa\", \"jnguyen\", \"tbecker\", \"nhersh\", \"redwards\", \"lpope\"]\n", + "\n", + "# Display `username_list`\n", + "\n", + "print(username_list)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "02bW-FQSgYX7" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "When reassigning a variable to a new value in Python, place the name of the variable to the left of the `=` operator, just as you would when assigning the variable for the first time.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ETcEZOqnU1pz" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To reassign `username_list` to the updated list, place `username_list` to the left of the `=` operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JvpvqtW6g-QT" + }, + "source": [ + "#### **Question 3**\n", + "**Based on the output above, what do you observe about the contents of `username_list`?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OavH92wXg-QU" + }, + "source": [ + "After putting type to username_list and then printing out username_list to it, it outputs ['madebowa', 'jnguyen', 'tbecker', 'nhersh', 'redwards'] when username_list is called. The second one, I noticed just change the list but when called it has a differnt output. After puting type to username_list and then printing out username_list to it, it outputs ['madebowa', 'jnguyen', 'tbecker', 'nhersh', 'redwards', 'lpope'] when username_list is called. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iYDV65CclQlc" + }, + "source": [ + "## Task 6\n", + "In this task, define a variable called `max_logins` that represents the maximum number of login attempts allowed per user. Store the value `3` in this variable. Then, store its data type in another variable called `max_logins_type`. Display `max_logins_type` to examine the output.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "id": "rrmY4XeNx_d7" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# Assign `max_logins` to the value 3\n", + "\n", + "max_logins = 3\n", + "\n", + "# Assign `max_logins_type` to the data type of `max_logins`\n", + "\n", + "max_logins_type = type(max_logins)\n", + "\n", + "# Display `max_logins_type`\n", + "\n", + "print(max_logins_type)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bDTRW5iDVZvb" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "When assigning a value to a variable in Python, use the `=` operator. Place the name of the variable to the left of the `=` operator, and place the value to the right of the `=` operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HnwzqIY5xknA" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To assign `3` to `max_logins`, place `max_logins` to the left of the `=` operator, and place `3` to the right of the `=` operator.\n", + "\n", + "To assign `max_logins_type`, place `max_logins_type` to the left of the `=` operator before the `type()` function call.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kQbcfpbjVZvc" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "In Python, you can use the `print()` function to display the value of a variable. To display `max_logins_type`, call `print()` and pass in `max_logins_type`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Bl35H3myh4xZ" + }, + "source": [ + "#### **Question 4**\n", + "**Based on the output above, what do you observe about the data type of `max_logins`?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MCTU3HsXh4xi" + }, + "source": [ + "After puting type to max_logins_type and then inserting the max_logins to it, it outputs when max_logins_type is called. This means it is an integer type." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "85o9hfJmyH3W" + }, + "source": [ + "## Task 7\n", + "In this task, define a variable called `login_attempts` that represents the current number of login attempts made by a user. Store the value `2` in this variable. Then, store its data type in a variable called `login_attempts_type`. Display `login_attempts_type` to observe the output.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "id": "-3YDmrBWzOFc" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# Assign `login_attempts` to the value 2\n", + "\n", + "login_attempts = 2\n", + "\n", + "# Assign `login_attempts_type` to the data type of `login_attempts`\n", + "\n", + "login_attempts_type = type(login_attempts)\n", + "\n", + "# Display `login_attempts_type`\n", + "\n", + "print(login_attempts_type)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "t-l9SR2mVeqc" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "When assigning a value to a variable in Python, use the `=` operator. Place the name of the variable to the left of the `=` operator, and place the value to the right of the `=` operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NNEn8rkpVnMQ" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To assign `2` to `login_attempts`, place `login_attempts` to the left of the `=` operator, and place `2` to the right of the `=` operator.\n", + "\n", + "To assign `login_attempts_type`, place `login_attempts_type` to the left of the `=` operator, and place a call to the `type()` function to the right of the `=` operator.\n", + "\n", + "When calling `type()`, make sure to pass in `login_attempts`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IM8picKtVnMQ" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "In Python, you can use the `print()` function to display the value of a variable. To display `login_attempts_type`, call `print()` and pass in `login_attempts_type`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sFSn1Xvuj0a8" + }, + "source": [ + "#### **Question 5**\n", + "**Based on the output above, what do you observe about the data type of `login_attempts`?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XomtDQZ2j0a9" + }, + "source": [ + "After puting type to login_attempts_type and then inserting the login_attempts to it, it outputs when login_attempts_type is called. This means it is an integer type." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JKNwC4lKzWW6" + }, + "source": [ + "## Task 8\n", + "In this task, you'll determine the Boolean value that represents whether the current number of login attempts a user has made is less than or equal to the maximum number of login attempts allowed.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "id": "2mwU0rO40CSU" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# Assign `max_logins` to the value 3\n", + "\n", + "max_logins = 3\n", + "\n", + "# Assign `login_attempts` to the value 2\n", + "\n", + "login_attempts = 2\n", + "\n", + "# Determine whether the current number of login attempts a user has made is less than or equal to the maximum number of login attempts allowed,\n", + "# and display the resulting Boolean value\n", + "\n", + "print(login_attempts <= max_logins)\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CcFDQ5iMklzz" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "In Python, you can use the `<=` comparison operator to determine whether one value is less than or equal to another value.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nmT8q0j-klzz" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To determine whether the current number of login attempts a user has made is less than or equal to the maximum number of login attempts allowed, use the `<=` operator. Place `login_attempts` to the left of the `<=` operator, and place `max_logins` to the right of the `<=` operator.\n", + "\n", + "To make sure the resulting Boolean value is displayed, write this code inside of the parantheses where `print()` is called.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RS9AaPuVklz0" + }, + "source": [ + "#### **Question 6**\n", + "**What is the output? What does this mean?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hzye4hciklz0" + }, + "source": [ + "The code print(login_attempts <= max_logins) asks the python machine is 2 <= 3 true? If so print true and if not print false. Since it prints true, this is a boolean type that outputs true for this statement 2 <=3 or \n", + "print(login_attempts <= max_logins)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rjxdlE170I70" + }, + "source": [ + "## Task 9\n", + "This code continues to check for the Boolean value of whether `max_logins` is less than or equal to `login_attempts`. In this task, reassign other values to `login_attempts`. For example, you might choose a value that is higher than the maximum number of attempts allowed. Observe how the output changes.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "id": "uIdS23Qr0NG9" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "# Assign `max_logins` to the value 3\n", + "\n", + "max_logins = 3\n", + "\n", + "# Assign `login_attempts` to a specific value\n", + "\n", + "login_attempts = 7\n", + "\n", + "# Determine whether the current number of login attempts a user has made is less than or equal to the maximum number of login attempts allowed,\n", + "# and display the resulting Boolean value\n", + "\n", + "print(login_attempts <= max_logins)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ApSY7aQamLZE" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To assign `login_attempts` to a specific value, make sure to write the value to the right of the `=` operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MbJ5mMuPmLZF" + }, + "source": [ + "#### **Question 7**\n", + "**Based on the different values you assigned to `login_attempts`, what did you observe about the output?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LPl0u_jOmLZF" + }, + "source": [ + "The code print(login_attempts <= max_logins) asks the python machine is 7 <= 3 true? If so print true and if not print false. Since it prints false, this is a boolean type that outputs false for this statement 7 <= 3 or \n", + "print(login_attempts <= max_logins)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "toglOMvH0bx4" + }, + "source": [ + "## Task 10\n", + "Finally, you can also assign a Boolean value of `True` or `False` to a variable.\n", + "\n", + "In this task, you'll create a variable called `login_status`, which is a Boolean that represents whether a user is logged in. Assign `False` to this variable and store its data type in a variable called `login_status_type` and display it.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "id": "BwjxdmYc1sDY" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# Assign `login_status` to the Boolean value False\n", + "\n", + "login_status = False\n", + "\n", + "# Assign `login_status_type` to the data type of `login_status`\n", + "\n", + "login_status_type = type(login_status)\n", + "\n", + "# Display `login_status_type`\n", + "\n", + "print(login_status_type)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "v9mAtdMHrkiS" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To assign the Boolean value `False` to the `login_status` variable, make sure to write `False` to the right of the `=` operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9LZYgEvKQ9tA" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Note that Boolean values should not have quotation marks around them in code.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EpuzbcA0rkiS" + }, + "source": [ + "#### **Question 8**\n", + "**Based on the output above, what do you observe about the data type of `login_status`?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AZAqQKoFrkiS" + }, + "source": [ + "After setting login_status to False, and then find data type with login_status_type and then inserting the login_status to it, it outputs when login_status_type is called. This means it is a bool type." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ru2ytGBLCh4q" + }, + "source": [ + "## Conclusion\n", + "\n", + "**What are your key takeaways from this lab?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ls2WEGqsiSAd" + }, + "source": [ + "In this lesson, I learned about Python variables and data types. I practiced assigning values to variables like `device_id`, `username_list`, and `max_logins`. I also explored different data types, including strings, lists, integers, and booleans, and used the `type()` function to check their types. I learned how to print and compare values using operators like `<=`, and how to manage Boolean values like `True` and `False` in conditions. The main takeaway is how important it is to understand data types and how to manipulate and check them in Python to perform various tasks like authentication or tracking login attempts." + ] + } + ], + "metadata": { + "colab": { + "collapsed_sections": [ + "JvpvqtW6g-QT", + "Bl35H3myh4xZ", + "sFSn1Xvuj0a8", + "RS9AaPuVklz0", + "MbJ5mMuPmLZF", + "EpuzbcA0rkiS" + ], + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Activity_Assign Python variables.pdf b/Activity_Assign Python variables.pdf new file mode 100644 index 0000000..ef001e6 Binary files /dev/null and b/Activity_Assign Python variables.pdf differ diff --git a/Activity_Create another algorithm.ipynb b/Activity_Create another algorithm.ipynb new file mode 100644 index 0000000..a1a426f --- /dev/null +++ b/Activity_Create another algorithm.ipynb @@ -0,0 +1,1115 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "4w9z0bsxsL4Y" + }, + "source": [ + "# Activity: Create another algorithm" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ivt8p77hCKcO" + }, + "source": [ + "## Introduction\n", + "\n", + "An important part of cybersecurity is controlling access to restricted content. In this lab, you'll work with a text file containing IP addresses that are allowed to access specific restricted content at your organization.\n", + "\n", + "Parsing a file allows security analysts to read and update the contents. Python helps analysts develop algorithms to automate the process of parsing files and keeping them up-to-date.\n", + "\n", + "You'll develop an algorithm that parses this text file of IP addresses and updates the file by removing that addresses that no longer have access to the restricted content." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eW4pA4LsC-vb" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task. \n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook. \n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook. \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dQ2elTOeCMNv" + }, + "source": [ + "## Scenario\n", + "\n", + "In this lab, you're working as a security analyst and you're responsible for developing an algorithm that parses a file containing IP addresses that are allowed to access restricted content and removes addresses that no longer have access." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zo1HFh3XsS6S" + }, + "source": [ + "## Task 1\n", + "Your eventual goal is to develop an algorithm that parses a series of IP addresses that can access restricted information and removes the addresses that are no longer allowed. Python can automate this process.\n", + "\n", + "You're given a text file called `\"allow_list.txt\"` that contains a series of IP addresses that are allowed to access restricted information. \n", + "\n", + "There are IP addresses that should no longer have access to this information, and their IP addresses need to be removed from the text file. You're given a variable named `remove_list` that contains the list of IP addresses to be removed.\n", + "\n", + "Display both variables to explore their contents, and run the cell. Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "dxeblX9m2Lva" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "allow_list.txt\n", + "['192.168.97.225', '192.168.158.170', '192.168.201.40', '192.168.58.57']\n" + ] + } + ], + "source": [ + "# Assign `import_file` to the name of the file \n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `remove_list` to a list of IP addresses that are no longer allowed to access restricted information. \n", + "\n", + "remove_list = [\"192.168.97.225\", \"192.168.158.170\", \"192.168.201.40\", \"192.168.58.57\"]\n", + "\n", + "# Display `import_file`\n", + "\n", + "print(import_file)\n", + "\n", + "# Display `remove_list`\n", + "\n", + "print(remove_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "N4iCk5h-BEW4" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To display the contents of a variable, pass it as an argument to the `print()` function.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "F5C4y6A8jWPj" + }, + "source": [ + "#### **Question 1**\n", + "**What do you observe about the output above?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_v4mWtXSmkUX" + }, + "source": [ + "I beleive after all the code changes, I noticed that the first line of the output shows the name of the text file and that the second line of the output shows the list of IP addresses from the remove_list. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "z0N_eEcRpZQS" + }, + "source": [ + "## Task 2\n", + "In this task, start by opening the text file using the `import_file` variable, the `with` keyword, and the `open()` function with the `\"r\"` parameter. Be sure to replace the `### YOUR CODE HERE ###` with your own code.\n", + "\n", + "For now, you'll write the first line of the `with` statement. Running this code will produce an error because it will only contain the first line of the `with` statement; you'll complete this `with` statement in the task after this." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "LPVPVJKC3j01" + }, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "unexpected EOF while parsing (, line 11)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m11\u001b[0m\n\u001b[0;31m with open(import_file, \"r\") as file:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unexpected EOF while parsing\n" + ] + } + ], + "source": [ + "# Assign `import_file` to the name of the file \n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `remove_list` to a list of IP addresses that are no longer allowed to access restricted information. \n", + "\n", + "remove_list = [\"192.168.97.225\", \"192.168.158.170\", \"192.168.201.40\", \"192.168.58.57\"]\n", + "\n", + "# First line of `with` statement\n", + "\n", + "with open(import_file, \"r\") as file:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vunjTntCnYSS" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `open()` function in Python allows you to open a file. \n", + "\n", + "As the first parameter, it takes in the name of the file (or a variable containing the name of the file). As the second parameter, it takes in a string that indicates how the file should be handled.\n", + "\n", + "Pass in the letter `\"r\"` as the second parameter when you want to read the file.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0MpuZ_7Y3qHB" + }, + "source": [ + "## Task 3\n", + "Now, use the `.read()` method to read the imported file and store it in a variable named `ip_addresses`. \n", + "\n", + "Afterwards, display `ip_addresses` to examine the data in its current format. \n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "zvlBO9Eh5J4H" + }, + "outputs": [], + "source": [ + "# Assign `import_file` to the name of the file \n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `remove_list` to a list of IP addresses that are no longer allowed to access restricted information. \n", + "\n", + "remove_list = [\"192.168.97.225\", \"192.168.158.170\", \"192.168.201.40\", \"192.168.58.57\"]\n", + "\n", + "# Build `with` statement to read in the initial contents of the file\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store it in a variable named `ip_addresses`\n", + "\n", + " ip_addresses = file.read()\n", + "\n", + "# Display `ip_addresses`\n", + "\n", + "print(ip_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fxll4f1K5lCm" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `.read()` method in Python allows you to read in a file.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zU2SGDi_q8_v" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Call `file.read()` to read the imported file.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pqh-zmUh5-qi" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To display the contents of a variable, pass it as an argument to the `print()` function.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MrvKB19ltc6A" + }, + "source": [ + "#### **Question 2**\n", + "**Do you notice any IP addresses in the allow list that are also in the `remove_list`?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Tk1NmzVNuIa6" + }, + "source": [ + "Yes, I do notice IP assresses in the allow list that are also in the remove_list. I can see that there are 4 IP addresses in the allow list that are also in the remove_list demonstrated through the code and output." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Iv06Y6Dv6CME" + }, + "source": [ + "## Task 4\n", + "After reading the file, reassign the `ip_addresses` variable so its data type is updated from a string to a list. Use the `.split()` method to achieve this. Adding this step will allow you to iterate through each of the IP addresses in the allow list instead of navigating a large string that contains all the addresses merged together. \n", + "\n", + "Afterwards, display the `ip_addresses` variable to verify that the update took place.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "HB-HMhEX8jzU" + }, + "outputs": [], + "source": [ + "# Assign `import_file` to the name of the file \n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `remove_list` to a list of IP addresses that are no longer allowed to access restricted information. \n", + "\n", + "remove_list = [\"192.168.97.225\", \"192.168.158.170\", \"192.168.201.40\", \"192.168.58.57\"]\n", + "\n", + "# Build `with` statement to read in the initial contents of the file\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store it in a variable named `ip_addresses`\n", + "\n", + " ip_addresses = file.read()\n", + "\n", + "# Use `.split()` to convert `ip_addresses` from a string to a list\n", + "\n", + "ip_addresses = ip_addresses.split()\n", + "\n", + "# Display `ip_addresses`\n", + "\n", + "print(ip_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1tOgXngO82g8" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `.split()` method in Python allows you to convert a string to a list. This method can take in a parameter that specifies which character to split on. If a parameter is not passed in, the method will split on whitespace by default. Note that whitespace includes any space between text on the same line and the space between one line and the next line.\n", + "\n", + "In this task, the default behavior of `.split()` works well. Each IP address is on a new line in the `allow_list.txt` file. In other words, there is whitespace between IP addresses in the text file. When you use `.split()`, it will separate the IP addresses and output them as a list.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zLtButSu82g9" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To display the contents of a variable, pass it as an argument to the `print()` function.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "083QDEoV-6ZP" + }, + "source": [ + "## Task 5\n", + "Now, you'll write code that removes the elements of `remove_list` from the `ip_addresses` list. This will require both an iterative statement and a conditional statement. \n", + "\n", + "First, build the iterative statement. Name the loop variable `element`, loop through `ip_addresses`, and display each element. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "9Liw9LIH__TN" + }, + "outputs": [], + "source": [ + "# Assign `import_file` to the name of the file \n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `remove_list` to a list of IP addresses that are no longer allowed to access restricted information. \n", + "\n", + "remove_list = [\"192.168.97.225\", \"192.168.158.170\", \"192.168.201.40\", \"192.168.58.57\"]\n", + "\n", + "# Build `with` statement to read in the initial contents of the file\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store it in a variable named `ip_addresses`\n", + "\n", + " ip_addresses = file.read()\n", + "\n", + "# Use `.split()` to convert `ip_addresses` from a string to a list\n", + "\n", + "ip_addresses = ip_addresses.split()\n", + "\n", + "# Build iterative statement\n", + "# Name loop variable `element`\n", + "# Loop through `ip_addresses`\n", + "\n", + "for element in ip_addresses:\n", + "\n", + " # Display `element` in every iteration\n", + "\n", + " print(element)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yEtm97lVAFPl" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Build a `for` loop to iterate through `ip_addresses`. Be sure to start with the `for` keyword. Use `element` as the loop variable and use `in` as the loop condition.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hLGVwvuGAFPm" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To display the contents of a variable, pass it as an argument to the `print()` function.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BJ1SEHvrCHP5" + }, + "source": [ + "## Task 6\n", + "Now, build a conditional statement to remove the elements of `remove_list` from the `ip_addresses` list. The conditional statement should be placed inside the iterative statement that loops through `ip_addresses`. In every iteration, if the current element in the `ip_addresses` list is in the `remove_list`, the `remove()` method should be used to remove that element. \n", + "\n", + "Afterwards, display the updated `ip_addresses` list to verify that the elements of remove_list are no longer in the `ip_addresses`. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "hINEYp9pEZuy" + }, + "outputs": [], + "source": [ + "# Assign `import_file` to the name of the file \n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `remove_list` to a list of IP addresses that are no longer allowed to access restricted information. \n", + "\n", + "remove_list = [\"192.168.97.225\", \"192.168.158.170\", \"192.168.201.40\", \"192.168.58.57\"]\n", + "\n", + "# Build `with` statement to read in the initial contents of the file\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store it in a variable named `ip_addresses`\n", + "\n", + " ip_addresses = file.read()\n", + "\n", + "# Use `.split()` to convert `ip_addresses` from a string to a list\n", + "\n", + "ip_addresses = ip_addresses.split()\n", + "\n", + "# Build iterative statement\n", + "# Name loop variable `element`\n", + "# Loop through `ip_addresses`\n", + "\n", + "for element in ip_addresses:\n", + " \n", + " # Build conditional statement\n", + " # If current element is in `remove_list`,\n", + "\n", + " if element in remove_list:\n", + "\n", + " # then current element should be removed from `ip_addresses`\n", + "\n", + " ip_addresses.remove(element)\n", + "\n", + "# Display `ip_addresses` \n", + "\n", + "print(ip_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vo345gwoFYBZ" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "When building the conditional statement, use the `in` operator to check if `element` is in `remove_list`. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NDPkfbp3FYBZ" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To remove `element` from `ip_addresses`, call the `.remove()` method on `ip_addresses`, and pass in `element`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r8_PSeXMF7O2" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To remove `element` from `ip_addresses`, call `ip_addresses.remove()` and pass in `element`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Y6tImjeD3h6L" + }, + "source": [ + "## Task 7\n", + "The next step is to update the original file that was used to create the `ip_addresses` list. A line of code containing the `.join()` method has been added to the code so that the file can be updated. This is necessary because `ip_addresses` must be in string format when used inside the `with` statement to rewrite the file.\n", + "\n", + "The `.join()` method takes in an iterable (such as a list) and concatenates every element of it into a string. The `.join()` method is applied to a string consisting of the character that will be used to separate every element in the iterable once its converted into a string. In the code below, the method is applied to the string `\" \"`, which contains just a space character. The argument of the `.join()` method is the iterable you want to convert, and in this case, that's `ip_addresses`. As a result, it converts `ip_addresses` from a list back into a string with a space between each element and the next.\n", + "\n", + "After this line with the `.join()` method, build the `with` statement that rewrites the original file. Use the `\"w\"` parameter when calling the `open()` function to delete the contents in the original file and replace it with what you want to write. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell. This code cell will not produce an output." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "kuCDsf-AHHLL" + }, + "outputs": [], + "source": [ + "# Assign `import_file` to the name of the file \n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `remove_list` to a list of IP addresses that are no longer allowed to access restricted information. \n", + "\n", + "remove_list = [\"192.168.97.225\", \"192.168.158.170\", \"192.168.201.40\", \"192.168.58.57\"]\n", + "\n", + "# Build `with` statement to read in the initial contents of the file\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store it in a variable named `ip_addresses`\n", + "\n", + " ip_addresses = file.read()\n", + "\n", + "# Use `.split()` to convert `ip_addresses` from a string to a list\n", + "\n", + "ip_addresses = ip_addresses.split()\n", + "\n", + "# Build iterative statement\n", + "# Name loop variable `element`\n", + "# Loop through `ip_addresses`\n", + "\n", + "for element in ip_addresses:\n", + " \n", + " # Build conditional statement\n", + " # If current element is in `remove_list`,\n", + " \n", + " if element in remove_list:\n", + "\n", + " # then current element should be removed from `ip_addresses`\n", + "\n", + " ip_addresses.remove(element)\n", + "\n", + "# Convert `ip_addresses` back to a string so that it can be written into the text file \n", + "\n", + "ip_addresses = \" \".join(ip_addresses)\n", + "\n", + "# Build `with` statement to rewrite the original file\n", + "\n", + "with open(import_file, \"w\") as file:\n", + "\n", + " # Rewrite the file, replacing its contents with `ip_addresses`\n", + "\n", + " file.write(ip_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XcVVj9ikHO_9" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To complete the first line of the `with` statement, call the `open()` function and pass in the name of the file as the first parameter and the letter `\"w\"` as the second parameter. \n", + "\n", + "The `\"w\"` parameter specifies that you're opening the file for the purpose of writing to it. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fC8HQty-HO_-" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Inside the `with` statement, call the `.write()` method to replace the contents of the file with the data stored in `ip_addresses`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3t12UIDSHO_-" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Inside the `with` statement, call `file.write()` and pass in `ip_addresses`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MPeELiLnJNMI" + }, + "source": [ + "## Task 8\n", + "In this task, you'll verify that the original file was rewritten using the correct list. \n", + "\n", + "Write another `with` statement, this time to read in the updated file. Start by opening the file. Then read the file and store its contents in the `text` variable. \n", + "\n", + "Afterwards, display the `text` variable to examine the result.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "s0dlsfrPKd_q" + }, + "outputs": [], + "source": [ + "# Assign `import_file` to the name of the file \n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `remove_list` to a list of IP addresses that are no longer allowed to access restricted information. \n", + "\n", + "remove_list = [\"192.168.97.225\", \"192.168.158.170\", \"192.168.201.40\", \"192.168.58.57\"]\n", + "\n", + "# Build `with` statement to read in the initial contents of the file\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store it in a variable named `ip_addresses`\n", + "\n", + " ip_addresses = file.read()\n", + "\n", + "# Use `.split()` to convert `ip_addresses` from a string to a list\n", + "\n", + "ip_addresses = ip_addresses.split()\n", + "\n", + "# Build iterative statement\n", + "# Name loop variable `element`\n", + "# Loop through `ip_addresses`\n", + "\n", + "for element in ip_addresses:\n", + " \n", + " # Build conditional statement\n", + " # If current element is in `remove_list`,\n", + " \n", + " if element in remove_list:\n", + "\n", + " # then current element should be removed from `ip_addresses`\n", + "\n", + " ip_addresses.remove(element)\n", + "\n", + "# Convert `ip_addresses` back to a string so that it can be written into the text file \n", + "\n", + "ip_addresses = \" \".join(ip_addresses)\n", + "\n", + "# Build `with` statement to rewrite the original file\n", + "\n", + "with open(import_file, \"w\") as file:\n", + "\n", + " # Rewrite the file, replacing its contents with `ip_addresses`\n", + "\n", + " file.write(ip_addresses)\n", + "\n", + "# Build `with` statement to read in the updated file\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Read in the updated file and store the contents in `text`\n", + "\n", + " text = file.read()\n", + "\n", + "# Display the contents of `text`\n", + "\n", + "print(text)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gy2Q1UkUKmRz" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To complete the first line of the `with` statement, call the `open()` function and pass in the name of the file as the first parameter and the letter `\"r\"` as the second parameter. \n", + "\n", + "The `\"r\"` parameter specifies that you're opening the file for the purpose of reading it. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tAXScw4mKmRz" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Inside the `with` statement, call the `.read()` method to read the contents of the file. Assign the `text` variable to the result. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0tZZIM12K7rg" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To display the contents of a variable, pass it as an argument to the `print()` function.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "R1WDqLt_K9U7" + }, + "source": [ + "## Task 9\n", + "The next step is to bring all of the code you've written leading up to this point and put it all into one function. \n", + "\n", + "Define a function named `update_file()` that takes in two parameters. The first parameter is the name of the text file that contains IP addresses (call this parameter `import_file`). The second parameter is a list that contains IP addresses to be removed (call this parameter `remove_list`).\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell. Note that this code cell will not produce an output.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "rmoz_SD4NBbj" + }, + "outputs": [], + "source": [ + "# Define a function named `update_file` that takes in two parameters: `import_file` and `remove_list`\n", + "# and combines the steps you've written in this lab leading up to this\n", + "\n", + "def update_file(import_file, remove_list):\n", + "\n", + " # Build `with` statement to read in the initial contents of the file\n", + "\n", + " with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store it in a variable named `ip_addresses`\n", + "\n", + " ip_addresses = file.read()\n", + "\n", + " # Use `.split()` to convert `ip_addresses` from a string to a list\n", + "\n", + " ip_addresses = ip_addresses.split()\n", + "\n", + " # Build iterative statement\n", + " # Name loop variable `element`\n", + " # Loop through `ip_addresses`\n", + "\n", + " for element in ip_addresses:\n", + "\n", + " # Build conditional statement\n", + " # If current element is in `remove_list`,\n", + "\n", + " if element in remove_list:\n", + "\n", + " # then current element should be removed from `ip_addresses`\n", + "\n", + " ip_addresses.remove(element)\n", + "\n", + " # Convert `ip_addresses` back to a string so that it can be written into the text file \n", + "\n", + " ip_addresses = \" \".join(ip_addresses)\n", + "\n", + " # Build `with` statement to rewrite the original file\n", + "\n", + " with open(import_file, \"w\") as file:\n", + "\n", + " # Rewrite the file, replacing its contents with `ip_addresses`\n", + "\n", + " file.write(ip_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3VpUpR6Zey9V" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `def` keyword to start the function definition. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uoGt3HqQey9f" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "After the `def` keyword, specify the name of the function, followed by parantheses and a colon. Inside the parantheses, specify the parameters that the function takes in.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WxLVS38vhOxi" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "After the `def` keyword, write `update_file(import_file, remove_list):` to complete the function definition header.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cD0hgjRLuS98" + }, + "source": [ + "#### **Question 3**\n", + "**What are the benefits of incorporating the algorithm into a single function?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Y5UWYLeDvPXh" + }, + "source": [ + "Based on the notes and how I am doing hands on leanring here in this lab, by incorporating the algorithm into a single function helps me organize the code and make it reusable. This is good because I learned that if I want to repeat/execute the algorithm more than once, I can just call the function again." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WM9ON3JkxcOL" + }, + "source": [ + "## Task 10\n", + "Finally, call the `update_file()` that you defined. Apply the function to `\"allow_list.txt\"` and pass in a list of IP addresses as the second argument.\n", + "\n", + "Use the following list of IP addresses as the second argument: \n", + "\n", + "`[\"192.168.25.60\", \"192.168.140.81\", \"192.168.203.198\"]`\n", + "\n", + "After the function call, use a `with` statement to read the contents of the allow list. Then display the contents of the allow list. Run it to verify that the file has been updated by the function.\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "YO87XukHRSC1" + }, + "outputs": [], + "source": [ + "# Define a function named `update_file` that takes in two parameters: `import_file` and `remove_list`\n", + "# and combines the steps you've written in this lab leading up to this\n", + "\n", + "def update_file(import_file, remove_list):\n", + "\n", + " # Build `with` statement to read in the initial contents of the file\n", + "\n", + " with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store it in a variable named `ip_addresses`\n", + "\n", + " ip_addresses = file.read()\n", + "\n", + " # Use `.split()` to convert `ip_addresses` from a string to a list\n", + "\n", + " ip_addresses = ip_addresses.split()\n", + "\n", + " # Build iterative statement\n", + " # Name loop variable `element`\n", + " # Loop through `ip_addresses`\n", + "\n", + " for element in ip_addresses:\n", + " \n", + " # Build conditional statement\n", + " # If current element is in `remove_list`,\n", + " \n", + " if element in remove_list:\n", + "\n", + " # then current element should be removed from `ip_addresses`\n", + "\n", + " ip_addresses.remove(element)\n", + "\n", + " # Convert `ip_addresses` back to a string so that it can be written into the text file \n", + "\n", + " ip_addresses = \" \".join(ip_addresses) \n", + "\n", + " # Build `with` statement to rewrite the original file\n", + "\n", + " with open(import_file, \"w\") as file:\n", + "\n", + " # Rewrite the file, replacing its contents with `ip_addresses`\n", + "\n", + " file.write(ip_addresses)\n", + "\n", + "# Call `update_file()` and pass in \"allow_list.txt\" and a list of IP addresses to be removed\n", + "\n", + "update_file(\"allow_list.txt\", [\"192.168.25.60\", \"192.168.140.81\", \"192.168.203.198\"])\n", + "\n", + "# Build `with` statement to read in the updated file\n", + "\n", + "with open(\"allow_list.txt\", \"r\") as file:\n", + "\n", + " # Read in the updated file and store the contents in `text`\n", + "\n", + " text = file.read()\n", + "\n", + "# Display the contents of `text`\n", + "\n", + "print(text)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UoVsVe37RkfT" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To call the `update_file()` function, write the name of the function, followed by parantheses, and pass in the file name and list of IP addresses that you want to try out the function on. Be sure to separate the two arguments with a comma (`,`). \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CFB8ClVxRkfU" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Inside the `with` statement, call the `.read()` method to read the contents of the file. Assign the `text` variable to the result. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pPpQTn_uRkfU" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To display the contents of the `text` variable, pass it as an argument to the `print()` function.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ru2ytGBLCh4q" + }, + "source": [ + "## Conclusion\n", + "\n", + "**What are your key takeaways from this lab?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ls2WEGqsiSAd" + }, + "source": [ + "I believe that this lab and allowing me to get hands on experince with Python allows me to understand text files and automating analysis. I believe that the `with` statement allows for efficient file handling, and the `open()` function lets you read or write files depending on the mode specified (\"r\" for reading, \"w\" for writing). I believe that the `.read()` and `.write()` methods allow you to access and modify file contents. I also believe that the `.split()` method is useful for converting strings into lists, making it easier to process data. I believe that using for loops and if statements allows you to iterate over lists and check for specific values to perform actions. I believe that Python can compare text file contents against list elements, and that algorithms can be organized into functions by specifying parameters and the actions they perform.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [], + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Activity_Create another algorithm.pdf b/Activity_Create another algorithm.pdf new file mode 100644 index 0000000..f37f3ed Binary files /dev/null and b/Activity_Create another algorithm.pdf differ diff --git a/Activity_Create more functions.ipynb b/Activity_Create more functions.ipynb new file mode 100644 index 0000000..5466c29 --- /dev/null +++ b/Activity_Create more functions.ipynb @@ -0,0 +1,894 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "Jaurs3usvWl_" + }, + "source": [ + "# Activity: Create more functions" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ivt8p77hCKcO" + }, + "source": [ + "## Introduction\n", + "\n", + "Built-in functions are functions that exist within Python and can be called directly. They help analysts efficiently complete tasks. Python also supports user-defined functions. These are functions that analysts write for their specific needs.\n", + "\n", + "For example, patterns in login attempts could reveal suspicious activity. Python functions can help analysts work efficiently with lists of login attempts. Both built-in functions and user-defined functions in Python can help security analysts analyze login attempts.\n", + "\n", + "In this lab, you'll use built-in functions to work with a list of failed login attempts per month to prepare it for further analysis, and you'll define a function that compares the user's login attempts for the current day to their average number of login attempts." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eW4pA4LsC-vb" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task.\n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook.\n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook.\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6ln7qtoePxRX" + }, + "source": [ + "## Scenario\n", + "In your work as a security analyst, you're responsible for working with a list that contains the number of failed attempts that occurred each month. You'll identify any patterns that might indicate malicious activity. You're also responsible for defining a function that compares the logins for the current day to an average and improving it by adding a `return` statement." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "caDM98GxK4zi" + }, + "source": [ + "## Task 1\n", + "In your work as an analyst, imagine that you're provided a list of the number of failed login attempts per month, as follows:\n", + "\n", + "`119`, `101`, `99`, `91`, `92`, `105`, `108`, `85`, `88`, `90`, `264`, and `223`.\n", + "\n", + "This list is organized in chronological order of months (January, February, March, April, May, June, July, August, September, October, November, and December).\n", + "\n", + "This list is stored in a variable named `failed_login_list`.\n", + "\n", + "In this task, use a built-in Python function to order the list. You'll pass the call to the function that sorts the list directly into the `print()` function. This will allow you to display and examine the result.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "PhN-Y8tT70jc" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[85, 88, 90, 91, 92, 99, 101, 105, 108, 119, 223, 264]\n" + ] + } + ], + "source": [ + "# Assign `failed_login_list` to the list of the number of failed login attempts per month\n", + "\n", + "failed_login_list = [119, 101, 99, 91, 92, 105, 108, 85, 88, 90, 264, 223]\n", + "\n", + "# Sort `failed_login_list` in ascending numerical order and display the result\n", + "\n", + "print(sorted(failed_login_list))\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DFfkNDCj79b5" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To order the `failed_login_list` in ascending numerical order, use the `sorted()` function.\n", + "\n", + "This is a built-in Python function that takes in a list, sorts its components, and returns the result.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QUAQjcMx79b5" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To order the `failed_login_list` in ascending numerical order, call the `sorted()` function and pass in `failed_login_list`.\n", + "\n", + "To display the result, make sure to place the call to `sorted()` inside the `print()` statement.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J6h1TIVVHL8C" + }, + "source": [ + "#### **Question 1**\n", + "**What do you observe from the output above? Do you notice any outlying numbers that indicate an increase in the failed number of login attempts?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ei_TcYYuHL8C" + }, + "source": [ + "By using the python's libaray built-in functions sorted() it sorts out the failed_login_list in order from least to greatest from left to right or in ascending numerical order It is shows that the outputted list starts with the lowest number of failed login attempts and ends with the highest number of failed login attempts and then the last two numbers in the sorted list (223 and 264) are outlying numbers where there is an increase in the number of failed login attempts.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "n4O1JyVfQ9n3" + }, + "source": [ + "## Task 2\n", + "Now, you'll want to isolate the highest number of failed login attempts so you can later investigate information about the month when that highest value occurred.\n", + "\n", + "You'll use the function that returns the largest numeric element from a list. Then, you'll pass this function into the `print()` function to display the result. This will allow you to determine which month to investigate further.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "id": "HnjJtWEc_pmi" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "264\n" + ] + } + ], + "source": [ + "# Assign `failed_login_list` to the list of the number of failed login attempts per month\n", + "\n", + "failed_login_list = [119, 101, 99, 91, 92, 105, 108, 85, 88, 90, 264, 223]\n", + "\n", + "# Determine the highest number of failed login attempts from `failed_login_list` and display the result \n", + "\n", + "print(max(failed_login_list))\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "twOlOfav_2t2" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To determine the highest number of failed login attempts from `failed_login_list`, use the `max()` function.\n", + "\n", + "This is a built-in Python function that takes in a sequence, identifies the maximum value from the sequence and returns the result.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bRhpKVqq_2t2" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To determine the highest number of failed login attempts from `failed_login_list`, call the `max()` function and pass in `failed_login_list`.\n", + "\n", + "To display the result, make sure to place the call to `max()` inside the `print()` statement.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HrJJVxYdHvvn" + }, + "source": [ + "#### **Question 2**\n", + "**What do you observe from the output above?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "35Pfa9xKHvvo" + }, + "source": [ + "After adding in the max() python built-in function from the libaray, the output above shows that the function call to max() isolated the highest number from the failed_login_list where it prints out the highest number of failed login attempts was 264." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "992Wo-yuh66n" + }, + "source": [ + "## Task 3\n", + "\n", + "In your work as an analyst, you'll first define a function that displays a message about how many login attempts a user has made that day.\n", + "\n", + "In this task, define a function named `analyze_logins()` that takes in two parameters, `username` and `current_day_logins`. Every time this function is called, it should display a message about the number of login attempts the user has made that day.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell. Note that the code cell will contain only a function definition, so running it will not produce an output." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "8w3Y2UwgvE-O" + }, + "outputs": [], + "source": [ + "# Define a function named `analyze_logins()` that takes in two parameters, `username` and `current_day_logins`\n", + "\n", + "def analyze_logins(username, current_day_logins):\n", + "\n", + " # Display a message about how many login attempts the user has made that day\n", + "\n", + " print(\"Current day login total for\", username, \"is\", current_day_logins)\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "v9mAtdMHrkiS" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To write a function header in Python, start with the `def` keyword, followed by the function name and then parantheses.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pqCxDr5fx4Kc" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "In Python, to define a function that takes in parameters, place the names of the parameters inside of the parantheses at the function header, and use a `,` between each parameter and the next.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yKQMmCS9vVVe" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To define a function named `analyze_logins()` that takes in two parameters, `username` and `current_day_logins`, start with the `def` keyword, followed by `analyze_logins()`, and write `username, current_day_logins` inside the parantheses. Be sure to write this code before the `:`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZMOnS7eFH9xs" + }, + "source": [ + "## Task 4\n", + "Now that you've defined the `analyze_logins()` function, call it to test out how it behaves.\n", + "\n", + "Call `analyze_logins()` with the arguments `\"ejones\"` and `9`.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "id": "qQosoYICv5yW" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current day login total for ejones is 9\n" + ] + } + ], + "source": [ + "# Define a function named `analyze_logins()` that takes in two parameters, `username` and `current_day_logins`\n", + "\n", + "def analyze_logins(username, current_day_logins):\n", + "\n", + " # Display a message about how many login attempts the user has made that day\n", + "\n", + " print(\"Current day login total for\", username, \"is\", current_day_logins)\n", + "\n", + "# Call `analyze_logins()` \n", + "\n", + "analyze_logins(\"ejones\", 9)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sTmd0KDFwI1d" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To call the `analyze_logins()` function after it's defined, write `analyze_logins()`. Then make sure to place the arguments `\"ejones\"` and `9` inside the parantheses.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3JwIItPYRmY6" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "The function call should be written as `analyze_logins(\"ejones\", 9)`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_9-FuRRGwwVk" + }, + "source": [ + "#### **Question 3**\n", + "**What does this function display? Would the output vary for different users?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gRSVO_XLwwVk" + }, + "source": [ + "After adding this to the end: analyze_logins(\"ejones\", 9), the outputs does \"Current day login total for ejones is 9\" when the arguments are \"ejones\" and 9. As discussed in the notes preivously, the function is defined to the user's specific inputed arguments, which means a differnt output can change if the arugment is changed by a differnt user." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6SAyyYgHIFuI" + }, + "source": [ + "## Task 5\n", + "\n", + "Now, you'll need to expand this function so that it also provides the average number of login attempts made by the user on that day. Doing this will require incorporating a third parameter into the function definition.\n", + "\n", + "In this task, add a parameter called `average_day_logins`. The code will use this parameter to display an additional message. The additional message will convey the average login attemps made by the user on that day. Then, call the function with the same first and second arguments as used in Task 4 and a third argument of `3`.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "fHlDFR0qxnc_" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current day login total for ejones is 9\n", + "Average logins per day for ejones is 3\n" + ] + } + ], + "source": [ + "# Define a function named `analyze_logins()` that takes in three parameters, `username`, `current_day_logins`, and `average_day_logins`\n", + "\n", + "def analyze_logins(username, current_day_logins, average_day_logins):\n", + "\n", + " # Display a message about how many login attempts the user has made that day\n", + "\n", + " print(\"Current day login total for\", username, \"is\", current_day_logins)\n", + " \n", + " # Display a message about average number of login attempts the user has made that day \n", + " \n", + " print(\"Average logins per day for\", username, \"is\", average_day_logins)\n", + "\n", + "# Call `analyze_logins()` \n", + "\n", + "analyze_logins(\"ejones\", 9, 3)\n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "k_EkErRCRmY7" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "In Python, to define a function that takes in parameters, place the names of the parameter inside the parantheses at the function header, with a `,` between each parameter and the next.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "X0_wryFfx4Kc" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "You need to define a function named `analyze_logins()` that takes in three parameters, `username`, `current_day_logins`, and `average_day_logins`. So you'll need to write `username, current_day_logins, average_day_logins` inside the parantheses.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AOex5Lz5RmY7" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To call the `analyze_logins()` function after it's defined, write `analyze_logins()`. Then make sure to place the arguments `\"ejones\"`, `9`, and `3` inside the parantheses.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_rt689J7INDU" + }, + "source": [ + "## Task 6\n", + "In this task, you'll further expand the function. Include a calculation to get the ratio of the logins made on the current day to the logins made on an average day. Store this in a new variable named `login_ratio`. The function displays an additional message that uses this variable.\n", + "\n", + "\n", + "Note that if `average_day_logins` is equal to `0`, then dividing `current_day_logins` by `average_day_logins` will cause an error. Due to the error, Python will display the following message: `ZeroDivisionError: division by zero`. For this activity, assume that all users will have logged in at least once before. This means that their `average_day_logins` will be greater than `0`, and the function will not involve dividing by zero.\n", + "\n", + "After defining the function, call the function with the same arguments that you used in the previous task.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "id": "IamLonisy8-g" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current day login total for ejones is 9\n", + "Average logins per day for ejones is 3\n", + "ejones logged in 3.0 times as much as they do on an average day.\n" + ] + } + ], + "source": [ + "# Define a function named `analyze_logins()` that takes in three parameters, `username`, `current_day_logins`, and `average_day_logins`\n", + "\n", + "def analyze_logins(username, current_day_logins, average_day_logins):\n", + "\n", + " # Display a message about how many login attempts the user has made that day\n", + "\n", + " print(\"Current day login total for\", username, \"is\", current_day_logins)\n", + " \n", + " # Display a message about average number of login attempts the user has made that day \n", + " \n", + " print(\"Average logins per day for\", username, \"is\", average_day_logins)\n", + " \n", + " # Calculate the ratio of the logins made on the current day to the logins made on an average day, storing in a variable named `login_ratio`\n", + " \n", + " login_ratio = current_day_logins / average_day_logins\n", + " \n", + " # Display a message about the ratio\n", + " \n", + " print(username, \"logged in\", login_ratio, \"times as much as they do on an average day.\")\n", + "\n", + "# Call `analyze_logins()` \n", + "\n", + "analyze_logins(\"ejones\", 9, 3)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2Odj_yMizJ06" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To calculate the ratio of the logins made on the current day to the logins made on an average day, divide `current_day_logins` by `average_day_logins`.\n", + "\n", + "Assign a variable named `login_ratio` to the result of this calculation, using the `=` assignment operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "i9qEkmM0RmY8" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To assign a variable named `login_ratio` to the result of the calculation, use the `=` assignment operator. Write `login_ratio` to the left of `=`, and place the calculation to the right of `=`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bDFPMCD9RmY8" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Call the updated `analyze_logins()` function and pass in `\"ejones\"`, `9`, and `3` as the three arguments, in that order.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QEKtFhvrzFxW" + }, + "source": [ + "#### **Question 4**\n", + "**What does this version of the `analyze_logins()` function display? Would the output vary for different users?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "G0Ffx-hZzFxW" + }, + "source": [ + "After adding: login_ratio = current_day_logins / average_day_logins -- and then calling the function: analyze_logins(\"ejones\", 9, 3), this version of the analyze_logins() function outputs/prints out a user's current day login total, average logins per day, and ratio of current day login total to average logins per day. Specifically, it shows a unique output for each distinct username that it takes in due to the code changes...." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "X9v_Uy4BjIGC" + }, + "source": [ + "## Task 7\n", + "\n", + "You'll continue working with the `analyze_logins()` function and add a return statement to it. Return statements allow you to send information back to the function call.\n", + "\n", + "In this task, use the `return` keyword to output the `login_ratio` from the function, so that it can be used later in your work.\n", + "\n", + "You'll call the function with the same arguments used in the previous task and store the output from the function call in a variable named `login_analysis`. You'll then use a `print()` statement to display the saved information.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "RsmPj8kf0IuW" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current day login total for ejones is 9\n", + "Average logins per day for ejones is 3\n", + "ejones logged in 3.0 times as much as they do on an average day.\n" + ] + } + ], + "source": [ + "# Define a function named `analyze_logins()` that takes in three parameters, `username`, `current_day_logins`, and `average_day_logins`\n", + "\n", + "def analyze_logins(username, current_day_logins, average_day_logins):\n", + "\n", + " # Display a message about how many login attempts the user has made that day\n", + "\n", + " print(\"Current day login total for\", username, \"is\", current_day_logins)\n", + " \n", + " # Display a message about average number of login attempts the user has made that day \n", + " \n", + " print(\"Average logins per day for\", username, \"is\", average_day_logins)\n", + " \n", + " # Calculate the ratio of the logins made on the current day to the logins made on an average day, storing in a variable named `login_ratio`\n", + " \n", + " login_ratio = current_day_logins / average_day_logins\n", + " \n", + " # Return the ratio\n", + " \n", + " return login_ratio\n", + "\n", + "# Call `analyze_logins() and store the output in a variable named `login_analysis` \n", + "\n", + "login_analysis = analyze_logins(\"ejones\", 9, 3)\n", + " \n", + "# Display a message about the `login_analysis`\n", + "\n", + "print(\"ejones\", \"logged in\", login_analysis, \"times as much as they do on an average day.\")\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5Lt3Jmn-0Um2" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "When defining the `analyze_logins()` function this time, place the `return` keyword in front of the output that you want the function to return.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sMOKq4Aq0Um2" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "When defining the `analyze_logins()` function this time, write `return` in front of `login_ratio`. (Do not place parentheses after the `return` keyword. It is not a function.)\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Kya_XRMjGOZ7" + }, + "source": [ + "#### **Question 5**\n", + "**How does this version of the `analyze_logins()` function compare to the previous versions?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6LkpTqj-Gn3s" + }, + "source": [ + "After adding the \" return login_ratio\" code to the function, makes this version of the `analyze_logins()` function have a `return` statement instead of a `print()` statement, which allows me to store the output from the function call in a variable for later usage. Once it's there, it will return the value that it needs to be calculated. Returns are useful for returning data instead of writing it all out on the print statement..." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r77Cyb-JItHF" + }, + "source": [ + "## Task 8\n", + "\n", + "In this task, you'll use the value of `login_analysis` in a conditional statement. When the value of `login_analysis` is greater than or equal to `3`, then the login activity will require further investigation, and an alert will be displayed. Incorporate this condition to complete the conditional statement in the code. \n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "id": "fm4WfEQk12gA" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current day login total for ejones is 9\n", + "Average logins per day for ejones is 3\n", + "Alert! This account has more login activity than normal.\n" + ] + } + ], + "source": [ + "# Define a function named `analyze_logins()` that takes in three parameters, `username`, `current_day_logins`, and `average_day_logins`\n", + "\n", + "def analyze_logins(username, current_day_logins, average_day_logins):\n", + "\n", + " # Display a message about how many login attempts the user has made that day\n", + "\n", + " print(\"Current day login total for\", username, \"is\", current_day_logins)\n", + " \n", + " # Display a message about average number of login attempts the user has made that day \n", + " \n", + " print(\"Average logins per day for\", username, \"is\", average_day_logins)\n", + " \n", + " # Calculate the ratio of the logins made on the current day to the logins made on an average day, storing in a variable named `login_ratio`\n", + " \n", + " login_ratio = current_day_logins / average_day_logins\n", + " \n", + " # Return the ratio\n", + " \n", + " return login_ratio\n", + "\n", + "# Call `analyze_logins() and store the output in a variable named `login_analysis` \n", + "\n", + "login_analysis = analyze_logins(\"ejones\", 9, 3)\n", + "\n", + "# Conditional statement that displays an alert about the login activity if it's more than normal\n", + "\n", + "if login_analysis >= 3:\n", + " print(\"Alert! This account has more login activity than normal.\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WwiN3XI91-Y3" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To calculate the ratio of the logins made on the current day to the logins made on an average day, divide `current_day_logins` by `average_day_logins`.\n", + "\n", + "Assign a variable named `login_ratio` to the result of this calculation, using the `=` assignment operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7VHMG61A2Hig" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To assign a variable named `login_ratio` to the result of the calculation, use the `=` assignment operator. Write `login_ratio` to the left of `=`, and place the calculation to the right of `=`. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PZGf18SVnpLm" + }, + "source": [ + "
\n", + "

Hint 3

\n", + " \n", + "Call the updated `analyze_logins()` function and pass in `\"ejones\"`, `9`, and `3` as the three arguments, in that order.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ru2ytGBLCh4q" + }, + "source": [ + "## Conclusion\n", + "\n", + "**What are your key takeaways from this lab?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ls2WEGqsiSAd" + }, + "source": [ + "In this lab, I learned that there are various ways to write functions in Python becuase Functions can be designed to either display information directly to the screen or return information that can be stored in a variable for further use. They can also be written to accept any number of parameters, use these parameters to perform a series of tasks, and then return a result based on those operations. For example, the `sorted()` function is a built-in Python function that helps you sort the elements of a list in a specific order, such as ascending numerical order. When you call `sorted()` with a list of numbers, it returns the sorted list. Similarly, the `max()` function is another built-in function that allows you to identify the maximum value in a list of numbers. By calling `max()` on a list, it will return the largest number. Additionally, the `print()` function is used to display information to the screen. It can also be employed to directly display the output of another function by placing that function call inside a `print()` statement. These built-in functions, can help me automating tasks instead of copy and pasting it multiple times." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Activity_Create more functions.pdf b/Activity_Create more functions.pdf new file mode 100644 index 0000000..2b01ef1 Binary files /dev/null and b/Activity_Create more functions.pdf differ diff --git a/Activity_Define and call a function.ipynb b/Activity_Define and call a function.ipynb new file mode 100644 index 0000000..ced3d39 --- /dev/null +++ b/Activity_Define and call a function.ipynb @@ -0,0 +1,665 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "ISr45HAPh1Xq" + }, + "source": [ + "# Activity: Define and call a function" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kRU7MObZEY36" + }, + "source": [ + "## Introduction\n", + "\n", + "As a security analyst, when you're writing out Python code to automate a certain task, you'll often find yourself needing to reuse the same block of code more than once. This is why functions are important. You can call that function whenever you need the computer to execute those steps. Python not only has built-in functions that have already been defined, but also provides the tools for users to define their own functions. Security analysts often define and call functions in Python to automate series of tasks. \n", + "\n", + "In this lab, you'll practice defining and calling functions in Python." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bM8zStFIWIkI" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task. \n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook. \n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook. \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "A8q3M1L7isIj" + }, + "source": [ + "## Scenario\n", + "Writing functions in Python is a useful skill in your work as a security analyst. In this lab, you'll define and a call a function that displays an alert about a potential security issue. Also, you'll work with a list of employee usernames, creating a function that converts the list into one string." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7kVc1buv_XBG" + }, + "source": [ + "## Task 1\n", + "\n", + "The following code cell contains a user-defined function named `alert()`. \n", + "\n", + "For this task, analyze the function definition, and make note of your observations. \n", + "\n", + "You won't need to run the cell in order to answer the question that follows. But if you do run the cell, note that it will not produce an output because the function is just being defined here. " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "MIWcP2bq_R-V" + }, + "outputs": [], + "source": [ + "# Define a function named `alert()` \n", + "\n", + "def alert():\n", + " print(\"Potential security issue. Investigate further.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "N4iCk5h-BEW4" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Take a look at the function above. ` def alert():` defines a function named alert. This means you're creating a reusable block of code that can be called later in your program to execute its instructions. The body of the function is `print(\"Potential security issue. Investigate further.\")` It contains the code that will be executed when the `alert()` function is called. In this case, the print statement displays the message \"Potential security issue. Investigate further.\" to the consol\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lf8YCFRMDL38" + }, + "source": [ + "#### **Question 1**\n", + "Summarize what the user-defined function above does in your own words. Think about what the output would be if this function were called." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Rq45fNzNsKmc" + }, + "source": [ + "Based on the notes and the lab example, the alert() prints the string \"Potential security issue. Investigate further.\" This is used to notify security issues in a system like if this function were called, the output would show Potential security issue. Also note that when a string is displayed, the quotes around the string do not appear in the output....\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1MK0FV0nDk7n" + }, + "source": [ + "## Task 2\n", + "\n", + "For this task, call the `alert()` function that was defined earlier and analyze the output. \n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before running the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "id": "qeLg2iXzME1e" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Potential security issue. Investigate further.\n" + ] + } + ], + "source": [ + "# Define a function named `alert()` \n", + "\n", + "def alert():\n", + " print(\"Potential security issue. Investigate further.\")\n", + "\n", + "# Call the `alert()` function\n", + "\n", + "alert()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nLaEnEsaa0Nf" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To call the function, write `alert()` after the function definition. Note that the function can be called only after it's defined. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E9wvcRm1ayLf" + }, + "source": [ + "#### **Question 2**\n", + "What are the advantages of placing this code in a function rather than running it directly?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Eq_sqIGDayLf" + }, + "source": [ + "Based on my past experinces and notes, if we place the code in a function, it helps me reuse the code by calling the alert() function, if I need it to print messages about a potential security issue and further investigation.... The second option would be to write out that print() statement every time, which would be tedious." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1VddrUV7fo-C" + }, + "source": [ + "## Task 3\n", + "\n", + "Functions can include other components that you've already worked with. The following code cell contains a variation of the `alert()` function that now uses a `for` loop to display the alert message multiple times.\n", + "\n", + "For this task, call the new `alert()` function and observe the output. \n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before running the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "Wi3KRQV7jdcJ" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Potential security issue. Investigate further.\n", + "Potential security issue. Investigate further.\n", + "Potential security issue. Investigate further.\n" + ] + } + ], + "source": [ + "# Define a function named `alert()`\n", + "\n", + "def alert(): \n", + " for i in range(3):\n", + " print(\"Potential security issue. Investigate further.\")\n", + "\n", + "# Call the `alert()` function\n", + "alert()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CdN6iNlvmMsb" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Task 3 is similar to Task 2 here. The difference is that a `for` loop is used to display the alert message multiple times.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zk8dwl4bmczd" + }, + "source": [ + "#### **Question 3**\n", + "How does the output above compare to the output from calling the previous version of the `alert()` function? How are the two definitions of the function different?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0VLFZyKAmcze" + }, + "source": [ + "In the past one, it does not have the for loop to repeat the print statement. This one, the output shows \"Potential security issue. Investigate further.\" three times due to the for loop where the messages appears eveyr new line. This loop iterates over a range of numbers (specified by range(3)) and executes a print() statement in each iteration. The only thing was similar was the print mehtod, not the for loop." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VsKDFJEcfMHV" + }, + "source": [ + "## Task 4\n", + "\n", + "In the next part of your work, you're going to work with a list of approved usernames, representing users who can enter a system. You'll be developing a function that helps you convert the list of approved usernames into one big string. Structuring this data differently enables you to work with it in different ways. For example, structuring the usernames as a list allows you to easily add or remove a username from it. In contrast, structuring it as a string allows you to easily place its contents into a text file. \n", + "\n", + "For this task, start defining a function named `list_to_string()`. Write the function header. \n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code. Note that running this cell will produce an error since this cell will just contain the function header; you'll write the function body and complete the function definition in a later task." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "id": "RO3qCLX3R07T" + }, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "unexpected EOF while parsing (, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m def list_to_string():\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unexpected EOF while parsing\n" + ] + } + ], + "source": [ + "# Define a function named `list_to_string()`\n", + "\n", + "def list_to_string():" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ccGeXp9er_IU" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To write the function header, start with the `def` keyword, followed by the name of the function, parentheses, and a colon. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yAwUDAHpsXS4" + }, + "source": [ + "## Task 5\n", + "\n", + "Now you'll begin to develop the body of the `list_to_string()` function. \n", + "\n", + "In the following code cell, you're provided a list of approved usernames, stored in a variable named `username_list`. Your task is to complete the body of the `list_to_string()` function. Recall that the body of a function must be indented. To complete the function body, write a loop that iterates through the elements of the `username_list` and displays each element. Then, call the function and run the cell to observe what happens. \n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "vdUZ7OUdumWu" + }, + "outputs": [], + "source": [ + "# Define a function named `list_to_string()`\n", + "\n", + "def list_to_string():\n", + "\n", + " # Store the list of approved usernames in a variable named `username_list`\n", + "\n", + " username_list = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\", \"gesparza\", \"alevitsk\", \"wjaffrey\"]\n", + "\n", + " # Write a for loop that iterates through the elements of `username_list` and displays each element\n", + "\n", + " for i in username_list:\n", + " print(i)\n", + "\n", + "# Call the `list_to_string()` function\n", + "\n", + "list_to_string()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ce6YR4gcti83" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `for` loop in the body of the `list_to_string()` function must iterate through the elements of `username_list`. So, use the `username_list` variable to complete the `for` loop condition.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sLxLO_Ymv03w" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "In each iteration of the `for` loop, an element of `username_list` should be displayed. The loop variable `i` represents each element of `username_list`. To complete the `print()` statement inside the `for` loop, pass`i` to the `print()` function call.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QVPLYl6mwu9c" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To call the function, write `list_to_string()` after the function definition. Recall that the function can be called only after it's defined.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OrhGVQToxE7f" + }, + "source": [ + "#### **Question 4**\n", + "What do you observe from the output above?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "w41CcRbJxE7h" + }, + "source": [ + "Since it uses a for loop and printing i parts, it will print each element in the list. for i in username_list means you are accessing the list and then if you call i it means you want it to print out each element in the list.\n", + "This will print out all the names in the list." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VoUS63vCxX6a" + }, + "source": [ + "## Task 6\n", + "\n", + "String concatenation is a powerful concept in coding. It allows you to combine multiple strings together to form one large string, using the addition operator (`+`). Sometimes analysts need to merge individual pieces of data into a single string value. In this task, you'll use string concatenation to modify how the `list_to_string()` function is defined.\n", + "\n", + "In the following code cell, you're provided a variable named `sum_variable` that initially contains an empty string. Your task is to use string concatenation to combine the usernames from the `username_list` and store the result in `sum_variable`.\n", + "\n", + "In each iteration of the `for` loop, add the current element of `username_list` to `sum_variable`. At the end of the function definition, write a `print()` statement to display the value of `sum_variable` at that stage of the process. Then, run the cell to call the `list_to_string()` function and examine its output.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "MulvjIZH2Fyf" + }, + "outputs": [], + "source": [ + "# Define a function named `list_to_string()`\n", + "\n", + "def list_to_string():\n", + "\n", + " # Store the list of approved usernames in a variable named `username_list`\n", + "\n", + " username_list = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\", \"gesparza\", \"alevitsk\", \"wjaffrey\"]\n", + "\n", + " # Assign `sum_variable` to an empty string\n", + "\n", + " sum_variable = \"\"\n", + "\n", + " # Write a for loop that iterates through the elements of `username_list` and displays each element\n", + "\n", + " for i in username_list:\n", + " sum_variable = sum_variable + i\n", + "\n", + " # Display the value of `sum_variable`\n", + "\n", + " print(sum_variable)\n", + "\n", + "# Call the `list_to_string()` function\n", + "\n", + "list_to_string()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "G-IUddHy3LV2" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Inside the `for` loop, complete the line that updates the `sum_variable` in each iteration. The loop variable `i` represents each element of `username_list`. Since you need to add the current element to the current value of `sum_variable`, place `i` after the addition operator `(+)`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "S9VgoPcr3LV-" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `print()` function to display the value of `sum_variable`. Make sure to pass in `sum_variable` to the call to `print()`. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yWfkqnTV5b-C" + }, + "source": [ + "#### **Question 5**\n", + "What do you observe from the output above?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "84duQyQ45b-L" + }, + "source": [ + "When i is placed into the sum_variable and added, it means it is concated which makes the output shows all the elements from username_list merged together in one line, but the output is difficult to read since I have a hard time knowing where the username ends and the next begins. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pcRf8PLa4bt1" + }, + "source": [ + "## Task 7\n", + "\n", + "In this final task, you'll modify the code you wrote previously to improve the readability of the output. \n", + "\n", + "This time, in the definition of the `list_to_string()` function, add a comma and a space (`\", \"`) after each username. This will prevent all the usernames from running into each other in the output. Adding a comma helps clearly separate one username from the next in the output. Adding a space following the comma as an additional separator between one username and the next makes it easier to read the output. Then, call the function and run the cell to observe the output.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "lQy1eAg87v-3" + }, + "outputs": [], + "source": [ + "# Define a function named `list_to_string()`\n", + "\n", + "def list_to_string():\n", + "\n", + " # Store the list of approved usernames in a variable named `username_list`\n", + "\n", + " username_list = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\", \"gesparza\", \"alevitsk\", \"wjaffrey\"]\n", + "\n", + " # Assign `sum_variable` to an empty string\n", + "\n", + " sum_variable = \"\"\n", + "\n", + " # Write a for loop that iterates through the elements of `username_list` and displays each element\n", + "\n", + " for i in username_list:\n", + " sum_variable = sum_variable + i + \", \"\n", + "\n", + " # Display the value of `sum_variable`\n", + "\n", + " print(sum_variable)\n", + "\n", + "# Call the `list_to_string()` function\n", + "\n", + "list_to_string()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wl8UVVZ39H-d" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Inside the `for` loop, complete the line that updates the `sum_variable` in each iteration. The loop variable `i` represents each element of `username_list`. After the current element is added to the current value of `sum_variable`, add a string that contains a comma followed by a space.\n", + "\n", + "To complete this step, place `\", \"` after the last addition operator (`+`). \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BPn4fuRl9H-m" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To call the function, write `list_to_string()` after the function definition. Note that the function can be called only after it's defined.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MCsLDLob_liw" + }, + "source": [ + "#### **Question 6**\n", + "What do you notice about the output from the function call this time?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NpZ47U3q_liw" + }, + "source": [ + "Based on this example, once the \",\" is added/concantated to to sum_variable, and then the function list_to_string() is called, the output shows all the elements from username_list in one line, but with a comma and a space after each username. I found this useful as preivously it was not easy to read it all mushed up together. And now, this format is much easier to read to know which one is the username from the next. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ru2ytGBLCh4q" + }, + "source": [ + "## Conclusion\n", + "\n", + "**What are your key takeaways from this lab?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2mzUW_1SH0E0" + }, + "source": [ + "In this lab, I learned how to define and use functions in Python using the `def` keyword. Functions allow me to organize code into reusable blocks. For example, I created a function `alert()` that prints a message when called. To call a function, I simply use its name followed by parentheses, like this: `alert()`. I also worked with string concatenation, where I used the `+` operator to combine strings. For instance, I concatenated usernames from a list into one large string by adding each username to a variable inside a loop. I used a `for` loop to iterate over the list of usernames, which helped me process each item one by one. The benefit of using a `for` loop in this case is that it automates repetitive tasks, saving time and reducing errors. Overall, using functions, string concatenation, and loops made my code more efficient, organized, and easy to maintain." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Activity_Define and call a function.pdf b/Activity_Define and call a function.pdf new file mode 100644 index 0000000..3b1c652 Binary files /dev/null and b/Activity_Define and call a function.pdf differ diff --git a/Activity_Develop an algorithm.ipynb b/Activity_Develop an algorithm.ipynb new file mode 100644 index 0000000..2ffddbe --- /dev/null +++ b/Activity_Develop an algorithm.ipynb @@ -0,0 +1,1062 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "FpdoHsRVBxVN" + }, + "source": [ + "# Activity: Develop an algorithm" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KxK7oitJd3wz" + }, + "source": [ + "## Introduction\n", + "\n", + "An algorithm is a set of steps that can be used to solve a problem. Security analysts develop algorithms to provide the solutions that they need for their work. For example, an analyst may work with users who bring them devices. The analyst may need an algorithm that first checks if a user is approved to access the system and then checks if the device that they have brought is the one assigned to them.\n", + "\n", + "In this lab, you'll develop an algorithm in Python that automates this process." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eW4pA4LsC-vb" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task.\n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook.\n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook.\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-btSB292gVHU" + }, + "source": [ + "## Scenario\n", + "\n", + "In this lab, you're working as a security analyst and you're responsible for developing an algorithm that connects users to their assigned devices. You'll write code that indicates if a user is approved on the system and has brought their assigned device to the security team.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-KY1VeP9CfM-" + }, + "source": [ + "## Task 1\n", + "You'll work with a list of approved usernames along with a list of the approved devices assigned to these users. The elements of the two lists are synchronized. In other words, the user at index `0` in `approved_users` uses the device at index `0` in `approved_devices`. Later, this will allow you to verify if the username and device ID entered by a user correspond to each other.\n", + "\n", + "First, to explore how indices in lists work, run the following code cell as is and observe the output. Then, replace each `0` with another index and run the cell to observe what happens." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "id": "Cwvk-6Wn_DWo" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "elarson\n", + "8rp2k75\n" + ] + } + ], + "source": [ + "# Assign `approved_users` to a list of approved usernames\n", + "\n", + "approved_users = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\"]\n", + "\n", + "# Assign `approved_devices` to a list of device IDs that correspond to the usernames in `approved_users`\n", + "\n", + "approved_devices = [\"8rp2k75\", \"hl0s5o1\", \"2ye3lzg\", \"4n482ts\", \"a307vir\"]\n", + "\n", + "# Display the element at the specified index in `approved_users`\n", + "\n", + "print(approved_users[0])\n", + "\n", + "# Display the element at the specified index in `approved_devices`\n", + "\n", + "print(approved_devices[0])\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wrx99R-XL8ZH" + }, + "source": [ + "#### **Question 1**\n", + "**What did you observe about the output when `approved_users[0]` is displayed and when `approved_devices[0]` is displayed? What happens when you replace each `0` with another index?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L0rNcJs4-q-Q" + }, + "source": [ + "After running the code, I believe using indexes helps us easily access specific items stored in lists. When we use `approved_users[0]`, it shows the first username in the `approved_users` list, and `approved_devices[0]` shows the first device ID in the `approved_devices` list. By changing the index number (for example, from `0` to `2`), we can get different elements from each list. So, `approved_users[2]` and `approved_devices[2]` would display the third username and third device ID. I believe this makes it simple to match and view related data stored in lists." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OE_fiWN4OjWJ" + }, + "source": [ + "## Task 2\n", + "There's a new employee joining the organization, and they need to be provided with a username and device ID. In the following code cell, you are given a username and device ID of this new user, stored in the variables `new_user` and `new_device`, respectively. Use the `.append()` method to add these variables to the `approved_users` and `approved_devices` respectively. Afterwards, display the `approved_users` and `approved_devices` variables to confirm the added information. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "DqMW7dHBUffd" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['elarson', 'bmoreno', 'tshah', 'sgilmore', 'eraab', 'gesparza']\n", + "['8rp2k75', 'hl0s5o1', '2ye3lzg', '4n482ts', 'a307vir', '3rcv4w6']\n" + ] + } + ], + "source": [ + "# Assign `approved_users` to a list of approved usernames\n", + "\n", + "approved_users = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\"]\n", + "\n", + "# Assign `approved_devices` to a list of device IDs that correspond to the usernames in `approved_users`\n", + "\n", + "approved_devices = [\"8rp2k75\", \"hl0s5o1\", \"2ye3lzg\", \"4n482ts\", \"a307vir\"]\n", + "\n", + "# Assign `new_user` to the username of a new approved user\n", + "\n", + "new_user = \"gesparza\"\n", + "\n", + "# Assign `new_device` to the device ID of the new approved user\n", + "\n", + "new_device = \"3rcv4w6\"\n", + "\n", + "# Add that user's username and device ID to `approved_users` and `approved_devices` respectively\n", + "\n", + "approved_users.append(new_user)\n", + "approved_devices.append(new_device)\n", + "\n", + "# Display the contents of `approved_users`\n", + "\n", + "print(approved_users)\n", + "\n", + "# Diplay the contents of `approved_devices`\n", + "\n", + "print(approved_devices)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "N4iCk5h-BEW4" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `.append()` method to add `new_user` to `approved_users`.\n", + "\n", + "Use the `.append()` method to add `new_device` to `approved_devices`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9BwgLKQGAozi" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `print()` function to display the contents of `approved_users`.\n", + "\n", + "Use the `print()` function to display the contents of `approved_devices`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2UVf8du_CdfW" + }, + "source": [ + "#### **Question 2**\n", + "**After the new approved user is added, what did you observe about the output when `approved_users` is displayed and when `approved_devices` is displayed?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "APOgX6RbCdff" + }, + "source": [ + "After the new approved user is added, wI noticed after print is called for approved_users, their username is at the end of the approved_users and their device ID is at the end of the approved_devices." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nDgdKRQ5TzdV" + }, + "source": [ + "## Task 3\n", + "An employee has left the team and should no longer have access to the system. In the following code cell, you are given the username and device ID of the user to be removed, stored in the variables `removed_user` and `removed_device` respectively. Use the `.remove()` method to remove each of these elements from the corresponding list. Afterwards, display both the `approved_users` and the `approved_devices` variables to view the removed users. Run the code and observe the results. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "2oPgNiC0hT_T" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['elarson', 'bmoreno', 'sgilmore', 'eraab', 'gesparza']\n", + "['8rp2k75', 'hl0s5o1', '4n482ts', 'a307vir', '3rcv4w6']\n" + ] + } + ], + "source": [ + "# Assign `approved_users` to a list of approved usernames\n", + "\n", + "approved_users = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\", \"gesparza\"]\n", + "\n", + "# Assign `approved_devices` to a list of device IDs that correspond to the usernames in `approved_users`\n", + "\n", + "approved_devices = [\"8rp2k75\", \"hl0s5o1\", \"2ye3lzg\", \"4n482ts\", \"a307vir\", \"3rcv4w6\"]\n", + "\n", + "# Assign `removed_user` to the username of the employee who has left the team\n", + "\n", + "removed_user = \"tshah\"\n", + "\n", + "# Assign `removed_device` to the device ID of the employee who has left the team\n", + "\n", + "removed_device = \"2ye3lzg\"\n", + "\n", + "# Remove that employee's username and device ID from `approved_users` and `approved_devices` respectively\n", + "\n", + "approved_users.remove(removed_user)\n", + "approved_devices.remove(removed_device)\n", + "\n", + "# Display `approved_users`\n", + "\n", + "print(approved_users)\n", + "\n", + "# Diplay `approved_devices`\n", + "\n", + "print(approved_devices)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mDptIupjDzkZ" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `.remove()` method to remove `removed_user` from `approved_users`.\n", + "\n", + "Use the `.remove()` method to remove `removed_device` from `approved_devices`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cnTrQyxLpJh-" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `print()` function to display the contents of `approved_users`.\n", + "\n", + "Use the `print()` function to display the contents of `approved_devices`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0rePIFfpFgJL" + }, + "source": [ + "#### **Question 3**\n", + "**After the user who left the team is removed, what did you observe about the output when `approved_users` is displayed and when `approved_devices` is displayed?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rAeGIEexFgJU" + }, + "source": [ + "After the user who left the team is removed, I noticed that the username is no longer part of the approved_users and their device ID is no longer part of the approved_devices once the output is hsown or pirinted out...." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aLiBPEERhxdC" + }, + "source": [ + "## Task 4\n", + "\n", + "As part of verifying a user's identity in the system, you'll need to check if the user is one of the approved users. Write a conditional statement that verifies if a given username is an element of the list of approved usernames. If it is, display `\"The user ______ is approved to access the system.\"`. Otherwise, display `\"The user ______ is not approved to access the system.\"`. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "ybZt-6n0lWNV" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The username sgilmore is approved to access the system.\n" + ] + } + ], + "source": [ + "# Assign `approved_users` to a list of approved usernames\n", + "\n", + "approved_users = [\"elarson\", \"bmoreno\", \"sgilmore\", \"eraab\", \"gesparza\"]\n", + "\n", + "# Assign `approved_devices` to a list of device IDs that correspond to the usernames in `approved_users`\n", + "\n", + "approved_devices = [\"8rp2k75\", \"hl0s5o1\", \"4n482ts\", \"a307vir\", \"3rcv4w6\"]\n", + "\n", + "# Assign `username` to a username\n", + "\n", + "username = \"sgilmore\"\n", + "\n", + "# Conditional statement\n", + "# If `username` belongs to `approved_users`, then display \"The user ______ is approved to access the system.\"\n", + "# Otherwise display \"The user ______ is not approved to access the system.\"\n", + "if username in approved_users:\n", + " print(\"The username\", username, \"is approved to access the system.\")\n", + "else:\n", + " print(\"The username\", username, \"is not approved to access the system.\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cfY5l5xbGL9E" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "In the `if` condition, be sure to check if `username` belongs to `approved_users`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4AsCC1gRGiUT" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "After the `if` statement, use the `else` keyword to create an `else` statement that handles the case when `username` is not part of the `approved_users`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hQv4GCcWGv2o" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Inside the `else` statement, use the `print()` function to display the message `\"The user ______ is not approved to access the system.\"`.\n", + "\n", + "Refer to the `print()` function call in the `if` statement and observe how commas separate a string containing the first part of the message, the `username` variable, and another string containing the second part of the message.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Kvtoh6nLK4cG" + }, + "source": [ + "#### **Question 4**\n", + "**What message do you observe in the output when `username` is `\"sgilmore\"`?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xMiJxEh7K4cH" + }, + "source": [ + "When the output is username=\"sgilmore\", the message that print outputs is \"The username sgilmore is approved to access the system.\" This is becuase it i snoticable that \"sgilmore\" is an element of the approved_users." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "i-0xllOwdl8r" + }, + "source": [ + "## Task 5\n", + "The next part of the algorithm uses the `.index()` method to find the index of `username` in the `approved_users` and store that index in a variable named `ind`.\n", + "\n", + "When used on a list, the `.index()` method will return the position of the given value in the list.\n", + "\n", + "Add a statement to display `ind` in the following code cell to explore the value it contains. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "f8C1FaF0k2OG" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "# Assign `approved_users` to a list of approved usernames\n", + "\n", + "approved_users = [\"elarson\", \"bmoreno\", \"sgilmore\", \"eraab\", \"gesparza\"]\n", + "\n", + "# Assign `approved_devices` to a list of device IDs that correspond to the usernames in `approved_users`\n", + "\n", + "approved_devices = [\"8rp2k75\", \"hl0s5o1\", \"4n482ts\", \"a307vir\", \"3rcv4w6\"]\n", + "\n", + "# Assign `username` to a username\n", + "\n", + "username = \"sgilmore\"\n", + "\n", + "# Assign `ind` to the index of `username` in `approved_users`\n", + "\n", + "ind = approved_users.index(username)\n", + "\n", + "# Display the value of `ind`\n", + "\n", + "print(ind)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EqUySUGuKPIB" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `print()` function to display the value of `ind`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uvLBuBxVMKNU" + }, + "source": [ + "#### **Question 5**\n", + "**What do you observe from the output when `username` is `\"sgilmore\"`?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KxcST8CUMKNV" + }, + "source": [ + "I believe when the username is \"sgilmore\", the output 2 shows that this username is stored at index 2 in the approved_users list. This means \"sgilmore\" is actually the third item in the list, since Python starts counting from 0. I believe understanding this helps make it clear how Python lists organize and access data using index positions." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uWhHEIAkk4Gt" + }, + "source": [ + "## Task 6\n", + "This task will allow you to build your understanding of list operations for the algorithm that you'll eventually build. It will demonstrate how you can find an index in one list and then use this index to display connected information in another list. First, use the `.index()` method again to find the index of `username` in the `approved_users` and store that in a variable named `ind`. Then, connect `ind` to the `approved_devices` and display the device ID located at the index `ind`. Afterwards, run the cell to observe the result. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "p3OoQe894EM9" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4n482ts\n" + ] + } + ], + "source": [ + "# Assign `approved_users` to a list of approved usernames\n", + "\n", + "approved_users = [\"elarson\", \"bmoreno\", \"sgilmore\", \"eraab\", \"gesparza\"]\n", + "\n", + "# Assign `approved_devices` to a list of device IDs that correspond to the usernames in `approved_users`\n", + "\n", + "approved_devices = [\"8rp2k75\", \"hl0s5o1\", \"4n482ts\", \"a307vir\", \"3rcv4w6\"]\n", + "\n", + "# Assign `username` to a username\n", + "\n", + "username = \"sgilmore\"\n", + "\n", + "# Assign `ind` to the index of `username` in `approved_users`\n", + "\n", + "ind = approved_users.index(username)\n", + "\n", + "# Display the device ID at the index that matches the value of `ind` in `approved_devices`\n", + "\n", + "print(approved_devices[ind])\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OSp3RAghUbpU" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `.index()` method to get the index value of the `username` in the `approved_users`. Assign `ind` to the result.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5lUtc8DbVDFp" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To display the correct device ID from `approved_devices`, use `ind` as the index. Place `ind` inside the square brackets to extract the correct element from `approved_devices`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OMIniudTWfJl" + }, + "source": [ + "#### **Question 6**\n", + "**What do you observe from the output when `username` is `\"sgilmore\"`?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hSjQGei7WfJm" + }, + "source": [ + "So far with these code changes to thte variable and the [ind] at the print, I believe when the username is `\"sgilmore\"`, the output `\"4n482ts\"` shows the device ID linked to that user. Since `\"sgilmore\"` is the third username in the `approved_users` list, the matching third device ID in the `approved_devices` list is `\"4n482ts\"`. I believe this demonstrates how list positions help connect related information across multiple lists.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Z6p0xuJ34MUf" + }, + "source": [ + "## Task 7\n", + "Your next step in creating the algorithm is to determine if a username and device ID correspond. To do this, write a conditional that checks if the `username` is an element of the `approved_devices` and if the `device_id` stored at the same index as `username` matches the `device_id` entered. You'll use the logical operator `and` to connect the two conditions. When both conditions evaluate to `True`, display a message that the username is approved and another message that the user has their assigned device. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "6cPaGUInGhiE" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The username sgilmore is approved to access the system.\n", + "4n482ts is the assigned device for sgilmore\n" + ] + } + ], + "source": [ + "# Assign `approved_users` to a list of approved usernames\n", + "\n", + "approved_users = [\"elarson\", \"bmoreno\", \"sgilmore\", \"eraab\", \"gesparza\"]\n", + "\n", + "# Assign `approved_devices` to a list of device IDs that correspond to the usernames in `approved_users`\n", + "\n", + "approved_devices = [\"8rp2k75\", \"hl0s5o1\", \"4n482ts\", \"a307vir\", \"3rcv4w6\"]\n", + "\n", + "# Assign `username` to a username\n", + "\n", + "username = \"sgilmore\"\n", + "\n", + "# Assign `device_id` to a device ID\n", + "\n", + "device_id = \"4n482ts\"\n", + "\n", + "# Assign `ind` to the index of `username` in `approved_users`\n", + "\n", + "ind = approved_users.index(username)\n", + "\n", + "# Conditional statement\n", + "# If `username` belongs to `approved_users`, and if the device ID at `ind` in `approved_devices` matches `device_id`,\n", + "# then display a message that the username is approved,\n", + "# followed by a message that the user has the correct device\n", + "\n", + "if username in approved_users and device_id == approved_devices[ind]:\n", + " print(\"The username\", username, \"is approved to access the system.\")\n", + " print(device_id, \"is the assigned device for\", username)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "S0BfEOi8V1P_" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "After the logical operator `and`, write the second condition in the `if` statement using a comparison operator to check whether the element at `ind` in `approved_devices` matches `device_id`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UbkFDkoVDwqD" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `==` comparison operator to check whether the element at `ind` in `approved_devices` matches `device_id`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1Fl6R6-mXIUX" + }, + "source": [ + "#### **Question 7**\n", + "**What do you observe from the output when `username` is `\"sgilmore\"` and `device_id` is `\"4n482ts\"`?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "X0fwkZEuXIUf" + }, + "source": [ + "After adding a conditional and and a compariison between device id is true == approved_devices[ind], I believe when the username is `\"sgilmore\"` and the device ID is `\"4n482ts\"`, the output clearly shows their connection. The first line, The username sgilmore is approved to access the system.” confirms that the user has access. The second line, 4n482ts is the assigned device for sgilmore.”, shows which device belongs to that user. I believe this helps make the relationship between approved users and their assigned devices easy to understand.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9lFPzCnyHAPg" + }, + "source": [ + "## Task 8\n", + "It would also be helpful for users to receive messages when their username is not approved or their device ID is incorrect.\n", + "\n", + "Add to the code by writing an `elif` statement. This `elif` statement should run when the `username` is part of the `approved_users` but the `device_id` doesn't match the corresponding device ID in the `approved_devices`. The statement should also display two messages conveying that information.\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell.\n", + "\n", + "(After you run the code once with a `device_id` of `\"4n482ts\"`, you might want to explore what happens if you assign a different value to `device_id`.)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "id": "DlvfIxLIFI9H" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The user sgilmore is approved to access the system.\n", + "4n482ts is the assigned device for sgilmore\n" + ] + } + ], + "source": [ + "# Assign `approved_users` to a list of approved usernames\n", + "\n", + "approved_users = [\"elarson\", \"bmoreno\", \"sgilmore\", \"eraab\", \"gesparza\"]\n", + "\n", + "# Assign `approved_devices` to a list of device IDs that correspond to the usernames in `approved_users`\n", + "\n", + "approved_devices = [\"8rp2k75\", \"hl0s5o1\", \"4n482ts\", \"a307vir\", \"3rcv4w6\"]\n", + "\n", + "# Assign `username` to a username\n", + "\n", + "username = \"sgilmore\"\n", + "\n", + "# Assign `device_id` to a device ID\n", + "\n", + "device_id = \"4n482ts\"\n", + "\n", + "# Assign `ind` to the index of `username` in `approved_users`\n", + "\n", + "ind = approved_users.index(username)\n", + "\n", + "# If statement\n", + "# If `username` belongs to `approved_users`, and if the element at `ind` in `approved_devices` matches `device_id`,\n", + "# then display a message that the username is approved,\n", + "# followed by a message that the user has the correct device\n", + "\n", + "if username in approved_users and device_id == approved_devices[ind]:\n", + " print(\"The user\", username, \"is approved to access the system.\")\n", + " print(device_id, \"is the assigned device for\", username)\n", + "\n", + "# Elif statement\n", + "# Handles the case when `username` belongs to `approved_users` but element at `ind` in `approved_devices` does not match `device_id`,\n", + "# and displays two messages accordingly\n", + "\n", + "elif username in approved_users and device_id != approved_devices[ind]:\n", + " print(\"The user\", username, \"is approved to access the system, but\", device_id, \"is not their assigned device.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WwXQIKT5Isjs" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "In the `elif` statement, use the `in` operator to check whether `username` belongs to `approved_users`, use a comparison operator to check whether the element at `ind` in `approved_devices` doesn't match `device_id`, and use a logical operator to connect these two conditions to check whether both of them are met.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WCYmmHB4Isj2" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "In the `elif` statement, use the `in` operator to check whether `username` belongs to `approved_users`, use the `!=` comparison operator to check whether the element at `ind` in `approved_devices` doesn't match `device_id`, and use the `and` logical operator to connect these two conditions to check whether both of them are met.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V9Bo5WDCIiSW" + }, + "source": [ + "#### **Question 8**\n", + "**What do you observe from the output when `username` is `\"sgilmore\"` and `device_id` is `\"4n482ts\"`?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IBq7GluGYFnK" + }, + "source": [ + "I believe when the username is `\"sgilmore\"` and the device ID is `\"4n482ts\"`, the output shows two clear messages: the first line states that sgilmore is approved to access the system, and the second line confirms that 4n482ts is the assigned device for sgilmore. If the username were not in the `approved_users` list, the message would explain that the user is not approved to access the system. However, if the username were approved but the device ID did not match, the output would indicate that the user is approved, but the device ID is not assigned to them. I believe this process ensures proper verification of both user access and their associated device.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gNmZaLueDnqM" + }, + "source": [ + "## Task 9\n", + "In this task, you'll complete your algorithm by developing a function that uses some of the code you've written in earlier tasks. This will automate the login process.\n", + "\n", + "There are multiple ways to use conditionals to automate the login process. In the following code, a nested conditional is used to achieve the goals of the algorithm. There is a conditional statement inside of another conditional statement. The outer conditional handles the case when the `username` is approved and the case when `username` is not approved. The inner conditional, which is placed inside the first `if` statement, handles the case when the `username` is approved and the `device_id` is correct, as well as the case when the `username` is approved and the `device_id` is incorrect.\n", + "\n", + "To complete this task, you must define a function named `login` that takes in two parameters, `username` and `device_id`. Afterwards, call the function and pass in different username and device ID combinations to experiment and observe the function's behavior. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "bhcjqvDmkNnw" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The user bmoreno is approved to access the system.\n", + "hl0s5o1 is the assigned device for bmoreno\n", + "The user elarson is approved to access the system.\n", + "r2s5r9g is not their assigned device.\n", + "The username abernard is not approved to access the system.\n" + ] + } + ], + "source": [ + "# Assign `approved_users` to a list of approved usernames\n", + "\n", + "approved_users = [\"elarson\", \"bmoreno\", \"sgilmore\", \"eraab\", \"gesparza\"]\n", + "\n", + "# Assign `approved_devices` to a list of device IDs that correspond to the usernames in `approved_users`\n", + "\n", + "approved_devices = [\"8rp2k75\", \"hl0s5o1\", \"4n482ts\", \"a307vir\", \"3rcv4w6\"]\n", + "\n", + "# Define a function named `login` that takes in two parameters, `username` and `device_id`\n", + "\n", + "def login(username, device_id):\n", + "\n", + " # If `username` belongs to `approved_users`,\n", + "\n", + " if username in approved_users:\n", + "\n", + " # then display \"The user ______ is approved to access the system.\",\n", + "\n", + " print(\"The user\", username, \"is approved to access the system.\")\n", + "\n", + " # assign `ind` to the index of `username` in `approved_users`,\n", + "\n", + " ind = approved_users.index(username)\n", + "\n", + " # and execute the following conditional\n", + " # If `device_id` matches the element at the index `ind` in `approved_devices`,\n", + "\n", + " if device_id == approved_devices[ind]:\n", + "\n", + " # then display \"______ is the assigned device for ______\"\n", + "\n", + " print(device_id, \"is the assigned device for\", username)\n", + "\n", + " # Otherwise,\n", + "\n", + " else:\n", + "\n", + " # display \"______ is not their assigned device\"\n", + "\n", + " print(device_id, \"is not their assigned device.\")\n", + "\n", + " # Otherwise (part of the outer conditional and handles the case when `username` does not belong to `approved_users`),\n", + "\n", + " else:\n", + "\n", + " # Display \"The user ______ is not approved to access the system.\"\n", + "\n", + " print(\"The username\", username, \"is not approved to access the system.\")\n", + "\n", + "# Call the function you just defined to experiment with different username and device_id combinations\n", + "\n", + "login(\"bmoreno\", \"hl0s5o1\")\n", + "login(\"elarson\", \"r2s5r9g\")\n", + "login(\"abernard\", \"4n482ts\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3VpUpR6Zey9V" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `def` keyword to start the function definition.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uoGt3HqQey9f" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "After the `def` keyword, specify the name of the function, followed by parantheses and a colon. Inside the parantheses, specify the parameters that the function takes in.\n", + "\n", + "To call the function, write the name of the function, followed by parantheses, and pass in the username and device ID that you want to experiment with.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WxLVS38vhOxi" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "After the `def` keyword, write `login(username, device_id):` to complete the function definition header.\n", + "\n", + "To call the function, write `login()`, and pass in the username and device ID that you want to experiment with, separated by a comma. Keep in mind that the arguments you pass in are string data.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "woVZXLtPZv5i" + }, + "source": [ + "#### **Question 9**\n", + "**After Python enters the inner conditional, what happens when the `device_id` is correct, and what happens when the `device_id` is incorrect?**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "K3PHA77lr4nb" + }, + "source": [ + "After adding a def function() at the top of the if statments and then adding the prints : login(\"bmoreno\", \"hl0s5o1\")\n", + "login(\"elarson\", \"r2s5r9g\")\n", + "login(\"abernard\", \"4n482ts\"), I believe when Python reaches the inner conditional, it checks whether the `device_id` matches correctly. If the `device_id` is correct, the inner `if` statement evaluates to True, and Python displays a message confirming that the device ID is assigned to the user. If the `device_id` is incorrect, the condition evaluates to False, and Python moves to the else block, showing a message that the device ID is not the one assigned to that user. I believe this structure helps ensure accuracy when verifying user-device matches." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ru2ytGBLCh4q" + }, + "source": [ + "## Conclusion\n", + "\n", + "**What are your key takeaways from this lab?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ls2WEGqsiSAd" + }, + "source": [ + "I believe my key takeaways from this lab are that indexing works the same way for lists as it does for strings, starting at 0. I believe the `.append()` method is useful for adding new items to the end of a list, while the `.remove()` method lets you delete items. I believe the `.index()` method can be applied to both strings and lists to find the position of a specific element. I also believe that when two lists have related information in the same order, indices can be used to pair their elements accurately. Finally, I believe functions are powerful tools for creating algorithms, where you define the inputs (parameters) and the steps the function should perform." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [ + { + "file_id": "1KTA84JmoWjf4-1MyXsnnxxX2mlwEZwa7", + "timestamp": 1667280576202 + } + ] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Activity_Develop an algorithm.pdf b/Activity_Develop an algorithm.pdf new file mode 100644 index 0000000..e658b60 Binary files /dev/null and b/Activity_Develop an algorithm.pdf differ diff --git a/Activity_Import and parse a text file.ipynb b/Activity_Import and parse a text file.ipynb new file mode 100644 index 0000000..8cec05c --- /dev/null +++ b/Activity_Import and parse a text file.ipynb @@ -0,0 +1,813 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "BTj1ga3Gxuxe" + }, + "source": [ + "# Activity: Import and parse a text file" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ivt8p77hCKcO" + }, + "source": [ + "## Introduction\n", + "\n", + "Security logs are often stored in text files. To analyze the security logs in these files, security analysts have to import and parse these files. Python has some functions that come in handy for these tasks, allowing analysts to efficiently access information from text files. \n", + "\n", + "In this lab, you'll practice using functions and other syntax in Python to import and parse text files.\n", + "\n", + "**Note:** *Have you already completed this lab once?* Due to how Coursera handles files, you will need to reset the `login.txt\"` file used in this lab to its original contents if you want to complete this lab more than once. The [File contents reset](#file_contents_reset) section at the end of this notebook contains code that allows you to reset the `login.txt\"` file to its original contents. After you have run the code in that section, you can begin the lab again." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eW4pA4LsC-vb" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task. \n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook. \n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook. \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dQ2elTOeCMNv" + }, + "source": [ + "## Scenario\n", + "\n", + "In this lab, you're working as a security analyst. You're responsible for preparing a security log file for analysis and creating a text file with IP addresses that are allowed to access restricted information." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8lEFzu64x8T1" + }, + "source": [ + "## Task 1\n", + "In this task, you'll import a security log text file and store it as a string to prepare it for analysis. \n", + "\n", + "In Python, a `with` statement is often used in file handling to open a file and then automatically close the file after reading it.\n", + "\n", + "You're given a variable named `import_file` that contains the name of the log file that you want to import. Start by writing the first line of the `with` statement in the following code cell. Use the `open()` function, setting the second parameter to `\"r\"`. Note that running this code will produce an error because it will only contain the first line of the `with` statement; you'll complete this `with` statement in the task after this. Be sure to replace the `### YOUR CODE HERE ###` with your own code." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "id": "mDSFWg1nC7Qc" + }, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "unexpected EOF while parsing (, line 8)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m8\u001b[0m\n\u001b[0;31m with open(import_file, \"r\") as file:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unexpected EOF while parsing\n" + ] + } + ], + "source": [ + "# Assign `import_file` to the name of the text file that contains the security log file\n", + "\n", + "import_file = \"login.txt\"\n", + "\n", + "# First line of the `with` statement\n", + "# Use `open()` to import security log file and store it as a string\n", + "\n", + "with open(import_file, \"r\") as file:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "N4iCk5h-BEW4" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `open()` function in Python allows you to open a file.\n", + "\n", + "As the first parameter, it takes in the name of the file (or a variable containing the name of the file). As the second parameter, it takes in a string that indicates how the file should be handled.\n", + "\n", + "Pass in the letter `\"r\"` as the second argument when you want to read the file.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V2viFcW0FIID" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "The `import_file` variable contains the name of the file that you want to open, so that should be the first argument you pass in to the `open()` function. \n", + "\n", + "Since you also want to read the contents of the file, you should pass in `\"r\"` as the second argument. \n", + "\n", + "Make sure to use a comma (`,`) to separate the two arguments.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "T5G87NJGFmuv" + }, + "source": [ + "## Task 2\n", + "Now, you'll use the `.read()` method to read the imported file, and you'll store the result in a variable named `text`. Afterwards, display the `text` and explore what it contains by running the cell. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "LiUPiKKnGZE2" + }, + "outputs": [], + "source": [ + "# Assign `import_file` to the name of the text file that contains the security log file\n", + "\n", + "import_file = \"login.txt\"\n", + "\n", + "# The `with` statement\n", + "# Use `open()` to import security log file and store it as a string\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store the result in a variable named `text`\n", + "\n", + " text = file.read()\n", + "\n", + "# Display the contents of `text`\n", + "\n", + "print(text)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TuMsRoZTHJqb" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `.read()` method in Python converts text files to strings. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hZYf2YUUbpHP" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "The `file` object contains the file that you want to read, so apply the `.read()` method to `file`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sv4c2jpbHJqk" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `print()` function to display the contents of `text`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SnDwmUR1IcaA" + }, + "source": [ + "## Task 3\n", + "The output in the previous step is one big string. In this task, you'll explore how you can split the string that contains the entire imported log file into a list of strings, one string per line. \n", + "\n", + "Use the `.split()` method to perform this split and then display the result. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell. \n", + "\n", + "Note that displaying `.split()` doesn’t change what is stored in the `text` variable. Variable reassignment would be necessary if you want to store the result after splitting." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "9VMf3LGzQl8-" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['username,ip_address,time,date', 'tshah,192.168.92.147,15:26:08,2022-05-10', 'dtanaka,192.168.98.221,9:45:18,2022-05-09', 'tmitchel,192.168.110.131,14:13:41,2022-05-11', 'daquino,192.168.168.144,7:02:35,2022-05-08', 'eraab,192.168.170.243,1:45:14,2022-05-11', 'jlansky,192.168.238.42,1:07:11,2022-05-11', 'acook,192.168.52.90,9:56:48,2022-05-10', 'asundara,192.168.58.217,23:17:52,2022-05-12', 'jclark,192.168.214.49,20:49:00,2022-05-10', 'cjackson,192.168.247.153,19:36:42,2022-05-12', 'jclark,192.168.197.247,14:11:04,2022-05-12', 'apatel,192.168.46.207,17:39:42,2022-05-10', 'mabadi,192.168.96.244,10:24:43,2022-05-12', 'iuduike,192.168.131.147,17:50:00,2022-05-11', 'abellmas,192.168.60.111,13:37:05,2022-05-10', 'gesparza,192.168.148.80,6:30:14,2022-05-11', 'cgriffin,192.168.4.157,23:04:05,2022-05-09', 'alevitsk,192.168.210.228,8:10:43,2022-05-08', 'eraab,192.168.24.12,11:29:27,2022-05-11', 'jsoto,192.168.25.60,5:09:21,2022-05-09', 'jrafael,192.168.243.140,4:56:27,2022-05-09']\n" + ] + } + ], + "source": [ + "# Assign `import_file` to the name of the text file that contains the security log file\n", + "\n", + "import_file = \"login.txt\"\n", + "\n", + "# The `with` statement\n", + "# Use `open()` to import security log file and store it as a string\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read the imported file and store the result in a variable named `text`\n", + "\n", + " text = file.read()\n", + "\n", + "# Display the contents of `text` split into separate lines \n", + "\n", + "print(text.split())" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "izaB2i1XRo_h" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `.split()` method in Python converts a string into a list. It can take in a separator character that specifies which character to split on. If a character is not specified, it will split on whitespace by default. This default will work well for your task, since the log file contains whitespace between each line in the log. \n", + "\n", + "Note that whitespace includes any space between text on the same line and the space between one line and the next line.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lEn8WmKARo_j" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `.split()` method to convert the `text` into a list, where each element in the list represents a line in the log file.\n", + "\n", + "Place this between the parantheses in the `print()` function call.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DC2uOLAON0BW" + }, + "source": [ + "#### **Question 1**\n", + "**What do you notice about the output before and after using the `.split()` method?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "D_TIeLkYN6gv" + }, + "source": [ + "I believe that before using the `.split()` method, the log file’s contents appear as one long string. I also believe that after applying `.split()`, the output becomes a list of strings, with each string representing a single line from the log file.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1urIMObnTOA_" + }, + "source": [ + "## Task 4\n", + "There is a missing entry in the log file. You'll need to account for that by appending it to the log file. You're given the missing entry stored in a variable named `missing_entry`. \n", + "\n", + "Use the `.write()` method and the parameter `\"a\"` in the `open()` function. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell.\n", + "\n", + "After the portion of the code that writes to the file, another with statement uses the `.read()` method to read the updated file into the `text` variable and then display it." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "id": "QTpKEAl1Wscq" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "username,ip_address,time,date\n", + "tshah,192.168.92.147,15:26:08,2022-05-10\n", + "dtanaka,192.168.98.221,9:45:18,2022-05-09\n", + "tmitchel,192.168.110.131,14:13:41,2022-05-11\n", + "daquino,192.168.168.144,7:02:35,2022-05-08\n", + "eraab,192.168.170.243,1:45:14,2022-05-11\n", + "jlansky,192.168.238.42,1:07:11,2022-05-11\n", + "acook,192.168.52.90,9:56:48,2022-05-10\n", + "asundara,192.168.58.217,23:17:52,2022-05-12\n", + "jclark,192.168.214.49,20:49:00,2022-05-10\n", + "cjackson,192.168.247.153,19:36:42,2022-05-12\n", + "jclark,192.168.197.247,14:11:04,2022-05-12\n", + "apatel,192.168.46.207,17:39:42,2022-05-10\n", + "mabadi,192.168.96.244,10:24:43,2022-05-12\n", + "iuduike,192.168.131.147,17:50:00,2022-05-11\n", + "abellmas,192.168.60.111,13:37:05,2022-05-10\n", + "gesparza,192.168.148.80,6:30:14,2022-05-11\n", + "cgriffin,192.168.4.157,23:04:05,2022-05-09\n", + "alevitsk,192.168.210.228,8:10:43,2022-05-08\n", + "eraab,192.168.24.12,11:29:27,2022-05-11\n", + "jsoto,192.168.25.60,5:09:21,2022-05-09\n", + "jrafael,192.168.243.140,4:56:27,2022-05-09jrafael,192.168.243.140,4:56:27,2022-05-09\n" + ] + } + ], + "source": [ + "# Assign `import_file` to the name of the text file that contains the security log file\n", + "\n", + "import_file = \"login.txt\"\n", + "\n", + "# Assign `missing entry` to a log that was not recorded in the log file\n", + "\n", + "missing_entry = \"jrafael,192.168.243.140,4:56:27,2022-05-09\"\n", + "\n", + "# Use `open()` to import security log file and store it as a string\n", + "# Pass in \"a\" as the second parameter to indicate that the file is being opened for appending purposes\n", + "\n", + "with open(import_file, \"a\") as file:\n", + "\n", + " # Use `.write()` to append `missing_entry` to the log file\n", + "\n", + " file.write(missing_entry)\n", + "\n", + "# Use `open()` with the parameter \"r\" to open the security log file for reading purposes\n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Use `.read()` to read in the contents of the log file and store in a variable named `text`\n", + "\n", + " text = file.read()\n", + "\n", + "# Display the contents of `text`\n", + "\n", + "print(text)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UUGPJFRzW786" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `open()` function in Python allows you to open a file. \n", + "\n", + "As the first parameter, it takes in the name of the file (or a variable containing the name of the file). As the second parameter, it takes in a string that indicates how the file should be handled.\n", + "\n", + "Pass in the letter `\"a\"` as the second parameter when you want to append the file.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "y1u-m2SEW787" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Call the `.write()` method on the log file and pass in `missing_entry`. This will append `missing_entry` to the log file. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fBK1r6EnX4KG" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Call `file.write()` and pass in `missing_entry`. This will append `missing_entry` to the log file. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jQfXi8r1pth1" + }, + "source": [ + "#### **Question 2**\n", + "**What do you notice about the position of the entry that was added to the log file?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FF6YaeEOqC86" + }, + "source": [ + "I noticed about the position of the entry that was added to the log file, was that the additional entry was added to the end of the log file, so it appears in the last line of the output." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uXAdgPsGdYDY" + }, + "source": [ + "## Task 5\n", + "The next task you're responsible for is creating a text file. This text file should include a list of IP addresses that are allowed to access restricted information. Documenting this in a text file will help you communicate your findings to your security team. \n", + "\n", + "Start by creating a variable named `import_file` that stores the name of the file, which should be `\"allow_list.txt\"`. \n", + "\n", + "You're also given a variable named `ip_addresses` that stores a string containing the IP addresses that are allowed.\n", + "\n", + "Run the code to display the two variables and explore what they contain. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell. " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "1UxwR4Mdf4SN" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "data/allow_list.txt\n", + "192.168.218.160 192.168.97.225 192.168.145.158 192.168.108.13 192.168.60.153 192.168.96.200 192.168.247.153 192.168.3.252 192.168.116.187 192.168.15.110 192.168.39.246\n" + ] + } + ], + "source": [ + "# Assign `import_file` to the name of the text file that you want to create\n", + "\n", + "import_file = \"data/allow_list.txt\"\n", + "\n", + "# Assign `ip_addresses` to a list of IP addresses that are allowed to access the restricted information\n", + "\n", + "ip_addresses = \"192.168.218.160 192.168.97.225 192.168.145.158 192.168.108.13 192.168.60.153 192.168.96.200 192.168.247.153 192.168.3.252 192.168.116.187 192.168.15.110 192.168.39.246\"\n", + "\n", + "# Display `import_file`\n", + "\n", + "print(import_file)\n", + "\n", + "# Display `ip_addresses`\n", + "\n", + "print(ip_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "D75K_yO-gzI3" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Keep in mind that the name of the text file you want to create should be `\"allow_list.txt\"`. Make sure to include the `.txt` file extension, which specifies the file format.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "twOTzdiOhoA6" + }, + "source": [ + "## Task 6\n", + "Your next goal is to create a `with` statement in order to write the IP addresses to the text file you created in the previous step. \n", + "\n", + "You'll first open the file using the `\"w\"` parameter. Then, you'll write the IP addresses to the file. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell. Note that the code cell will contain a `with` statement that writes to a file but does not display information to the screen, so running it will not produce an output." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "c8JJxAYvkHuG" + }, + "outputs": [], + "source": [ + "# Assign `import_file` to the name of the text file that you want to create\n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `ip_addresses` to a list of IP addresses that are allowed to access the restricted information\n", + "\n", + "ip_addresses = \"192.168.218.160 192.168.97.225 192.168.145.158 192.168.108.13 192.168.60.153 192.168.96.200 192.168.247.153 192.168.3.252 192.168.116.187 192.168.15.110 192.168.39.246\"\n", + "\n", + "# Create a `with` statement to write to the text file \n", + "\n", + "with open(import_file, \"w\") as file:\n", + "\n", + " # Write `ip_addresses` to the text file\n", + "\n", + " file.write(ip_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wbguTQP0kVe3" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `open()` function in Python allows you to open a file. \n", + "\n", + "As the first parameter, it takes in the name of the file (or a variable containing the name of the file). As the second parameter, it takes in a string that indicates how the file should be handled. \n", + "\n", + "Pass in the letter `\"w\"` as the second parameter when you're opening a file for the purpose of writing to it.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oI2L1RTUkVe4" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Call the `.write()` method on the text file to write to it. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "k9kYhdtakxbF" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Call the `file.write()` method and pass in the `ip_addresses` variable to write the contents of that variable to the text file.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gBEIpHZQiDA5" + }, + "source": [ + "## Task 7\n", + "In this final step, you'll complete the code you've been writing up to this point. You'll add code to read the file containing IP addresses.\n", + "\n", + "Complete a `with` statement that reads the text file and stores it in a new variable called `text`. \n", + "\n", + "Afterwards, display the contents of `text` and run the cell to explore the result. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell. " + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "_gRyZCDhmcn8" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "192.168.218.160 192.168.97.225 192.168.145.158 192.168.108.13 192.168.60.153 192.168.96.200 192.168.247.153 192.168.3.252 192.168.116.187 192.168.15.110 192.168.39.246\n" + ] + } + ], + "source": [ + "# Assign `import_file` to the name of the text file that you want to create\n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `ip_addresses` to a list of IP addresses that are allowed to access the restricted information\n", + "\n", + "ip_addresses = \"192.168.218.160 192.168.97.225 192.168.145.158 192.168.108.13 192.168.60.153 192.168.96.200 192.168.247.153 192.168.3.252 192.168.116.187 192.168.15.110 192.168.39.246\"\n", + "\n", + "# Create a `with` statement to write to the text file \n", + "\n", + "with open(import_file, \"w\") as file:\n", + "\n", + " # Write `ip_addresses` to the text file\n", + "\n", + " file.write(ip_addresses)\n", + "\n", + "# Create a `with` statement to read in the text file \n", + "\n", + "with open(import_file, \"r\") as file:\n", + "\n", + " # Read the file and store the result in a variable named `text`\n", + "\n", + " text = file.read()\n", + "\n", + "# Display the contents of `text`\n", + "\n", + "print(text)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lRheuh50mq4X" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `open()` function in Python allows you to open a file. \n", + "\n", + "As the first parameter, it takes in the name of the file (or a variable containing the name of the file). As the second parameter, it takes in a string that indicates how the file should be handled. \n", + "\n", + "Pass in the letter `\"r\"` as the second parameter when you're opening a file for the purpose of reading in its contents.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fqoe1A9jmq4Y" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Call the `.read()` method on the text file to read it in.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Hr2Tjx7UAqNs" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Call `file.read()`. Place this to the right of the `=` operator to assign the output to the `text` variable. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ru2ytGBLCh4q" + }, + "source": [ + "## Conclusion\n", + "\n", + "**What are your key takeaways from this lab?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ls2WEGqsiSAd" + }, + "source": [ + "I believe that Python provides powerful tools for working with text files, which are essential for tasks like parsing logs. I believe that using the `with` statement helps handle files efficiently, and the `open()` function allows you to read, write, or append to a file depending on the mode specified (\"r\", \"w\", or \"a\"). I believe that the `.read()` method lets you read file contents, while the `.write()` method enables you to add or modify data in a file. I also believe that the `.split()` method is very useful because it converts a string into a list, making it easier to work with individual lines or pieces of data.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## File contents reset" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can run the following code to reset the `\"login.txt\"` file to its original contents. Because of how Coursera handles files, this will be necessary if you wish to complete this lab more than once or if you have unintentionally changed the file in a way that does not correspond to the lab tasks." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "# Resets the `\"login.txt\"` file to its original contents\n", + "# Allows learners to complete lab more than once\n", + "\n", + "# Assigns the original contents of the file to the `login_file` variable\n", + "login_file = \"\"\"username,ip_address,time,date\n", + "tshah,192.168.92.147,15:26:08,2022-05-10\n", + "dtanaka,192.168.98.221,9:45:18,2022-05-09\n", + "tmitchel,192.168.110.131,14:13:41,2022-05-11\n", + "daquino,192.168.168.144,7:02:35,2022-05-08\n", + "eraab,192.168.170.243,1:45:14,2022-05-11\n", + "jlansky,192.168.238.42,1:07:11,2022-05-11\n", + "acook,192.168.52.90,9:56:48,2022-05-10\n", + "asundara,192.168.58.217,23:17:52,2022-05-12\n", + "jclark,192.168.214.49,20:49:00,2022-05-10\n", + "cjackson,192.168.247.153,19:36:42,2022-05-12\n", + "jclark,192.168.197.247,14:11:04,2022-05-12\n", + "apatel,192.168.46.207,17:39:42,2022-05-10\n", + "mabadi,192.168.96.244,10:24:43,2022-05-12\n", + "iuduike,192.168.131.147,17:50:00,2022-05-11\n", + "abellmas,192.168.60.111,13:37:05,2022-05-10\n", + "gesparza,192.168.148.80,6:30:14,2022-05-11\n", + "cgriffin,192.168.4.157,23:04:05,2022-05-09\n", + "alevitsk,192.168.210.228,8:10:43,2022-05-08\n", + "eraab,192.168.24.12,11:29:27,2022-05-11\n", + "jsoto,192.168.25.60,5:09:21,2022-05-09\n", + "\"\"\"\n", + "\n", + "# Writes `login_file` to the `\"login.txt\"` file\n", + "with open(\"login.txt\", \"w\") as file:\n", + " file.write(login_file)" + ] + } + ], + "metadata": { + "colab": { + "provenance": [], + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Activity_Import and parse a text file.pdf b/Activity_Import and parse a text file.pdf new file mode 100644 index 0000000..75cea0b Binary files /dev/null and b/Activity_Import and parse a text file.pdf differ diff --git a/Activity_Use regular expressions to find patterns.ipynb b/Activity_Use regular expressions to find patterns.ipynb new file mode 100644 index 0000000..6184ff3 --- /dev/null +++ b/Activity_Use regular expressions to find patterns.ipynb @@ -0,0 +1,932 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "yhVY_dQEi1EN" + }, + "source": [ + "# Activity: Use regular expressions to find patterns" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ivt8p77hCKcO" + }, + "source": [ + "## Introduction\n", + "\n", + "Security analysts often analyze log files, including those that contain information about login attempts. For example as an analyst, you might flag IP addresses that relate to unusual attempts to log in to the system. \n", + "\n", + "Another area of focus in cybersecurity is detecting devices that require updates. Software updates help prevent security issues due to vulnerabilities.\n", + "\n", + "Using regular expressions in Python can help automate the processes involved in both of these areas of cybersecurity. Regular expression patterns and functions can be used to efficiently extract important information from strings and files. \n", + "\n", + "In this lab, you'll write regular expressions to extract information such as device IDs or IP addresses. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eW4pA4LsC-vb" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task. \n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook. \n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook. \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dQ2elTOeCMNv" + }, + "source": [ + "## Scenario\n", + "\n", + "In this lab, you're working as a security analyst and your main tasks are as follows:\n", + "- extracting device IDs containing certain characters from a log; these characters correspond with a certain operating system that requires an update.\n", + "- extracting all IP addresses from a log and then comparing them to those that are flagged in a list." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HoVpKSvyi-uI" + }, + "source": [ + "## Task 1\n", + "In order to work with regular expressions in Python, start by importing the `re` module. This module contains many functions that will help you work with regular expressions. By running the following code cell, the module will be available through the rest of the notebook." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "3G0JhqZCiULp" + }, + "outputs": [], + "source": [ + "# Import the `re` module in Python\n", + "\n", + "import re" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jF8bZT0SkiFr" + }, + "source": [ + "## Task 2\n", + "Currently, you are looking for device IDs that begin with `\"r15\"`. These characters indicate that the device is running an operating system that must be updated.\n", + "\n", + "You're given a log of device IDs, stored in a variable named `devices`. Your eventual goal is to extract the device IDs that start with the characters `\"r15\"`. For now, display the contents of the whole string to examine what it contains. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "id": "G54f4AzBrrYM" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "r262c36 67bv8fy 41j1u2e r151dm4 1270t3o 42dr56i r15xk9h 2j33krk 253be78 ac742a1 r15u9q5 zh86b2l ii286fq 9x482kt 6oa6m6u x3463ac i4l56nq g07h55q 081qc9t r159r1u\n" + ] + } + ], + "source": [ + "# Assign `devices` to a string containing device IDs, each device ID represented by alphanumeric characters\n", + "\n", + "devices = \"r262c36 67bv8fy 41j1u2e r151dm4 1270t3o 42dr56i r15xk9h 2j33krk 253be78 ac742a1 r15u9q5 zh86b2l ii286fq 9x482kt 6oa6m6u x3463ac i4l56nq g07h55q 081qc9t r159r1u\"\n", + "\n", + "# Display the contents of `devices`\n", + "\n", + "print(devices)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3VpUpR6Zey9V" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `print()` function to display the contents of `devices`. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ExgZlBBYvcpT" + }, + "source": [ + "## Task 3\n", + "In this task, you'll write a pattern to find devices that start with the character combination of `\"r15\"`. \n", + "\n", + "Use the regular expression symbols `\\w` and `+` to create the pattern, and store it as a string in a variable named `target_pattern`.\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell. Note that the code cell will contain only variable assignments, so running it will not produce an output." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "id": "caT1IV5HyLUo" + }, + "outputs": [], + "source": [ + "# Assign `devices` to a string containing device IDs, each device ID represented by alphanumeric characters\n", + "\n", + "devices = \"r262c36 67bv8fy 41j1u2e r151dm4 1270t3o 42dr56i r15xk9h 2j33krk 253be78 ac742a1 r15u9q5 zh86b2l ii286fq 9x482kt 6oa6m6u x3463ac i4l56nq g07h55q 081qc9t r159r1u\"\n", + "\n", + "# Assign `target_pattern` to a regular expression pattern for finding device IDs that start with \"r15\"\n", + "\n", + "target_pattern = \"r15\\w+\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cAEqqWJuyaEJ" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The regular expression symbol `\\w` matches with any alphanumeric character.\n", + "\n", + "The regular expression symbol `+` matches with one or more occurrences of the character before it in the pattern.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O13yc9K-3vqZ" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Combining the regular expression symbols `\\w` and `+` results in `\\w+`, which matches with an alphanumeric string of any length.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ztnUlDKaZ_on" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Since `target_pattern` needs to match with an alphanumeric string that starts with `\"r15\"`, make sure the pattern starts with `r15`, followed by `\\w+`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uyvpm4lZVtSQ" + }, + "source": [ + "#### **Question 1**\n", + "**What regular expression pattern did you use? For each component of the pattern, what would happen if it were missing?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r-pyoFv-ZTF7" + }, + "source": [ + "I believe the key takeaway is that the regular expression pattern `\"r15\\w+\"` is designed to match device IDs that start with `\"r15\"` followed by one or more alphanumeric characters. I believe the `\"r15\"` ensures the ID starts with those characters, the `\\w` allows it to include letters and numbers after `\"r15\"`, and the `+` ensures it matches all consecutive alphanumeric characters, not just the first one." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9-B2F3CDyWcv" + }, + "source": [ + "## Task 4\n", + "Use the `findall()` function from the `re` module to find the device IDs that the `target_pattern` matches with. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell.\n", + "\n", + "**Note:** In order to use `re.findall()` in Tasks 4, 7, 8, 9 and 11, you must have previously run the code `import re` in Task 1." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "id": "8F8yjPpu75Sw" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['r151dm4', 'r15xk9h', 'r15u9q5', 'r159r1u']\n" + ] + } + ], + "source": [ + "# Assign `devices` to a string containing device IDs, each device ID represented by alphanumeric characters\n", + "\n", + "devices = \"r262c36 67bv8fy 41j1u2e r151dm4 1270t3o 42dr56i r15xk9h 2j33krk 253be78 ac742a1 r15u9q5 zh86b2l ii286fq 9x482kt 6oa6m6u x3463ac i4l56nq g07h55q 081qc9t r159r1u\"\n", + "\n", + "# Assign `target_pattern` to a regular expression pattern for finding device IDs that start with \"r15\"\n", + "\n", + "target_pattern = \"r15\\w+\"\n", + "\n", + "# Use `re.findall()` to find the device IDs that start with \"r15\" and display the results\n", + "\n", + "print(re.findall(target_pattern, devices))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c8a8jzH-8CBu" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `findall()` function from the `re` module takes in a regular expression, followed by a string. The function applies the regular expression to the string and returns a list of matches.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MqTk0BN-8CBv" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "When calling the `re.findall()` function, pass in the `target_pattern` variable as the first argument and the `devices` variable as the second argument. This will ensure that `target_pattern` is applied to the string stored in `devices`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "abqX_MsSFWin" + }, + "source": [ + "## Task 5\n", + "Now, the next task you're responsible for is analyzing a network security log file and determining which IP addresses have been flagged for unusual activity. \n", + "\n", + "You're given the log file as a string stored in a variable named `log_file`. There are some invalid IP addresses in the log file due to issues in data collection. Your eventual goal is to use regular expressions to extract the valid IP addresses from the string. \n", + "\n", + "Start by displaying the contents of the `log_file` to examine the details inside. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "Y2g3Dd0CoD_k" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "eraab 2022-05-10 6:03:41 192.168.152.148 \n", + "iuduike 2022-05-09 6:46:40 192.168.22.115 \n", + "smartell 2022-05-09 19:30:32 192.168.190.178 \n", + "arutley 2022-05-12 17:00:59 1923.1689.3.24 \n", + "rjensen 2022-05-11 0:59:26 192.168.213.128 \n", + "aestrada 2022-05-09 19:28:12 1924.1680.27.57 \n", + "asundara 2022-05-11 18:38:07 192.168.96.200 \n", + "dkot 2022-05-12 10:52:00 1921.168.1283.75 \n", + "abernard 2022-05-12 23:38:46 19245.168.2345.49 \n", + "cjackson 2022-05-12 19:36:42 192.168.247.153 \n", + "jclark 2022-05-10 10:48:02 192.168.174.117 \n", + "alevitsk 2022-05-08 12:09:10 192.16874.1390.176 \n", + "jrafael 2022-05-10 22:40:01 192.168.148.115 \n", + "yappiah 2022-05-12 10:37:22 192.168.103.10654 \n", + "daquino 2022-05-08 7:02:35 192.168.168.144\n" + ] + } + ], + "source": [ + "# Assign `log_file` to a string containing username, date, login time, and IP address for a series of login attempts \n", + "\n", + "log_file = \"eraab 2022-05-10 6:03:41 192.168.152.148 \\niuduike 2022-05-09 6:46:40 192.168.22.115 \\nsmartell 2022-05-09 19:30:32 192.168.190.178 \\narutley 2022-05-12 17:00:59 1923.1689.3.24 \\nrjensen 2022-05-11 0:59:26 192.168.213.128 \\naestrada 2022-05-09 19:28:12 1924.1680.27.57 \\nasundara 2022-05-11 18:38:07 192.168.96.200 \\ndkot 2022-05-12 10:52:00 1921.168.1283.75 \\nabernard 2022-05-12 23:38:46 19245.168.2345.49 \\ncjackson 2022-05-12 19:36:42 192.168.247.153 \\njclark 2022-05-10 10:48:02 192.168.174.117 \\nalevitsk 2022-05-08 12:09:10 192.16874.1390.176 \\njrafael 2022-05-10 22:40:01 192.168.148.115 \\nyappiah 2022-05-12 10:37:22 192.168.103.10654 \\ndaquino 2022-05-08 7:02:35 192.168.168.144\"\n", + "\n", + "# Display contents of `log_file`\n", + "\n", + "print(log_file)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qPbHZuvJoDKi" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `print()` function to display the contents of the `log_file`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0ELFuaUXpHKf" + }, + "source": [ + "## Task 6\n", + "In this task, you'll build a regular expression pattern that you can use later on to extract IP addresses that are in the form of xxx.xxx.xxx.xxx. In other words, you'll extract all IP addresses that contain four segments of three digits that are separated by periods.\n", + "\n", + "Write a regular expression pattern that will match with these IP addresses and store it in a variable named `pattern`. Use the regular expression symbols `\\d` and `\\.` in your pattern. Note that the symbol `\\d` matches with digits, in other words, any integer between 0 and 9. Be sure to replace the `### YOUR CODE HERE ###` with your own code. Since you'll just build the pattern here, there won't be any output when you run this cell. " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "id": "y5gfil-k5kD7" + }, + "outputs": [], + "source": [ + "# Assign `log_file` to a string containing username, date, login time, and IP address for a series of login attempts \n", + "\n", + "log_file = \"eraab 2022-05-10 6:03:41 192.168.152.148 \\niuduike 2022-05-09 6:46:40 192.168.22.115 \\nsmartell 2022-05-09 19:30:32 192.168.190.178 \\narutley 2022-05-12 17:00:59 1923.1689.3.24 \\nrjensen 2022-05-11 0:59:26 192.168.213.128 \\naestrada 2022-05-09 19:28:12 1924.1680.27.57 \\nasundara 2022-05-11 18:38:07 192.168.96.200 \\ndkot 2022-05-12 10:52:00 1921.168.1283.75 \\nabernard 2022-05-12 23:38:46 19245.168.2345.49 \\ncjackson 2022-05-12 19:36:42 192.168.247.153 \\njclark 2022-05-10 10:48:02 192.168.174.117 \\nalevitsk 2022-05-08 12:09:10 192.16874.1390.176 \\njrafael 2022-05-10 22:40:01 192.168.148.115 \\nyappiah 2022-05-12 10:37:22 192.168.103.10654 \\ndaquino 2022-05-08 7:02:35 192.168.168.144\"\n", + "\n", + "# Assign `pattern` to a regular expression pattern that will match with IP addresses of the form xxx.xxx.xxx.xxx\n", + "\n", + "pattern = \"\\d\\d\\d\\.\\d\\d\\d\\.\\d\\d\\d\\.\\d\\d\\d\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sw8c673yvGTY" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `\\.` symbol matches with periods. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mTYHAPe25n6w" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Recall that this task's focus is on IP addresses in the form of xxx.xxx.xxx.xxx, where each x is a digit. In other words, these addresses have the following format: three digits followed by a period, three digits followed by a period, three digits followed by a period, three digits.\n", + "\n", + "To build a pattern that matches with IP addresses in this form, use the `\\d` symbol for every digit and the `\\.` for every period that should be in the IP address.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "j1p2BFuD87Vv" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "You can use the regular expression `\\d\\d\\d\\.` to match with a segment of three digits followed by a period.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Qb40iqNU9Wwq" + }, + "source": [ + "## Task 7\n", + "In this task, you'll use the `re.findall()` function on the regular expression pattern stored in the `pattern` variable and the provided `log_file` to extract the corresponding IP addresses. Afterwards, run the cell and take note of what it outputs. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "l80Q6EjF-p8_" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['192.168.152.148', '192.168.190.178', '192.168.213.128', '192.168.247.153', '192.168.174.117', '192.168.148.115', '192.168.103.106', '192.168.168.144']\n" + ] + } + ], + "source": [ + "# Assign `log_file` to a string containing username, date, login time, and IP address for a series of login attempts \n", + "\n", + "log_file = \"eraab 2022-05-10 6:03:41 192.168.152.148 \\niuduike 2022-05-09 6:46:40 192.168.22.115 \\nsmartell 2022-05-09 19:30:32 192.168.190.178 \\narutley 2022-05-12 17:00:59 1923.1689.3.24 \\nrjensen 2022-05-11 0:59:26 192.168.213.128 \\naestrada 2022-05-09 19:28:12 1924.1680.27.57 \\nasundara 2022-05-11 18:38:07 192.168.96.200 \\ndkot 2022-05-12 10:52:00 1921.168.1283.75 \\nabernard 2022-05-12 23:38:46 19245.168.2345.49 \\ncjackson 2022-05-12 19:36:42 192.168.247.153 \\njclark 2022-05-10 10:48:02 192.168.174.117 \\nalevitsk 2022-05-08 12:09:10 192.16874.1390.176 \\njrafael 2022-05-10 22:40:01 192.168.148.115 \\nyappiah 2022-05-12 10:37:22 192.168.103.10654 \\ndaquino 2022-05-08 7:02:35 192.168.168.144\"\n", + "\n", + "# Assign `pattern` to a regular expression pattern that will match with IP addresses of the form xxx.xxx.xxx.xxx\n", + "\n", + "pattern = \"\\d\\d\\d\\.\\d\\d\\d\\.\\d\\d\\d\\.\\d\\d\\d\"\n", + "\n", + "# Use the `re.findall()` function on `pattern` and `log_file` to extract the IP addresses of the form xxx.xxx.xxx.xxx and display the results\n", + "\n", + "print(re.findall(pattern, log_file))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_IWrRh9a_T9K" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `re.findall()` function takes in a regular expression, followed by a string. The function applies the regular expression to the string and returns a list of matches.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sz-igmPA_T9S" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "When calling the `re.findall()` function, pass in the `pattern` variable as the first argument and the `log_file` variable as the second argument. This will ensure that `pattern` is applied to the string stored in `log_file`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "upPckuSQzkMG" + }, + "source": [ + "#### **Question 2**\n", + "**What are some examples of IP addresses that were extracted? What are some examples of IP addresses that were not extracted? Do any that were not extracted seem to be valid IP addresses?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XMQ4u-oYzu3_" + }, + "source": [ + "I believe the key takeaway is that the extraction process captured IP addresses like `\"192.168.152.148\"` and `\"192.168.190.178\"` that have three digits in each segment. I believe addresses with fewer than three digits per segment, such as `\"192.168.22.115\"`, are still valid IPs but were not extracted. This shows that the extraction method specifically targeted IPs with exactly three digits in each segment.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a01eDQn7_mEB" + }, + "source": [ + "## Task 8\n", + "There are some valid IP addresses in the `log_file` that you haven't extracted yet. This is because each segment of digits in a valid IP address can have anywhere between one and three digits. \n", + "\n", + "Adjust the regular expression in the `pattern` to allow for variation in the number of digits in each segment. You can do this by using the `+` symbol after the `\\d` symbol. Afterwards, use the updated `pattern` to extract remaining IP addresses. Then, run the cell to analyze the results. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "SdBKrJigFrS3" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['192.168.152.148', '192.168.22.115', '192.168.190.178', '1923.1689.3.24', '192.168.213.128', '1924.1680.27.57', '192.168.96.200', '1921.168.1283.75', '19245.168.2345.49', '192.168.247.153', '192.168.174.117', '192.16874.1390.176', '192.168.148.115', '192.168.103.10654', '192.168.168.144']\n" + ] + } + ], + "source": [ + "# Assign `log_file` to a string containing username, date, login time, and IP address for a series of login attempts \n", + "\n", + "log_file = \"eraab 2022-05-10 6:03:41 192.168.152.148 \\niuduike 2022-05-09 6:46:40 192.168.22.115 \\nsmartell 2022-05-09 19:30:32 192.168.190.178 \\narutley 2022-05-12 17:00:59 1923.1689.3.24 \\nrjensen 2022-05-11 0:59:26 192.168.213.128 \\naestrada 2022-05-09 19:28:12 1924.1680.27.57 \\nasundara 2022-05-11 18:38:07 192.168.96.200 \\ndkot 2022-05-12 10:52:00 1921.168.1283.75 \\nabernard 2022-05-12 23:38:46 19245.168.2345.49 \\ncjackson 2022-05-12 19:36:42 192.168.247.153 \\njclark 2022-05-10 10:48:02 192.168.174.117 \\nalevitsk 2022-05-08 12:09:10 192.16874.1390.176 \\njrafael 2022-05-10 22:40:01 192.168.148.115 \\nyappiah 2022-05-12 10:37:22 192.168.103.10654 \\ndaquino 2022-05-08 7:02:35 192.168.168.144\"\n", + "\n", + "# Update `pattern` to a regular expression pattern that will match with IP addresses with any variation in the number of digits per segment\n", + "\n", + "pattern = \"\\d+\\.\\d+\\.\\d+\\.\\d+\"\n", + "\n", + "# Use the `re.findall()` function on `pattern` and `log_file` to extract the IP addresses of the updated form specifed above and display the results\n", + "\n", + "print(re.findall(pattern, log_file))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7sPldstrF8Wh" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The regular expression symbol `+` represents one or more occurrences of a specific character. \n", + "\n", + "The regular expression symbol `\\d` matches with digits, in other words any integer between 0 and 9.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZJFBq1M_F8Wh" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Placing `+` after `\\d` results in `\\d+`, which will match with one or more digits.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qEmeRX5l3Lmz" + }, + "source": [ + "#### **Question 3**\n", + "**What gets extracted here? Do all extracted IP addresses have between one and three digits in every segment?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-nF1H-_h3cNN" + }, + "source": [ + "I believe the key takeaway is that the extraction now captures a wider range of IP addresses, including those with exactly three digits per segment (like `\"192.168.152.148\"`), fewer than three digits per segment (like `\"192.168.22.115\"`), and more than three digits per segment (like `\"1923.1689.3.24\"`). I believe this shows that not all extracted IPs strictly follow the standard one-to-three-digit-per-segment format.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JYLRsxyuGudY" + }, + "source": [ + "## Task 9\n", + "Note that all the IP addresses are now extracted but they also include invalid IP addresses with more than three digits per segment.\n", + "\n", + "In this task, you'll update the `pattern` using curly brackets instead of the `+` symbol. In regular expressions, curly brackets can be used to represent an exact number of repetitions between two numbers. For example, `{2,4}` in a regular expression means between 2 and 4 occurrences of something. Applying this to an example, `\\w{2,4}` would match with two, three, or four alphanumeric characters. Afterwards, you'll call the `re.findall()` function on the updated `pattern` and the `log_file` and store the output in a variable named `valid_ip_addresses`. \n", + "\n", + "Then, display the contents of `valid_ip_addresses` and run the cell to analyze the results. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "9fZrs8YMK9yM" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['192.168.152.148', '192.168.22.115', '192.168.190.178', '192.168.213.128', '192.168.96.200', '192.168.247.153', '192.168.174.117', '192.168.148.115', '192.168.103.106', '192.168.168.144']\n" + ] + } + ], + "source": [ + "# Assign `log_file` to a string containing username, date, login time, and IP address for a series of login attempts \n", + "\n", + "log_file = \"eraab 2022-05-10 6:03:41 192.168.152.148 \\niuduike 2022-05-09 6:46:40 192.168.22.115 \\nsmartell 2022-05-09 19:30:32 192.168.190.178 \\narutley 2022-05-12 17:00:59 1923.1689.3.24 \\nrjensen 2022-05-11 0:59:26 192.168.213.128 \\naestrada 2022-05-09 19:28:12 1924.1680.27.57 \\nasundara 2022-05-11 18:38:07 192.168.96.200 \\ndkot 2022-05-12 10:52:00 1921.168.1283.75 \\nabernard 2022-05-12 23:38:46 19245.168.2345.49 \\ncjackson 2022-05-12 19:36:42 192.168.247.153 \\njclark 2022-05-10 10:48:02 192.168.174.117 \\nalevitsk 2022-05-08 12:09:10 192.16874.1390.176 \\njrafael 2022-05-10 22:40:01 192.168.148.115 \\nyappiah 2022-05-12 10:37:22 192.168.103.10654 \\ndaquino 2022-05-08 7:02:35 192.168.168.144\"\n", + "\n", + "# Assign `pattern` to a regular expression that matches with all valid IP addresses and only those \n", + "\n", + "pattern = \"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\"\n", + "\n", + "# Use `re.findall()` on `pattern` and `log_file` and assign `valid_ip_addresses` to the output \n", + "\n", + "valid_ip_addresses = re.findall(pattern, log_file)\n", + "\n", + "# Display the contents of `valid_ip_addresses`\n", + "\n", + "print(valid_ip_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SZKQ_HRSLHTU" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Recall that curly brackets in regular expressions match with a number of repetitions between two specified numbers. \n", + "\n", + "To build a regular expression pattern that matches with anywhere between one and three digits, use `\\d{1,3}`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-odncroJLHTU" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Recall that a valid IP address consists of four segments of three digits each, separated by periods. \n", + "\n", + "To represent a segment of three digits followed by a period, use `\\d{1,3}\\.` in the regular expression pattern you build. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4h-vE1NV-oHh" + }, + "source": [ + "#### **Question 4**\n", + "**What do you notice about the extracted IP addresses here compared to those extracted in the previous two tasks?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "I6UIRWsh_gPj" + }, + "source": [ + "I believe the key takeaway is that in this task, the extracted IP addresses all have one to three digits per segment, which is more accurate than previous tasks. I believe this improves on Task 7, which only captured IPs with exactly three digits per segment, and Task 8, which also included IPs with more than three digits per segment." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fN5DOA8kManD" + }, + "source": [ + "## Task 10\n", + "Now, all of the valid IP addresses have been extracted. The next step is to identify flagged IP addresses.\n", + "\n", + "You're given a list of IP addresses that have been previously flagged for unusual activity, stored in a variable named `flagged_addresses`. When these addresses are encountered, they should be investigated further. This list is just for educational purposes and contains examples of private IP addresses that are found only within internal networks.\n", + "\n", + "Display this list and examine what it contains by running the cell. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "L865MZkKOrAX" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['192.168.190.178', '192.168.96.200', '192.168.174.117', '192.168.168.144']\n" + ] + } + ], + "source": [ + "# Assign `flagged_addresses` to a list of IP addresses that have been previously flagged for unusual activity\n", + "\n", + "flagged_addresses = [\"192.168.190.178\", \"192.168.96.200\", \"192.168.174.117\", \"192.168.168.144\"]\n", + "\n", + "# Display the contents of `flagged_addresses`\n", + "\n", + "print(flagged_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "00n15oJIPDlq" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `print()` function to display the contents of `flagged_addresses`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Tk91RTC_PHx2" + }, + "source": [ + "## Task 11\n", + "Finally, you will write an iterative statement that loops through the `valid_ip_addresses` list and checks if each IP address is flagged. In the following code, the `address` will be the loop variable. Also, include a conditional that checks if the `address` belongs to the `flagged_addresses` list. If so, it should display `\"The IP address ______ has been flagged for further analysis.\"` If not, it should display `\"The IP address ______ does not require further analysis.\"` Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "2zWUMGPsRw8-" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The IP address 192.168.152.148 does not require further analysis.\n", + "The IP address 192.168.22.115 does not require further analysis.\n", + "The IP address 192.168.190.178 has been flagged for further analysis.\n", + "The IP address 192.168.213.128 does not require further analysis.\n", + "The IP address 192.168.96.200 has been flagged for further analysis.\n", + "The IP address 192.168.247.153 does not require further analysis.\n", + "The IP address 192.168.174.117 has been flagged for further analysis.\n", + "The IP address 192.168.148.115 does not require further analysis.\n", + "The IP address 192.168.103.106 does not require further analysis.\n", + "The IP address 192.168.168.144 has been flagged for further analysis.\n" + ] + } + ], + "source": [ + "# Assign `log_file` to a string containing username, date, login time, and IP address for a series of login attempts \n", + "\n", + "log_file = \"eraab 2022-05-10 6:03:41 192.168.152.148 \\niuduike 2022-05-09 6:46:40 192.168.22.115 \\nsmartell 2022-05-09 19:30:32 192.168.190.178 \\narutley 2022-05-12 17:00:59 1923.1689.3.24 \\nrjensen 2022-05-11 0:59:26 192.168.213.128 \\naestrada 2022-05-09 19:28:12 1924.1680.27.57 \\nasundara 2022-05-11 18:38:07 192.168.96.200 \\ndkot 2022-05-12 10:52:00 1921.168.1283.75 \\nabernard 2022-05-12 23:38:46 19245.168.2345.49 \\ncjackson 2022-05-12 19:36:42 192.168.247.153 \\njclark 2022-05-10 10:48:02 192.168.174.117 \\nalevitsk 2022-05-08 12:09:10 192.16874.1390.176 \\njrafael 2022-05-10 22:40:01 192.168.148.115 \\nyappiah 2022-05-12 10:37:22 192.168.103.10654 \\ndaquino 2022-05-08 7:02:35 192.168.168.144\"\n", + "\n", + "# Assign `pattern` to a regular expression that matches with all valid IP addresses and only those \n", + "\n", + "pattern = \"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\"\n", + "\n", + "# Use `re.findall()` on `pattern` and `log_file` and assign `valid_ip_addresses` to the output \n", + "\n", + "valid_ip_addresses = re.findall(pattern, log_file)\n", + "\n", + "# Assign `flagged_addresses` to a list of IP addresses that have been previously flagged for unusual activity\n", + "\n", + "flagged_addresses = [\"192.168.190.178\", \"192.168.96.200\", \"192.168.174.117\", \"192.168.168.144\"]\n", + "\n", + "# Iterative statement begins here\n", + "# Loop through `valid_ip_addresses` with `address` as the loop variable\n", + "\n", + "for address in valid_ip_addresses:\n", + "\n", + " # Conditional begins here\n", + " # If `address` belongs to `flagged_addresses`, display \"The IP address ______ has been flagged for further analysis.\"\n", + "\n", + " if address in flagged_addresses:\n", + " print(\"The IP address\", address, \"has been flagged for further analysis.\")\n", + "\n", + " # Otherwise, display \"The IP address ______ does not require further analysis.\"\n", + "\n", + " else:\n", + " print(\"The IP address\", address, \"does not require further analysis.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "p9mJTIpiR9sh" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Complete the `for` loop condition so that the loop iterates through the `valid_ip_addresses` list.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eLd4aRj8Gvc3" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Complete the `if` condition so that the `if` statement checks whether the value of the loop variable `address` is in the `flagged_addresses` list.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_H_t0mnzGwYU" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Inside the `else` statement, use the `print()` function to display the specified message.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-U4MTWPXdGf2" + }, + "source": [ + "## Conclusion\n", + "\n", + "**What are your key takeaways from this lab?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ls2WEGqsiSAd" + }, + "source": [ + "I believe the key takeaways from this lab are that regular expressions in Python let you create patterns to find important strings, and these patterns can be designed to match specific characters or combinations of characters. I believe symbols like `\\w`, `+`, `\\d`, `\\.`, and `{x,y}` are useful for specifying alphanumeric characters, repetitions, digits, periods, and ranges of occurrences. I believe the `re` module provides functions, such as `re.findall()`, which can search a string for all matches to a pattern and return them as a list, making it easier to extract and analyze data.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [ + { + "file_id": "1sEmFwoLDCRw8rCC0TFtCqPOh7Gb06DDl", + "timestamp": 1667282243028 + } + ], + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Activity_Use regular expressions to find patterns.pdf b/Activity_Use regular expressions to find patterns.pdf new file mode 100644 index 0000000..6b6ca5f Binary files /dev/null and b/Activity_Use regular expressions to find patterns.pdf differ diff --git a/Activity_Work with strings in Python.ipynb b/Activity_Work with strings in Python.ipynb new file mode 100644 index 0000000..eee1241 --- /dev/null +++ b/Activity_Work with strings in Python.ipynb @@ -0,0 +1,887 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "5e2a85c3-4d7c-43c0-b9dd-ef7af16eb73d" + }, + "source": [ + "# Activity: Work with strings in Python" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "07edb7a1-c2cb-49cb-94e6-886268e48715" + }, + "source": [ + "## Introduction\n", + "\n", + "Security analysts work with a lot of string data. For example, some security analysts work on creating and updating IDs such as employee IDs and device IDs, which are commonly represented as strings. As another example, certain network activity will be stored as string data. Becoming comfortable working with strings in Python is essential for the work of a security analyst.\n", + "\n", + "In this lab, you'll practice creating Python code and working with strings. You'll work with an employee ID, a device ID, and a URL, all represented as string data." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bM8zStFIWIkI" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace that with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task. \n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook. \n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook. \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vrD-eN3FVY65" + }, + "source": [ + "## Scenario\n", + "\n", + "You're working as a security analyst, and you are responsible for writing programs in Python to automate updating employee IDs, extracting characters from a device ID, and extracting components from a URL." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zSti7mdls5Ht" + }, + "source": [ + "## Task 1\n", + "\n", + "In your organization, employee IDs are currently either four digits or five digits in length. In this task, you're given a four-digit numeric employee ID stored in a variable called `employee_id`. Convert this to a string format and store the result in the same variable. Later, you'll update this employee ID string so that it complies with a new standardized format.\n", + "\n", + "Complete the following code. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "id": "G2SllNnuzI8T" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], + "source": [ + "# Assign `employee_id` to a four digit number as an initial value\n", + "\n", + "employee_id = 4186\n", + "\n", + "# Display the data type of `employee_id`\n", + "\n", + "print(type(employee_id))\n", + "\n", + "# Reassign `employee_id` to the same value but in the form of a string\n", + "\n", + "employee_id = str(employee_id)\n", + "\n", + "# Display the data type of `employee_id` now\n", + "\n", + "print(type(employee_id))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "572a0c93-025b-4787-8fee-f17761fb8137" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `str()` function in Python to convert the initial value of the `employee_id` variable into a string. \n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AE8E5fhC05im" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Pass `employee_id` into the `str()` function.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hydEiSfizxnj" + }, + "source": [ + "#### **Question 1**\n", + "**What do you observe about the data type of `employee_id` the first time it's displayed? What do you observe about the data type of `employee_id` the second time it's displayed (after the variable is reassigned)?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "znZOdxL5i-7-" + }, + "source": [ + "This is similar to adding the type(argument_to_find_type) like previously. But for this case, the first time the value is printed, the data type of employee_id is integer and in the second time second time, after the variable is reassigned and printed, the data type of employee_id is string.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jx7y7YTk1Up1" + }, + "source": [ + "## Task 2\n", + "\n", + "Imagine that you have just been informed of a new criteria for employee IDs. They must all be five digits long for standardization purposes.\n", + "\n", + "In this task, you will write a conditional statement that displays a message if the length of the employee ID is less than five digits. \n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "XMiKlqT-4Oxr" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This employee ID has less than five digits. It does not meet length requirements.\n" + ] + } + ], + "source": [ + "# Assign `employee_id` to a four digit number as an initial value\n", + "\n", + "employee_id = 4186\n", + "\n", + "# Reassign `employee_id` to the same value but in the form of a string\n", + "\n", + "employee_id = str(employee_id)\n", + "\n", + "# Conditional statement that displays a message if the length of `employee_id` is less than five digits\n", + "\n", + "if len(employee_id) < 5:\n", + " print(\"This employee ID has less than five digits. It does not meet length requirements.\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "51Le_bn31Up2" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The `len()` function in Python can be used to get the length of `employee_id`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UMgDUngT1Up2" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Start the conditional statement with the `if` keyword.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xMHf8n4BUv94" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "To write the condition in the conditional statement, use the `<` comparison operator to check whether the length of `employee_id` is less than `5`. Make sure to place the condition between the `if` and the `:`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kSAu5xoIQsT3" + }, + "source": [ + "## Task 3\n", + "\n", + "In this task, you'll build upon the previous code. If an employee ID is only four digits, you'll use concatenation to create a five-digit employee ID number.\n", + "\n", + "Concatenation is a process that allows you to merge strings together. The addition operator (`+`) in Python allows you to concatenate two strings.\n", + "\n", + "Write an `if` statement that evaluates whether the length of `employee_id` is less than `5`. When the condition evaluates to `True`, reassign `employee_id` by concatenating `\"E\"` in front of the four-digit employee ID to create a five character employee ID. Then, display `employee_id` again. Be sure to replace each `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "id": "Io77ostT6xWS" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4186\n", + "E4186\n" + ] + } + ], + "source": [ + "# Assign `employee_id` to a four digit number as an initial value\n", + "\n", + "employee_id = 4186\n", + "\n", + "# Reassign `employee_id` to the same value but in the form of a string\n", + "\n", + "employee_id = str(employee_id)\n", + "\n", + "# Display the `employee_id` as it currently stands\n", + "\n", + "print(employee_id)\n", + "\n", + "# Conditional statement that updates the `employee_id` if its length is less than 5 digits\n", + "\n", + "if len(employee_id) < 5:\n", + " employee_id = \"E\" + employee_id\n", + "\n", + "# Display the `employee_id` after the update\n", + "\n", + "print(employee_id)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6wcuH-6dUv95" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To complete the header of the conditional statement, use the `if` keyword to start, the `len()` function to get the length of `employee_id`, and the `<` comparison operator to check whether the length is less than `5`. Make sure to write this before the `:`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "w_EoTGoX_pfj" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `=` assignment operator to update the value of the `employee_id` variable. Update the value of `employee_id` to the concatenation of `\"E\"` with the variable's current value. The `\"E\"` should appear to the left of the current value.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rXP7GNPuBMmG" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `print()` function to display `employee_id` after the update.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "I5Kn8z-kvuBr" + }, + "source": [ + "## Task 4\n", + "\n", + "Now you'll move on to the next part of your task. Imagine that the characters in a device ID convey technical information about the device. You'll need to extract characters in specific positions from the device ID. Start off by extracting the fourth character. \n", + "\n", + "The variable `device_id` represents a device ID containing alphanumeric characters; it's already stored as a string. \n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "L8L4YxUR_vfe" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "# Assign `device_id` to a string that contains alphanumeric characters\n", + "\n", + "device_id = \"r262c36\"\n", + "\n", + "# Extract the fourth character in `device_id` and display it\n", + "\n", + "print(device_id[3])\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2nXu8LejEqrd" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use a pair of square brackets, passing in the appropriate index value, in order to extract the fourth character in `device_id`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HzdBK9IJEqrd" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "In Python, index values start at `0`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FVGnk7-jBpM1" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Given that index values start at `0` in Python, an index value of `3` corresponds to the fourth character in a sequence.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xqO7XbHeFpig" + }, + "source": [ + "## Task 5\n", + "Now you will also need to extract the first through the third characters in the device ID. So take a slice of the device ID. You can achieve this using bracket notation in Python. Then, display the slice to examine the result.\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "qZ-1BzTiBRrC" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "r26\n" + ] + } + ], + "source": [ + "# Assign `device_id` to a string that contains alphanumeric characters\n", + "\n", + "device_id = \"r262c36\"\n", + "\n", + "# Extract the first through the third characters in `device_id` and display the result\n", + "\n", + "print(device_id[0:3])\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "F4kCryYxKIwn" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use a pair of square brackets, passing in the appropriate index values, in order to extract the first through the third characters in `device_id`.\n", + "\n", + "Inside the square brackets, use a `:` to separate the first index value (the starting index value) and second index value (ending index value).\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3gk28FHRKIwn" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Keep in mind that the second index value passed into bracket notation is exclusive. In other words, the index value passed into the square brackets after the `:` is not included when the string is sliced. The resulting slice will not include the character at that index. \n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MXE3aF3HKkVW" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Recall that the second index value passed into bracket notation is exclusive and indexing in Python starts at `0`. The first index value should be `0` and the second index value should be `3`, in order to extract the first through the third characters in `device_id`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ofuRLadDKra6" + }, + "source": [ + "## Task 6\n", + "You'll now proceed to the last part of your task. This involves extracting components of a URL.\n", + "\n", + "You'll work with string indices to display various components of a URL that's stored in the URL variable. First, you'll extract and display the protocol of the URL and the `://` characters that follow it using string slicing. Consider that the protocol is in the secure format of `https` when determining the indices for your slice.\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "HkSYpFWPGbw0" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "https://\n" + ] + } + ], + "source": [ + "# Assign `url` to a specific URL\n", + "\n", + "url = \"https://exampleURL1.com\"\n", + "\n", + "# Extract the protocol of `url` along with the syntax following it, display the result\n", + "\n", + "print(url[0:8])\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YYS-NEsTMbMT" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Note that `https://` is eight characters long.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ahtv7SvEMbMT" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use a pair of square brackets to slice the string stored in `url`, passing in two index values separated by `:`. Keep in mind that the second index value is exclusive and that indexing in Python starts at `0`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_dq4tZ_kMbMT" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `print()` function to display the slice.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ESKyCxglOUy7" + }, + "source": [ + "## Task 7\n", + "\n", + "Later in this lab, you'll extract the domain extension. To prepare for this, use the `.index()` method to identify the index where the domain extension `.com` is located in the given URL.\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "ryYFvPg3Li2P" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "19\n" + ] + } + ], + "source": [ + "# Assign `url` to a specific URL\n", + "\n", + "url = \"https://exampleURL1.com\"\n", + "\n", + "# Display the index where the domain extension \".com\" is located in `url`\n", + "\n", + "print(url.index(\".com\"))\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QdvhH8Co-CCg" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Apply the `.index()` method to `url` in order to get the appropriate index. The `.index()` method takes in a substring, and if that substring is located in the original string, it returns the index where that substring starts to occur in the original string.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Uhsxwf_S-CCg" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Call `url.index()`, and inside the parentheses, pass in the targeted domain extension as a string.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QE2QVrWw9xDF" + }, + "source": [ + "## Task 8\n", + "It's a good idea to save important data in variables when programming. This allows for quick and easy tracking and reuse of information. \n", + "\n", + "Store the output of the `.index()` method in a variable called `ind`, which is short for index. This index represents the position where the domain extension `\".com\"` starts in the `url`. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell. Note that running this cell will not produce an output." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "WAJvwu4UT0HC" + }, + "outputs": [], + "source": [ + "# Assign `url` to a specific URL\n", + "\n", + "url = \"https://exampleURL1.com\"\n", + "\n", + "# Assign `ind` to the output of applying `.index()` to `url` in order to extract the starting index of \".com\" in `url` \n", + "\n", + "ind = url.index(\".com\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RvzA00XL-GVQ" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "To assign the extracted index to the `ind` variable, use the `.index()` method to the right of the `=` assignment operator. \n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "X9R0JgY3-GVQ" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Call `url.index()`, and inside the parantheses, pass in the targeted domain extension as a string. Be sure to place this to the right of the `=` assignment operator, so that the result is assigned to `ind`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "x3alBA-B_0CX" + }, + "source": [ + "## Task 9\n", + "You can use string slicing to also extract the domain extension of a URL. To do so, you can create a slice. The starting index should be the `ind` variable. This contains the index where the domain extension begins. The ending index should be `ind + 4` (since `\".com\"` is four characters long). Sometimes, like in this situation, it's easier to express the ending index in relation to the starting index. Examine the following code, run it as is, and observe the output." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "B05TSVrLMJDU" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + ".com\n" + ] + } + ], + "source": [ + "# Assign `url` to a specific URL\n", + "\n", + "url = \"https://exampleURL1.com\"\n", + "\n", + "# Assign `ind` to the output of applying `.index()` to `url` in order to extract the starting index of \".com\" in `url` \n", + "\n", + "ind = url.index(\".com\")\n", + "\n", + "# Extract the domain extension in `url` and display it\n", + "\n", + "print(url[ind:ind+4])\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KdB13f4x3JzH" + }, + "source": [ + "#### **Question 2**\n", + "**What does this code output and why?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iLYsbTFs3dbI" + }, + "source": [ + "After inputing the code url[idnd:ind+4], this tells the machine which ones to print which is the last parts of the ends of \".com\".. Once it prints out, it shows the domain name .com by first saving the starting position of the domain name in the ind variable and then extracting 4 consecutive characters from url starting from the saved position." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yl43AcPzBc_7" + }, + "source": [ + "## Task 10\n", + "Finally, extract the website name from the given URL using string slicing and the `ind` variable that you defined earlier. In the given URL, the website name is `\"exampleURL1\"`. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "id": "o0ZD8WAeQzPE" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "exampleURL1\n" + ] + } + ], + "source": [ + "# Assign `url` to a specific URL\n", + "\n", + "url = \"https://exampleURL1.com\"\n", + "\n", + "# Assign `ind` to the output of applying `.index()` to `url` in order to extract the starting index of \".com\" in `url` \n", + "\n", + "ind = url.index(\".com\")\n", + "\n", + "# Extract the website name in `url` and display it\n", + "\n", + "print(url[8:ind])\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xsVXZafGVfVw" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "In order to extract the website name in the given URL, use a pair of square brackets to create a slice of `url`, passing in a start index and an ending index, separating the two with `:`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ml549eeHWHmu" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "The starting index should be set to `8`, since this is the position after the protocol and the `://` syntax ends and where the website name begins. The ending index should be set to the position where the `.com` domain name begins.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BmvPYtFqEMc1" + }, + "source": [ + "## Conclusion\n", + "**What are your key takeaways from this lab?**\n", + "\n", + "\n", + "I believe strings are essential for storing important data, particularly in security-related contexts like device IDs and URLs. String manipulation techniques, such as concatenation and slicing, are powerful tools for combining and extracting specific parts of a string. In Python, several built-in functions and methods make working with strings easier. For example, the `type()` function reveals the data type of an object, while `str()` converts other data types into strings. The `len()` function helps determine the number of characters in a string, and `.index()` locates the first occurrence of a substring within a string. These tools are vital for analysts dealing with string values or converting data to string format.\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [ + { + "file_id": "1E8fUdfFmtwFILyLWHVCUDkwcYAwQ9BZf", + "timestamp": 1666706453836 + } + ] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Activity_Work with strings in Python.pdf b/Activity_Work with strings in Python.pdf new file mode 100644 index 0000000..e2a279b Binary files /dev/null and b/Activity_Work with strings in Python.pdf differ diff --git a/Algorithm-for-file-updates-in-Python-MY-RESPONSES.docx b/Algorithm-for-file-updates-in-Python-MY-RESPONSES.docx new file mode 100644 index 0000000..fb0ca25 Binary files /dev/null and b/Algorithm-for-file-updates-in-Python-MY-RESPONSES.docx differ diff --git a/Algorithm-for-file-updates-in-Python-MY-RESPONSES.pdf b/Algorithm-for-file-updates-in-Python-MY-RESPONSES.pdf new file mode 100644 index 0000000..22c991d Binary files /dev/null and b/Algorithm-for-file-updates-in-Python-MY-RESPONSES.pdf differ diff --git a/Course-7-glossary.docx b/Course-7-glossary.docx new file mode 100644 index 0000000..93d27e5 Binary files /dev/null and b/Course-7-glossary.docx differ diff --git a/Exemplar_Create loops.ipynb b/Exemplar_Create loops.ipynb new file mode 100644 index 0000000..c964ec5 --- /dev/null +++ b/Exemplar_Create loops.ipynb @@ -0,0 +1,877 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "5e2a85c3-4d7c-43c0-b9dd-ef7af16eb73d" + }, + "source": [ + "# Exemplar: Create loops" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "07edb7a1-c2cb-49cb-94e6-886268e48715" + }, + "source": [ + "## Introduction\n", + "\n", + "As a security analyst, some of the measures you take to protect a system will involve repetition. As an example, you might need to investigate multiple IP addresses that have attempted to connect to the network. In Python, iterative statements can help automate repetitive processes like these to make them more efficient.\n", + "\n", + "In this lab, you will practice writing iterative statements in Python." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bM8zStFIWIkI" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace that with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task. \n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook. \n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook. \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "35dbd645-a9b3-4f79-a239-b9fb3e1325d2" + }, + "source": [ + "## Scenario\n", + "\n", + "You're working as a security analyst, and you're writing programs in Python to automate displaying messages regarding network connection attempts, detecting IP addresses that are attempting to access restricted data, and generating employee ID numbers for a Sales department." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MnfWH6cesPdt" + }, + "source": [ + "## Task 1\n", + "\n", + "In this task, you'll create a loop related to connecting to a network. \n", + "\n", + "Write an iterative statement that displays `Connection could not be established` three times. Use the `for` keyword, the `range()` function, and a loop variable of `i`. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell. " + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "77ZfXt6DzhFr" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connection could not be established.\n", + "Connection could not be established.\n", + "Connection could not be established.\n" + ] + } + ], + "source": [ + "# Iterative statement using `for`, `range()`, and a loop variable of `i`\n", + "# Display \"Connection could not be established.\" three times\n", + "\n", + "for i in range(3):\n", + " print(\"Connection could not be established.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "572a0c93-025b-4787-8fee-f17761fb8137" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use `i` as the loop variable and then place the `in` operator after `i`. \n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AE8E5fhC05im" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "After the `in` operator, pass in the appropriate number to the `range()` function so that it instructs Python to repeat the specified action three times. \n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jx7y7YTk1Up1" + }, + "source": [ + "## Task 2\n", + "\n", + "The `range()` function can also take in a variable. To repeat a specified action a certain number of times, you can first assign an integer value to a variable. Then, you can pass that variable into the `range()` function within a `for` loop.\n", + "\n", + "In your code that displays a network message connection, incorporate a variable called `connection_attempts`. Assign the positive integer of your choice as the value of that variable and fill in the missing variable in the iterative statement. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell. Test out the code with different values for `connection_attempts` and observe what happens. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "id": "g1lA6JfZ1Up1" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connection could not be established\n", + "Connection could not be established\n", + "Connection could not be established\n" + ] + } + ], + "source": [ + "# Create a variable called `connection_attempts` that stores the number of times the user has tried to connect to the network\n", + "\n", + "connection_attempts = 3\n", + "\n", + "# Iterative statement using `for`, `range()`, a loop variable of `i`, and `connection_attempts`\n", + "# Display \"Connection could not be established.\" as many times as specified by `connection_attempts`\n", + "\n", + "for i in range(connection_attempts):\n", + " print(\"Connection could not be established\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "51Le_bn31Up2" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Assign the `connection_attempts` variable to a number that represents how many times the user will try to connect to the network.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UMgDUngT1Up2" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Pass in the appropriate variable to the `range()` function so that it instructs Python to repeat the specified action the specified number of times. \n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4Pm9VgYyfa8e" + }, + "source": [ + "## Task 3\n", + "\n", + "This task can also be achieved with a `while` loop. Complete the `while` loop with the correct code to instruct it to display `\"Connection could not be established.\"` three times.\n", + "\n", + "In this task, a `for` loop and a `while` loop will produce similar results, but each is based on a different approach. (In other words, the underlying logic is different in each.) A `for` loop terminates after a certain number of iterations have completed, whereas a `while` loop terminates once it reaches a certain condition. In situations where you do not know how many times the specified action should be repeated, `while` loops are most appropriate. \n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "rEpwg56I-XYd" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connection could not be established\n", + "Connection could not be established\n", + "Connection could not be established\n" + ] + } + ], + "source": [ + "# Assign `connection_attempts` to an initial value of 0, to keep track of how many times the user has tried to connect to the network\n", + "\n", + "connection_attempts = 0\n", + "\n", + "# Iterative statement using `while` and `connection_attempts`\n", + "# Display \"Connection could not be established.\" every iteration, until connection_attempts reaches a specified number\n", + "\n", + "while connection_attempts < 3:\n", + " print(\"Connection could not be established\")\n", + "\n", + " # Update `connection_attempts` (increment it by 1 at the end of each iteration) \n", + " connection_attempts = connection_attempts + 1\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hbhf94MR7FiX" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "In the condition, use a comparison operator to check whether `connection_attempts` has reached a specific number. This number represents the number of times the message will be displayed.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JvJ-vJ6T7FiY" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "In the condition, use the `<` comparison operator to check whether `connection_attempts` is less than a specific number. This number represents the number of times the message will be displayed. \n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rXP7GNPuBMmG" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `print()` function to display the appropriate message to the user.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eDkg_mydxEQn" + }, + "source": [ + "#### **Question 1**\n", + "**What do you observe about the differences between the `for` loop and the `while` loop that you wrote?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yKyHyhzwxkvJ" + }, + "source": [ + "The messages outputted from both loops were identical. The logic is what differed between the two loops. In the `for` loop, the loop variable `i` was automatically defined in the loop header, and it was updated automatically in each iteration. In the `while` loop, the loop variable `connection_attempts` had to be defined before the loop header, and it had to be explicitly updated inside the loop body." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0GP03CYhBVbt" + }, + "source": [ + "## Task 4\n", + "\n", + "Now, you'll move onto your next task. You'll automate checking whether IP addresses are part of an allow list. You will start with a list of IP addresses from which users have tried to log in, stored in a variable called `ip_addresses`. Write a `for` loop that displays the elements of this list one at a time. Use `i` as the loop variable in the `for` loop.\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "id": "r-qJI6RSD_h-" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "192.168.142.245\n", + "192.168.109.50\n", + "192.168.86.232\n", + "192.168.131.147\n", + "192.168.205.12\n", + "192.168.200.48\n" + ] + } + ], + "source": [ + "# Assign `ip_addresses` to a list of IP addresses from which users have tried to log in\n", + "\n", + "ip_addresses = [\"192.168.142.245\", \"192.168.109.50\", \"192.168.86.232\", \"192.168.131.147\",\n", + " \"192.168.205.12\", \"192.168.200.48\"]\n", + "\n", + "# For loop that displays the elements of `ip_addresses` one at a time\n", + "\n", + "for i in ip_addresses:\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-ATXXuRC9wkF" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use `i` as the loop variable and the `in` operator to convey that the specified action should repeat for each element that's in the list `ip_addresses`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2nXu8LejEqrd" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "To display the loop variable in every iteration, use the `print()` function inside the `for` loop.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xqO7XbHeFpig" + }, + "source": [ + "## Task 5\n", + "You are now given a list of IP addresses that are allowed to log in, stored in a variable called `allow_list`. Write an `if` statement inside of the `for` loop. For each IP address in the list of IP addresses from which users have tried to log in, display `\"IP address is allowed\"` if it is among the allowed addresses and display `\"IP address is not allowed\"` otherwise.\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "f_oetI5DGIik" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "IP address is not allowed\n", + "IP address is not allowed\n", + "IP address is allowed\n", + "IP address is not allowed\n", + "IP address is allowed\n", + "IP address is not allowed\n" + ] + } + ], + "source": [ + "# Assign `allow_list` to a list of IP addresses that are allowed to log in\n", + "\n", + "allow_list = [\"192.168.243.140\", \"192.168.205.12\", \"192.168.151.162\", \"192.168.178.71\", \n", + " \"192.168.86.232\", \"192.168.3.24\", \"192.168.170.243\", \"192.168.119.173\"]\n", + "\n", + "# Assign `ip_addresses` to a list of IP addresses from which users have tried to log in\n", + "\n", + "ip_addresses = [\"192.168.142.245\", \"192.168.109.50\", \"192.168.86.232\", \"192.168.131.147\",\n", + " \"192.168.205.12\", \"192.168.200.48\"]\n", + "\n", + "# For each IP address in the list of IP addresses from which users have tried to log in, \n", + "# If it is among the allowed addresses, then display “IP address is allowed”\n", + "# Otherwise, display “IP address is not allowed”\n", + "\n", + "for i in ip_addresses:\n", + "\tif i in allow_list:\n", + "\t\tprint(\"IP address is allowed\")\n", + "\telse:\n", + "\t\tprint(\"IP address is not allowed\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rRXw1sVU-ry2" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use `i` as the loop variable and the `in` operator to convey that the specified action should repeat for each element that's in the list `ip_addresses`.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3gk28FHRKIwn" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Make sure that the `if` statement checks whether the user's IP address is in the list of allowed IP addresses.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NHQF_o5a-7tz" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `print()` function to display the messages.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L6T2_9aDJ9A9" + }, + "source": [ + "## Task 6\n", + "Imagine now that the information the users are trying to access is restricted, and if an IP address outside the list of allowed IP addresses attempts access, the loop should terminate because further investigation would be needed to assess whether this activity poses a threat. To achieve this, use the `break` keyword and expand the message that is displayed to the user when their IP address is not in `allow_list` to provide more specifics. Instead of `\"IP address is not allowed\"`, display `\"IP address is not allowed. Further investigation of login activity required\"`.\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "id": "KN8_OTQtLtPk" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "IP address is not allowed. Further investigation of login activity required\n" + ] + } + ], + "source": [ + "# Assign `allow_list` to a list of IP addresses that are allowed to log in\n", + "\n", + "allow_list = [\"192.168.243.140\", \"192.168.205.12\", \"192.168.151.162\", \"192.168.178.71\", \n", + " \"192.168.86.232\", \"192.168.3.24\", \"192.168.170.243\", \"192.168.119.173\"]\n", + "\n", + "# Assign `ip_addresses` to a list of IP addresses from which users have tried to log in\n", + "\n", + "ip_addresses = [\"192.168.142.245\", \"192.168.109.50\", \"192.168.86.232\", \"192.168.131.147\",\n", + " \"192.168.205.12\", \"192.168.200.48\"]\n", + "\n", + "# For each IP address in the list of IP addresses from which users have tried to log in, \n", + "# If it is among the allowed addresses, then display “IP address is allowed”\n", + "# Otherwise, display “IP address is not allowed”\n", + " \n", + "for i in ip_addresses:\n", + "\tif i in allow_list:\n", + "\t\tprint(\"IP address is allowed\")\n", + "\telse:\n", + "\t\tprint(\"IP address is not allowed. Further investigation of login activity required\")\n", + "\t\tbreak" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dMuVBrXD_0td" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use `i` as the loop variable and the `in` operator to convey that the specified action should repeat for each element that's in the list `ip_addresses`.\n", + "\n", + "Make sure that the `if` statement checks whether the user's IP address is in the list of allowed IP addresses.\n", + "\n", + "Use the `break` keyword to terminate the loop at the appropriate time.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Vm0zh9cv_0td" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `break` keyword inside the `else` statement after the appropriate message is displayed.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zaTeUWPC_0td" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `print()` function to display the messages.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ESKyCxglOUy7" + }, + "source": [ + "## Task 7\n", + "\n", + "You'll now complete another task. This involves automating the creation of new employee IDs.\n", + "\n", + "You have been asked to create employee IDs for a Sales department, with the criteria that the employee IDs should all be numbers that are unique, divisble by 5, and falling between 5000 and 5150. The employee IDs can include both 5000 and 5150.\n", + "\n", + "Write a `while` loop that generates unique employee IDs for the Sales department by iterating through numbers and displays each ID created. \n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "id": "LAmLF0XTQDZT" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5000\n", + "5005\n", + "5010\n", + "5015\n", + "5020\n", + "5025\n", + "5030\n", + "5035\n", + "5040\n", + "5045\n", + "5050\n", + "5055\n", + "5060\n", + "5065\n", + "5070\n", + "5075\n", + "5080\n", + "5085\n", + "5090\n", + "5095\n", + "5100\n", + "5105\n", + "5110\n", + "5115\n", + "5120\n", + "5125\n", + "5130\n", + "5135\n", + "5140\n", + "5145\n", + "5150\n" + ] + } + ], + "source": [ + "# Assign the loop variable `i` to an initial value of 5000\n", + "\n", + "i = 5000\n", + "\n", + "# While loop that generates unique employee IDs for the Sales department by iterating through numbers\n", + "# and displays each ID created\n", + "\n", + "while i <= 5150: \n", + " print(i)\n", + " i = i + 5" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YZM2OmFQQCYB" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use a comparison operator to check whether `i` has reached the upper bound (which is the highest employee ID number allowed). Remember that the employee IDs need to fall between 5000 and 5150. \n", + "\n", + "Make sure to update the value of the loop variable `i` at the end of the loop.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zr0bEXiZQCYJ" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `<=` comparison operator to check whether `i` has reached the upper bound, since the employee IDs need to fall between 5000 and 5150. \n", + "\n", + "At the end of the loop, increment the loop variable by 5. This is because the employee IDs need to be divisble by 5 and the first employee ID is set to 5000.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c3amadigQCYJ" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `<=` comparison operator to check whether `i` has reached 5150, since the employee IDs need to fall between 5000 and 5150. \n", + "\n", + "Use the `print()` function to display the loop variable `i` in each iteration.\n", + "\n", + "Use the `=` assignment operator and the `+` addition operator to increment the value of the loop variable at the end of each iteration.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9ghtXGTaGOoi" + }, + "source": [ + "## Task 8\n", + "You would like to incorporate a message that displays `Only 10 valid employee ids remaining` as a helpful alert once the loop variable reaches `5100`. \n", + "\n", + "To do so, include an `if` statement in your code.\n", + "\n", + "Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "id": "WAJvwu4UT0HC" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5000\n", + "5005\n", + "5010\n", + "5015\n", + "5020\n", + "5025\n", + "5030\n", + "5035\n", + "5040\n", + "5045\n", + "5050\n", + "5055\n", + "5060\n", + "5065\n", + "5070\n", + "5075\n", + "5080\n", + "5085\n", + "5090\n", + "5095\n", + "5100\n", + "Only 10 valid employee ids remaining\n", + "5105\n", + "5110\n", + "5115\n", + "5120\n", + "5125\n", + "5130\n", + "5135\n", + "5140\n", + "5145\n", + "5150\n" + ] + } + ], + "source": [ + "# Assign the loop variable `i` to an initial value of 5000\n", + "\n", + "i = 5000\n", + "\n", + "# While loop that generates unique employee IDs for the Sales department by iterating through numbers\n", + "# and displays each ID created\n", + "# This loop displays \"Only 10 valid employee ids remaining\" once `i` reaches 5100\n", + "\n", + "while i <= 5150: \n", + " print(i)\n", + " if i == 5100:\n", + " print(\"Only 10 valid employee ids remaining\")\n", + " i = i + 5" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HPQ5EChaVJTJ" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use a comparison operator to check whether `i` has reached `5100`. \n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pMbgtu4IVJTK" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `==` comparison operator to check whether `i` has reached `5100`. \n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "G2AP7SOcVJTK" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `print()` function to display the message.\n", + " \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "goaW1X3mGUNt" + }, + "source": [ + "#### **Question 2**\n", + "**Why do you think the statement `print(i)` is written before the conditional rather than inside the conditional?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YV4hudrLGq5i" + }, + "source": [ + "In this code, I learned how that it must print out every employee ID number with a for loop, where variable `i` represents the ID number created in each iteration of the loop. The `print(i)` is written before the conditional, so that the loop is displayed in every iteration, otherwise, if `print(i)` was written inside the conditional, the loop variable would only be printed out when it's equal to 5100. This is beucase the condition in the `if` statement is `i == 5100` then the output is dmeosntrated of what I am tyring to explain..." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BmvPYtFqEMc1" + }, + "source": [ + "## Conclusion\n", + "**What are your key takeaways from this lab?**\n", + "* Iterative statements play a major role in automating security-related processes that need to be repeated. \n", + "* You can `for` loops allow you to repeat a process a specified number of times.\n", + "* You can use `while` loops allow you to repeat a process until a specified condition has been met. Comparison operators are often used in these conditions.\n", + " * The `<` comparison operator allows you to check whether one value is less than another.\n", + " * The `<=` comparison operator allows you to check whether one value is less than or equal to another.\n", + " * The `==` comparison operator allows you to check whether one value is equal to another.\n", + "\n", + "\n", + "In this lab, I learned the importance of using iterative statements, like `for` and `while` loops, to automate repetitive tasks in programming. These loops are especially helpful in security analysis, as they allow you to automate tasks like checking multiple network connection attempts or verifying IP addresses against a list of allowed addresses. The `for` loop is useful when you know the exact number of iterations, while the `while` loop is better suited for situations where the loop runs until a specific condition is met. The lab also highlighted the use of comparison operators in controlling the flow of these loops, such as `<`, `<=`, and `==`. Lastly, using `break` to exit a loop early, as well as incorporating useful messages (like alerting when only 10 employee IDs remain), helped me understand how to add more meaningful interactions within the loop logic. Overall, these concepts make it easier to handle repetitive tasks efficiently and apply logic to automate real-world scenarios.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [], + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Exemplar_Create loops.pdf b/Exemplar_Create loops.pdf new file mode 100644 index 0000000..160c1ac Binary files /dev/null and b/Exemplar_Create loops.pdf differ diff --git a/LAB_014_Activity.ipynb b/LAB_014_Activity.ipynb new file mode 100644 index 0000000..8e41b96 --- /dev/null +++ b/LAB_014_Activity.ipynb @@ -0,0 +1,798 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "wFcD0whLvLTP" + }, + "source": [ + "# Activity: Debug Python Code" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ivt8p77hCKcO" + }, + "source": [ + "## Introduction\n", + "\n", + "One of the biggest challenges faced by analysts is ensuring that automated processes run smoothly. Debugging is an important practice that security analysts incorporate in their work to identify errors in code and resolve them so that the code achieves the desired outcome. \n", + "\n", + "Through a series of tasks in this lab, you'll develop and apply your debugging skills in Python." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eW4pA4LsC-vb" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task. \n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook. \n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook. \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "A8q3M1L7isIj" + }, + "source": [ + "## Scenario\n", + "\n", + "In your work as a security analyst, you need to apply debugging strategies to ensure your code works properly.\n", + "\n", + "Throughout this lab, you'll work with code that is similar to what you've written before, but now it has some errors that need to be fixed. You'll need to read code cells, run them, identify the errors, and adjust the code to resolve the errors." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ytgkadfzRQE5" + }, + "source": [ + "## Task 1\n", + "\n", + "The following code cell contains a syntax error. In this task, you'll run the code, identify why the error is occuring, and modify the code to resolve it. (To ensure that it has been resolved, run the code again to check if it now functions properly.)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "id": "_Ts_iS8EWZvs" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connection cannot be established\n", + "Connection cannot be established\n", + "Connection cannot be established\n", + "Connection cannot be established\n", + "Connection cannot be established\n", + "Connection cannot be established\n", + "Connection cannot be established\n", + "Connection cannot be established\n", + "Connection cannot be established\n", + "Connection cannot be established\n" + ] + } + ], + "source": [ + "# For loop that iterates over a range of numbers\n", + "# and displays a message each iteration\n", + "\n", + "for i in range(10):\n", + " print(\"Connection cannot be established\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "N4iCk5h-BEW4" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "The header of a `for` loop in Python requires specific punctuation at the end. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SI4AQbNanqDm" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "The header of a `for` loop in Python requires a colon (`:`) at the end. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "haLsZKt-R2og" + }, + "source": [ + "#### **Question 1**\n", + "**What happens when you run the code before modifying it? How can you fix this?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9epEk8wkSB4x" + }, + "source": [ + "I believe when the code is run before it's modified, the output shows a \"SyntaxError: invalid syntax\" message. That typically means there's a mistake in the way the code is written. In this case, I think the error is happening because there's a missing colon (:) at the end of the for loop header. To fix it, I’d just add the colon in that spot, and it should work properly.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_OJCHm00RiSk" + }, + "source": [ + "## Task 2\n", + "In the following code cell, you're provided a list of usernames. There is an issue with the syntax. In this task, you'll run the cell, observe what happens, and modify the code to fix the issue." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "Juc9sShCQdt6" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['djames', 'jpark', 'tbailey', 'zdutchma', 'esmith', 'srobinso', 'dcoleman', 'fbautist']\n" + ] + } + ], + "source": [ + "# Assign `usernames_list` to a list of usernames\n", + "\n", + "usernames_list = [\"djames\", \"jpark\", \"tbailey\", \"zdutchma\", \"esmith\", \"srobinso\", \"dcoleman\", \"fbautist\"]\n", + "\n", + "# Display `usernames_list`\n", + "\n", + "print(usernames_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cjD2enJLQ_U9" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Each element in `usernames_list` is a username and should be a string. In Python, a string should have quotation marks around it.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zSA3MVwqQ_U-" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "When creating a list in Python, the elements of the list should be separated with commas. There should be a comma between every two consecutive elements. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "m0Qs82zqS2vv" + }, + "source": [ + "#### **Question 2**\n", + "**What happens when you run the code before modifying it? How can you fix it?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZohCbcZXS2vv" + }, + "source": [ + "I believe when the code is run before it’s modified, the output shows a \"SyntaxError: invalid syntax.\" It seems the issue comes from how the usernames are being assigned to the `usernames_list`. The fourth username is missing a closing quotation mark, and there's also no comma between the fourth and fifth usernames. Each username in the list needs to be a string, and commas are needed to separate each one. To fix the error, I’d add the missing quotation mark around the fourth username to make it a complete string, and then insert a comma between the fourth and fifth usernames. So instead of writing `\"zdutchma \"esmith\",`, it should look like `\"zdutchma\", \"esmith\",`.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "foJ_J8Cg5T7p" + }, + "source": [ + "## Task 3\n", + "In the following code cell, there is a syntax error. Your task is to run the cell, identify what is causing the error, and fix it." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "GMBNs-7BRvKl" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "UPDATE NEEDED\n" + ] + } + ], + "source": [ + "# Display a message in upper case \n", + "\n", + "print(\"update needed\".upper())" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XQjlLUPzVTDO" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Calling a function in Python requires both opening and closing parantheses. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LxfLdF94VTDO" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "In the code above, check that each function call has both opening and closing parantheses. \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "F3g_1WasUf3E" + }, + "source": [ + "#### **Question 3**\n", + "**What happens when you run the code before modifying it? What is causing the syntax error? How can you fix it?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cy9TlpIDUf3F" + }, + "source": [ + "I believe when the code is run before it’s modified, the output shows a \"SyntaxError: unexpected EOF while parsing.\" This error usually happens when something is left unfinished, and in this case, it’s likely because the closing parenthesis is missing at the end of the `print()` statement. To fix it, I’d just add the closing parenthesis `)` at the end of that line, and it should resolve the issue.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OJ7d7WFy5VL_" + }, + "source": [ + "## Task 4\n", + "In the following code cell, you're provided a `usernames_list`, a `username`, and code that determines whether the username is approved. There are two syntax errors and one exception. Your task is to find them and fix the code. A helpful debugging strategy is to focus on one error at a time and run the code after fixing each one." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "Vx5AdI-8XNCI" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The user is an approved user\n" + ] + } + ], + "source": [ + "# Assign `usernames_list` to a list of usernames that represent approved users\n", + "\n", + "usernames_list = [\"djames\", \"jpark\", \"tbailey\", \"zdutchma\", \"esmith\", \"srobinso\", \"dcoleman\", \"fbautist\"]\n", + "\n", + "# Assign `username` to a specific username \n", + "\n", + "username = \"esmith\"\n", + "\n", + "# For loop that iterates over the elements of `usernames_list` and determines whether each element corresponds to an approved user\n", + "\n", + "for name in usernames_list:\n", + "\n", + " # Check if `name` matches `username` \n", + " # If it does match, then display a message accordingly \n", + "\n", + " if name == username:\n", + " print(\"The user is an approved user\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qQhHGraDZgTs" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "In Python, the `=` assignment operator allows you to assign or reassign a variable to a value, and the `==` comparison operator allows you to compare one value to another (or the value of one variable to the value of another). \n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IpfqcfmSayyy" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Indentation is important in Python syntax. Check that the indentation inside the `for` loop and the indentation inside the `if` statement are correct.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VTsJ6HjQZgTk" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Check that each time a variable is used, it's spelled in the same way it was spelled when it was assigned.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nTj-r29dXWqS" + }, + "source": [ + "#### **Question 4**\n", + "**What happens when you run the code before modifying it? What is causing the errors? How can you fix it?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J6QWmFNDXWqT" + }, + "source": [ + "I believe when the code is run before it’s modified, the output shows a \"SyntaxError: invalid syntax,\" which is the first error Python encounters. There are actually three issues causing this error:\n", + "\n", + "1. In the `if` condition, the `=` assignment operator is used instead of the `==` comparison operator, which is incorrect. To fix this, I’d replace the `=` with `==`.\n", + "\n", + "2. Inside the `if` statement, there's missing indentation, which also leads to a syntax error. To fix this, I would add the proper indentation before the `print()` statement so it’s aligned correctly.\n", + "\n", + "3. Finally, in the `for` loop condition, the variable `usernames_list` is misspelled as `username_list`, which causes an exception. To fix this, I’d correct the typo by adding the missing “s” to the variable name.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "l302EqWo5XLF" + }, + "source": [ + "## Task 5\n", + "\n", + "In this task, you'll examine the following code and identify the type of error that occurs. Then, you'll adjust the code to fix the error." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "rHQMQuH7bVLg" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This username is the final one in the list.\n" + ] + } + ], + "source": [ + "# Assign `usernames_list` to a list of usernames\n", + "\n", + "usernames_list = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\"]\n", + "\n", + "# Assign `username` to a specific username\n", + "\n", + "username = \"eraab\"\n", + "\n", + "# Determine whether `username` is the final username in `usernames_list` \n", + "# If it is, then display a message accordingly \n", + "\n", + "if username == usernames_list[4]:\n", + " print(\"This username is the final one in the list.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BWZl1RNtcnfG" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Recall that indexing in Python starts at `0`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XE8_nGIacnfH" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Identify how many elements there are in the `usernames_list`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iA2e2JzfcnfH" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Since indexing in Python starts at `0` and the `usernames_list` contains `5` elements, identify which index value corresponds to the final element in `usernames_list`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vZ1oTR_JdM-P" + }, + "source": [ + "#### **Question 5**\n", + "**What happens when you run the code before modifying it? What type of error is this? How can you fix it?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "p8PmzReNdM-P" + }, + "source": [ + "I believe when the code is run before it’s modified, the output shows an \"IndexError: list index out of range.\" This means there’s an issue with the index value being used with the list, and it's an exception that occurs when trying to access an invalid index. It’s important to remember that in Python, indexing starts at 0, and the `usernames_list` has a length of 5. So, the valid index values range from 0 to 4, where 4 corresponds to the last element. An index of 5 is out of range for this list. To fix the error, I would replace the 5 with 4, which will point to the last element in the list and resolve the issue.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DrEsqF4udQqm" + }, + "source": [ + "## Task 6\n", + "In this task, you'll examine the following code. The code imports a text file into Python, reads its contents, and stores the contents as a list in a variable named `ip_addresses`. It then removes elements from `ip_addresses` if they are in `remove_list`. There are two errors in the code: first a syntax error and then an exception related to a string method. Your goal is to find these errors and fix them." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "Qm3VwuiolGOC" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ip_address', '192.168.25.60', '192.168.205.12', '192.168.6.9', '192.168.52.90', '192.168.90.124', '192.168.186.176', '192.168.133.188', '192.168.203.198', '192.168.218.219', '192.168.52.37', '192.168.156.224', '192.168.60.153', '192.168.69.116']\n" + ] + } + ], + "source": [ + "# Assign `import_file` to the name of the text file\n", + "\n", + "import_file = \"allow_list.txt\"\n", + "\n", + "# Assign `remove_list` to a list of IP addressess that are no longer allowed to access the network \n", + "\n", + "remove_list = [\"192.168.97.225\", \"192.168.158.170\", \"192.168.201.40\", \"192.168.58.57\"]\n", + "\n", + "# With statement that reads in the text file and stores its contents in `ip_addresses`\n", + "\n", + "with open(import_file, \"r\") as file:\n", + " ip_addresses = file.read()\n", + "\n", + "# Convert `ip_addresses` from a string to a list\n", + "\n", + "ip_addresses = ip_addresses.split()\n", + "\n", + "# For loop that iterates over the elements in `remove_list`,\n", + "# checks if each element is in `ip_addresses`,\n", + "# and removes each element that corresponds to an IP address that is no longer allowed\n", + "\n", + "for element in remove_list:\n", + " if element in ip_addresses:\n", + " ip_addresses.remove(element)\n", + "\n", + "# Display `ip_addresses` after the removal process\n", + "\n", + "print(ip_addresses)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GxO4n4bZqeDQ" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "A `with` statement in Python requires a colon (`:`) at the end of the header.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3JwaSafrqeDY" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "The `.split()` method in Python is used on strings to convert them to lists. To call the `.split()` method, place the string you want to split in front of the method call.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Us5ejcyxmVht" + }, + "source": [ + "#### **Question 6**\n", + "**What happens when you run the code before modifying it? What is causing the errors? How can you fix them?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rKrsejpdmVht" + }, + "source": [ + "I believe when the code is run before it’s modified, the output shows a \"SyntaxError: invalid syntax,\" which is the first error Python encounters. There are two issues in the code:\n", + "\n", + "1. There’s a syntax error because the header of the `with` statement is missing a colon (`:`) at the end. To fix this, I’d simply add the colon at the end of the `with` statement header.\n", + "\n", + "2. There’s also an exception related to the string method `.split()`. To use this method, you need to call it on a string variable, so the correct syntax should be the variable name, followed by a dot, and then `.split()`. In this case, to fix it, I’d replace `split.ip_addresses()` with `ip_addresses.split()`, where `ip_addresses` is the variable containing the string you want to split.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-mcSxgKsYZos" + }, + "source": [ + "## Task 7\n", + "In this final task, there are three operating systems: OS 1, OS 2, and OS 3. Each operating system needs a security patch by a specific date. The patch date for OS 1 is `\"March 1st\"`, the patch date for OS 2 is `\"April 1st\"`, and the patch date for OS 3 is `\"May 1st\"`. \n", + "\n", + "The following code stores one of these operating systems in a variable named `system`. Then, it uses conditionals to output the patch date for this operating system. \n", + "\n", + "However, this code has logic errors. Your goal is to assign the `system` variable to different values, run the code to examine the output, identify the error, and fix it." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "E58SBhIZCbuH" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Patch date: April 1st\n" + ] + } + ], + "source": [ + "# Assign `system` to a specific operating system as a string\n", + "\n", + "system = \"OS 2\"\n", + "\n", + "# Assign `patch_schedule` to a list of patch dates in order of operating system\n", + "\n", + "patch_schedule = [\"March 1st\", \"April 1st\", \"May 1st\"]\n", + "\n", + "# Conditional statement that checks which operating system is stored in `system` and displays a message showing the corresponding patch date \n", + "\n", + "if system == \"OS 1\":\n", + " print(\"Patch date:\", patch_schedule[0])\n", + "\n", + "elif system == \"OS 2\":\n", + " print(\"Patch date:\", patch_schedule[1])\n", + "\n", + "elif system == \"OS 3\":\n", + " print(\"Patch date:\", patch_schedule[2])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7so3r8ZOSHya" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Recall that indexing in Python starts at `0`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J4r13sqiSHyb" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Note that the patch dates in `patch_schedule` are in order of operating system. The first patch date in `patch_schedule` corresponds to OS 1, the second patch date in `patch_schedule` corresponds to OS 2, and so on.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GyXRvxx6SHyb" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Since indexing in Python starts at `0` and `patch_schedule` is in order of operating system from OS 1 to OS 3, the index value `0` corresponds to the patch date for OS 1, the index value `1` corresponds to the patch date for OS 2, and so on.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5Zr0jDNsSHyb" + }, + "source": [ + "#### **Question 7**\n", + "**What happens when you run the code before modifying it? What is causing the logic errors? How can you fix them?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-frPvt5ESHyb" + }, + "source": [ + "I believe when the code is run before it’s modified, the system variable is assigned to `\"OS 2\"`, but the output shows \"Patch date: March 1st,\" which isn’t the correct patch date for OS 2. Similarly, when the system is set to `\"OS 1\"`, the output shows \"Patch date: May 1st,\" but this isn’t the correct patch date for OS 1 either.\n", + "\n", + "These issues are caused by logic errors due to incorrect index values in the first and second `print()` statements. It’s important to remember that in Python, indexing starts at 0, and the `patch_schedule` list is ordered from OS 1 to OS 3. To fix these errors, I’d adjust the index values. For OS 1, I should use `patch_schedule[0]` to get the correct patch date, and for OS 2, I should use `patch_schedule[1]`. This will ensure the correct patch dates are printed for each operating system.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ru2ytGBLCh4q" + }, + "source": [ + "## Conclusion\n", + "\n", + "**What are your key takeaways from this lab?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ls2WEGqsiSAd" + }, + "source": [ + "I believe the key takeaways from this lab are centered around the importance of debugging and how to approach it effectively. Debugging is a critical practice for analysts to identify and fix errors in code, ensuring that it runs smoothly. Python executes code from top to bottom and stops at the first error it encounters, so if there are multiple issues in a code cell, the error message will usually point to the first one. Common types of errors in Python include syntax errors, logic errors, and exceptions. Syntax errors often involve small mistakes, like a missing colon (`:`) at the end of a `with` statement header or a missing comma between elements in a list. Logic errors, on the other hand, could arise from using incorrect indices when accessing list elements. Exceptions can happen when variable names are misspelled or when string methods are incorrectly called. A key strategy for debugging is running the code and checking if it produces the intended results. If the output is incorrect or if an error message appears, it’s important to examine the line(s) that could be causing the problem. Once the code is fixed, rerun it to make sure everything works as expected.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [ + { + "file_id": "1lZ-gaTtJDv2b8Sz6qrWl2bv0P36gvZj0", + "timestamp": 1667952726076 + } + ] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/LAB_014_Activity.pdf b/LAB_014_Activity.pdf new file mode 100644 index 0000000..726a72f Binary files /dev/null and b/LAB_014_Activity.pdf differ diff --git a/LAB_Activity_CreateAConditionalStatement.ipynb b/LAB_Activity_CreateAConditionalStatement.ipynb new file mode 100644 index 0000000..451bbc5 --- /dev/null +++ b/LAB_Activity_CreateAConditionalStatement.ipynb @@ -0,0 +1,944 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "f7c0d886" + }, + "source": [ + "# Activity: Create a conditional statement" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0c066c48" + }, + "source": [ + "## Introduction\n", + "\n", + "Conditional statements are a powerful structure that help in achieving automation when you need to make sure conditions are met before certain actions are executed. For example, security analysts can use conditional statements in Python to check if users are approved to access a device. \n", + "\n", + "In this lab, you will practice writing conditional statements in Python." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c5e84779" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task.\n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook.\n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook.\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cf55e108" + }, + "source": [ + "## Scenario\n", + "\n", + "You're working as a security analyst. First, you are responsible for checking whether a user's operating system requires an update. Then, you need to investigate login attempts to a specific device. You must determine if login attempts were made by users approved to access this device and if the login attempts occurred during organization hours." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dddf9066" + }, + "source": [ + "## Task 1\n", + "You are asked to help automate the process of checking whether a user's operating system requires an update. Imagine that a user's device can be running one of the following operating systems: OS 1, OS 2, or OS 3. While OS 2 is up-to-date, OS 1 and OS 3 are not. Your task is to check whether the user's system is up-to-date, and if it is, display a message accordingly. To do this, complete the conditional statement using the keyword `if`. Be sure to replace the `### YOUR CODE HERE ###` with your own code before you run the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "id": "ecf76d07" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "no update needed\n" + ] + } + ], + "source": [ + "# Assign a variable named `system` to a specific operating system, represented as a string\n", + "# This variable indicates which operating system is running\n", + "# Feel free to run this cell multiple times; each time try assigning `system` to different values (\"OS 1\", \"OS 2\", \"OS 3\") and observe the result\n", + "\n", + "system = \"OS 2\"\n", + "\n", + "# If OS 2 is running, then display a \"no update needed\" message\n", + "\n", + "if system == \"OS 2\":\n", + " print(\"no update needed\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "87404578" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use a comparison operator that will allow you to check whether the `system` is running `\"OS 2\"`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a84345d3" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `==` comparison operator to check whether the `system` is running `\"OS 2\"`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "64d3dfd5" + }, + "source": [ + "## Task 2\n", + "Now try assigning the `system` variable to different values (`\"OS 1\"`, `\"OS 2\"`, and `\"OS 3\"`), run the cell, and observe what happens. Keep the conditional statement as is. Be sure to replace the `### YOUR CODE HERE ###` with your own code." + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "id": "04268ef8" + }, + "outputs": [], + "source": [ + "# Assign `system` to a specific operating system\n", + "# This variable represents which operating system is running\n", + "# Feel free to run this cell multiple times; each time try assigning `system` to different values (\"OS 1\", \"OS 2\", \"OS 3\") and observe the result\n", + "\n", + "system = \"OS 1\"\n", + "\n", + "# If OS 2 is running, then display a \"no update needed\" message\n", + "\n", + "if system == \"OS 2\":\n", + " print(\"no update needed\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "91a9fa19" + }, + "source": [ + "#### **Question 1**\n", + "**What happens when OS 2 is running? What happens when OS 1 is running?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1f7b136c" + }, + "source": [ + "Since I cannot change the if statement, when system is set to \"OS 1\", the if statement will not print out as OS1 is not string OS 2. However, using the same if statement, when the system is set to \"OS 2\", the if statement will print out \"no update needed\" as the comparision boolean checks true.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5cb83fb8" + }, + "source": [ + "## Task 3\n", + "\n", + "Nothing is displayed when the `system` is not equal to `\"OS 2\"`. This is because the condition didn't evaluate to `True`.\n", + "\n", + "It would be beneficial if an alternative message is provided to them when updates are needed.\n", + "\n", + "In the following cell, add the appropriate keyword after the first conditional so that it will display a message that conveys that an update is needed when the `system` is not running OS 2. Be sure to replace each `### YOUR CODE HERE ###` with your own code.\n", + "\n", + "Then, set the value of the `system` variable to indicate that OS 2 is running and run the cell. After observing what happens, set the value of `system` to indicate either that OS 1 is running or that OS 3 is running and run the cell." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "id": "e837a2e5" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "update needed\n" + ] + } + ], + "source": [ + "# Assign `system` to a specific operating system\n", + "# This variable represents which operating system is running\n", + "\n", + "system = \"OS 3\"\n", + "\n", + "# If OS 2 is running, then display a \"no update needed\" message\n", + "# Otherwise, display a \"update needed\" message\n", + "\n", + "if system == \"OS 2\":\n", + " print(\"no update needed\")\n", + "else:\n", + " print(\"update needed\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a0990885" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `else` keyword.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "94d7173f" + }, + "source": [ + "#### **Question 2**\n", + "**In this setup what happens when OS 2 is running? And what happens when OS 2 is not running?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "35cebec3" + }, + "source": [ + "Since I cannot change the if statement, when system is set to \"OS 3\", the if statement will print out \"update needed\" in the else section as OS 3 is not string OS 2. However, using the same if statement, when the system is set to \"OS 2\", the if statement will print out \"no update needed\" as the comparision boolean checks true.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5dc33cf4" + }, + "source": [ + "## Task 4\n", + "\n", + "This setup is still not ideal. If the variable `system` contains a random string or integer, the conditional above would still display `update needed`.\n", + "\n", + "To improve the conditional, you will need to add the `elif` keyword. In the following cell, you will add two `elif` statements after the `if` statement, to create the final code. The first `elif` statement will display `update needed` if `system` is `\"OS 1\"`. The second `elif` statement will display the same message, if `system` is `\"OS 3\"`. Complete the second `elif` statement, and then run the cell with the variable `system` set to a different string each time. Observe what happens when each operating system is running. Also try assigning the `system` variable to some strings other than `\"OS 1\"`, `\"OS 2\"`, and `\"OS 3\"` (for example `\"OS 4\"`).\n", + "\n", + "Be sure to replace each `### YOUR CODE HERE ###` with your own code." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "id": "fdd233bd" + }, + "outputs": [], + "source": [ + "# Assign `system` to a specific operating system\n", + "# This variable represents which operating system is running\n", + "\n", + "system = \"OS 4\"\n", + "\n", + "# If OS 2 is running, then display a \"no update needed\" message\n", + "# Otherwise if OS 1 is running, display a \"update needed\" message\n", + "# Otherwise if OS 3 is running, display a \"update needed\" message\n", + "\n", + "if system == \"OS 2\":\n", + " print(\"no update needed\")\n", + "elif system == \"OS 1\":\n", + " print(\"update needed\")\n", + "elif system == \"OS 3\":\n", + " print(\"update needed\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ffe34587" + }, + "source": [ + "#### **Question 3**\n", + "**Under this setup what happens when OS 2 is running? What happens when OS 1 is running? What happens when OS 3 is running? What happens when neither of those three operating systems are running?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8f2a5fa7" + }, + "source": [ + "If the system has OS 2 running, it will print out no updated needed. If the system has OS 1 running, it will print update needed. And if the system has OS 3 running, it will print out update needed. If the system has OS 4 running it will print nothing as it does not meet the requirements of either an OS 1, OS 2, or an OS 3.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "406daa6d" + }, + "source": [ + "## Task 5\n", + "Writing code that is readable and concise is a best practice in programming.\n", + "\n", + "The conditional above can be written more concisely.\n", + "\n", + "In the following cell, use a logical operator to combine the two `elif` statements from the previous setup into one `elif` statement. Be sure to replace each `### YOUR CODE HERE ###`. Then, assign the `system` variable to a value and run the cell. Like you did in the previous task, use `\"OS 1\"`, `\"OS 2\"`, `\"OS 3\"`, and other strings." + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "id": "edcd0fe6" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "update needed\n" + ] + } + ], + "source": [ + "# Assign `system` to a specific operating system\n", + "# This variable represents which operating system is running\n", + "\n", + "system = \"OS 4\"\n", + "\n", + "# If OS 2 is running, then display a \"no update needed\" message\n", + "# Otherwise if either OS 1 or OS 3 is running, display a \"update needed\" message\n", + "\n", + "if system == \"OS 2\":\n", + " print(\"no update needed\")\n", + "elif system == \"OS 1\" or \"OS 3\":\n", + " print(\"update needed\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "26dfef63" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `or` logical operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b358319f" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `or` operator between two conditions that each use the `==` operator.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bf8d3259" + }, + "source": [ + "#### **Question 4**\n", + "**What do you observe about this conditional?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b5ee2b89" + }, + "source": [ + "elif system == \"OS 1\" or \"OS 3\":\n", + " print(\"update needed\")\n", + " \n", + "This conditional is the or operator that tells the system that if the system is running on OS 1 or OS 3 then it will print out that an updated is needed. And if the system is running OS 2 it will print out that there is no update needed. If it has OS 4 then it will print update needed due to the invisible else after the elif from if using the same print() as elif.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8f119729" + }, + "source": [ + "## Task 6\n", + "Now you'll move on to the next part of your work. You've been asked to investigate login attempts to a specific device. Only approved users should log on to this device.\n", + "\n", + "You'll start with two authorized users, stored in the variables `approved_user1` and `approved_user2`. You'll need to write a conditional statement that compares those variables to a third variable, `username`. This will be the username of a specific user trying to log in. Be sure to replace each `### YOUR CODE HERE ###` with your own code." + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "id": "fe43a1fc" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This user has access to this device.\n" + ] + } + ], + "source": [ + "# Assign `approved_user1` and `approved_user2` to usernames of approved users\n", + "\n", + "approved_user1 = \"elarson\"\n", + "approved_user2 = \"bmoreno\"\n", + "\n", + "# Assign `username` to the username of a specific user trying to log in\n", + "\n", + "username = \"bmoreno\"\n", + "\n", + "# If the user trying to log in is among the approved users, then display a message that they are approved to access this device\n", + "# Otherwise, display a message that they do not have access to this device\n", + "\n", + "if username == approved_user1 or approved_user2:\n", + " print(\"This user has access to this device.\")\n", + "else:\n", + " print(\"This user does not have access to this device.\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5a669a55" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `if` keyword in the first conditional statement and the `else` keyword in the second conditional statement. Make sure both statements end with the proper syntax of a colon (`:`).\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b43c14e3" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use a comparison operator that will allow you to check whether the user trying to log in is an approved user.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c9f97a89" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `==` comparison operator to check whether the user trying to log in is an approved user.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "563f6d9c" + }, + "source": [ + "## Task 7\n", + "The number of approved users has now expanded to five. Rather than storing each of the approved users' usernames individually, it would be more concise to store them in an allow list called `approved_list`.\n", + "\n", + "The `in` operator in Python can be used to determine whether a given value is an element of a sequence. Using the `in` operator in a condition can help you check whether a specific username is part of a list of approved usernames. For example, in the code below, `username in approved_list` evaluates to `True` if the value of the `username` variable is included in `approved_list`.\n", + "\n", + "Complete the code in the following cell to display the same messages that you used in the previous step. When the condition evaluates to `True`, the following message will be displayed: `\"This user has access to this device.\"` When it evaluates to `False`, the following message will be displayed: `\"This user does not have access to this device.\"` Then, run the cell to observe its behavior. Be sure to replace each `### YOUR CODE HERE ###` with your own code. Afterwards, reassign the `username` variable to a username that is not approved and run the cell to observe what happens." + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "id": "ddd374cf" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This user has access to this device.\n" + ] + } + ], + "source": [ + "# Assign `approved_list` to a list of approved usernames\n", + "\n", + "approved_list = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\"]\n", + "\n", + "# Assign `username` to the username of a specific user trying to log in\n", + "\n", + "username = \"bmoreno\"\n", + "\n", + "# If the user trying to log in is among the approved users, then display a message that they are approved to access this device\n", + "# Otherwise, display a message that they do not have access to this device\n", + "\n", + "if username in approved_list:\n", + " print(\"This user has access to this device.\")\n", + "else:\n", + " print(\"This user does not have access to this device.\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "f131bcb9" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `else` keyword in the second conditional statement.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bbba5231" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `print()` function to display messages.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "17daa2cf" + }, + "source": [ + "#### **Question 5**\n", + "**What happens when an approved user tries to log in? What happens when an unapproved user tries to log in?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dcbb0f5f" + }, + "source": [ + "[Double-click to enter your responses here.]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ff35f9ec" + }, + "source": [ + "## Task 8\n", + "\n", + "Now you'll write another conditional statement. This one will use a `organization_hours` variable to check if the user logged in during specific organization hours. When that condition is met, the code should display the string `\"Login attempt made during organization hours.\"`. When that condition isn't met, the code should display the string `\"Login attempt made outside of organization hours.\"`.\n", + "\n", + "The `organization_hours` variable will have a Boolean data type. If `organization_hours` has a Boolean value of `True`, that means the user is logged in during the specified organization hours. If `organization_hours` has a Boolean value of `False`, that means the user is not logged in during those hours.\n", + "\n", + "Complete the conditional in the following cell. Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "id": "4e8974a8" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Login attempt made during organization hours.\n" + ] + } + ], + "source": [ + "# Assign `organization_hours` to a Boolean value that represents whether the user is trying to log in during organization hours\n", + "\n", + "organization_hours = True\n", + "\n", + "# If the entered `organization_hours` has a value of True, then display \"Login attempt made during organization hours.\"\n", + "# Otherwise, display \"Login attempt made outside of organization hours.\"\n", + "\n", + "if organization_hours == True:\n", + " print(\"Login attempt made during organization hours.\")\n", + "else:\n", + " print(\"Login attempt made outside of organization hours.\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8b5ec002", + "tags": [] + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the `==` comparison operator to check whether the user is logged in during the specified organization hours. Compare `organization_hours` to the appropriate Boolean value.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "98534659" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `print()` function to display messages.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "83e6e4c8" + }, + "source": [ + "
\n", + "

Hint 3

\n", + "\n", + "Use the `else` keyword in the second conditional statement.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "06044a44" + }, + "source": [ + "#### **Question 6**\n", + "**What happens when the user logs in during organization hours? What happens when they log in outside of organization hours?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "54a052de" + }, + "source": [ + "if organization_hours == True:\n", + " print(\"Login attempt made during organization hours.\")\n", + "else:\n", + " print(\"Login attempt made outside of organization hours.\")...\n", + "If the entered `organization_hours` has a value of True, then display \"Login attempt made during organization hours.\"\n", + "If the entered `organization_hours` has a value of False, then display \"Login attempt made outside of organization hours.\"\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "af03a37c-7452-4fbb-b72f-ee355a1537b1" + }, + "source": [ + "## Task 9\n", + "The following cell assembles the code from the previous tasks. It includes the conditional statement that checks if a user is on the allow list and the conditional statement that checks if the user logged in during organization hours.\n", + "\n", + "Run the cell below a few times. Each time, enter a different combination of values for `username` and `organization_hours` to observe how that affects the output." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "id": "29c7c915" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This user has access to this device.\n", + "Login attempt made during organization hours.\n" + ] + } + ], + "source": [ + "# Assign `approved_list` to a list of approved usernames\n", + "\n", + "approved_list = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\"]\n", + "\n", + "# Assign `username` to the username of a specific user trying to log in\n", + "\n", + "username = \"bmoreno\"\n", + "\n", + "# If the user trying to log in is among the approved users, then display a message that they are approved to access this device\n", + "# Otherwise, display a message that they do not have access to this device\n", + "\n", + "if username in approved_list:\n", + " print(\"This user has access to this device.\")\n", + "\n", + "else:\n", + " print(\"This user does not have access to this device.\")\n", + "\n", + "# Assign `organization_hours` to a Boolean value that represents whether the user is trying to log in during organization hours\n", + "\n", + "organization_hours = True\n", + "\n", + "# If the entered `organization_hours` has a value of True, then display \"Login attempt made during organization hours.\"\n", + "# Otherwise, display \"Login attempt made outside of organization hours.\"\n", + "\n", + "if organization_hours == True:\n", + " print(\"Login attempt made during organization hours.\")\n", + "else:\n", + " print(\"Login attempt made outside of organization hours.\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bdd488dc" + }, + "source": [ + "#### **Question 7**\n", + "**What happens when the user trying to log in is not among the approved users? What happens when the user trying to log in is among the approved users? What happens when the user tries to log in outside of organization hours?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "26ba310f" + }, + "source": [ + "- 1) What happens when the user trying to log in is not among the approved users? \n", + " - *It prints this out as it is not in the approved_list: (change names not in list)*\n", + " - `This user does not have access to this device. Login attempt made during organization hours.`\n", + "- 2) What happens when the user trying to log in is among the approved users?\n", + " - *It print this out as it is in the approoved users list:*\n", + " - `This user has access to this device. Login attempt made during organization hours.`\n", + "- 3) What happens when the user tries to log in outside of organization hours?\n", + " - *It prints this out as thge organizaiton hours is set to false to meet that it is outside organization, but keeping approved users to true.*\n", + " - `This user has access to this device. Login attempt made outside of organization hours.`" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2de9d7a0" + }, + "source": [ + "## Task 10\n", + "You can also provide a single message about the login attempt. To do this, you can join both conditions into a single conditional statement using a logical operator. This will make the code more concise.\n", + "\n", + "Examine the code in the following cell and add the missing operator that would allow for a single message. Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell. Then run the cell, entering different combinations of information, and observe what happens." + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": { + "id": "3983355f" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Login attempt made by an approved user during organization hours.\n" + ] + } + ], + "source": [ + "# Assign `approved_list` to a list of approved usernames\n", + "\n", + "approved_list = [\"elarson\", \"bmoreno\", \"tshah\", \"sgilmore\", \"eraab\"]\n", + "\n", + "# Assign `username` to the username of a specific user trying to log in\n", + "\n", + "username = \"bmoreno\"\n", + "\n", + "# Assign `organization_hours` to a Boolean value that represents whether the user is trying to log in during organization hours\n", + "\n", + "organization_hours = True\n", + "\n", + "# If the user is among the approved users and they are logging in during organization hours, then convey that the user is logged in\n", + "# Otherwise, convey that either the username is not approved or the login attempt was made outside of organization hours\n", + "\n", + "if username in approved_list and organization_hours == True:\n", + " print(\"Login attempt made by an approved user during organization hours.\")\n", + "else:\n", + " print(\"Username not approved or login attempt made outside of organization hours.\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "985cd541" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Use the logical operator that would allow you to check both conditions in the `if` statement (the condition that the entered username is in the approved list and the condition that the login attempt is occurring during organization hours).\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "68556c1b" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Use the `and` logical operator to check both conditions in the `if` statement (the condition that the entered username is in the approved list and the condition that the login attempt is occurring during organization hours).\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7dd439a9" + }, + "source": [ + "#### **Question 8**\n", + "**In this setup, what happens when the user trying to log in is an approved user and doing so during organization hours? What happens when the user either is not approved or attempts to log in outside of organization hours?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fd8f8d2b" + }, + "source": [ + "- In this setup, what happens when the user trying to log in is an approved user and doing so during organization hours? \n", + " - *It prints this out as it is in the approved_list and during organization hours*\n", + " - `Login attempt made by an approved user during organization hours.`\n", + "- What happens when the user either is not approved or attempts to log in outside of organization hours?\n", + " - *It prints this out as it is not in the approved_list or attempts to log in outside of organization hours (change name not in list*\n", + " - `Username not approved or login attempt made outside of organization hours.`\n", + " - *It prints this out as it is not in the approved_list or attempts to log in outside of organization hours (change to False in organization hours*\n", + " - `Username not approved or login attempt made outside of organization hours.`\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "d47250b6" + }, + "source": [ + "## Conclusion\n", + "**What are your key takeaways from this lab?**\n", + "\n", + "This course teaches how to use conditional statements in Python, which help coders make decisions based on certain conditions like checking if a system needs an update or if a user is allowed to log in, using `if`, `else`, and `elif` statements. I learned to use logical operators like `or` and `and` to combine conditions, making your code simpler and more efficient and these labs showed me how to handle different scenarios, like checking if a user is approved or if a login attempt happens during organization hours, all using easy-to-understand code that automates tasks like security checks and system updates. These coding python skills (introduction levels) are key for writing programs that respond to different situations.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [ + { + "file_id": "1KtE2DAbz5pj_IP34uE9K5nils1RUHVKg", + "timestamp": 1667953394971 + } + ] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/LAB_Activity_CreateAConditionalStatement.pdf b/LAB_Activity_CreateAConditionalStatement.pdf new file mode 100644 index 0000000..fe156ea Binary files /dev/null and b/LAB_Activity_CreateAConditionalStatement.pdf differ diff --git a/LAB_Activity_PracticeWritingPythonCode.ipynb b/LAB_Activity_PracticeWritingPythonCode.ipynb new file mode 100644 index 0000000..eab6e0a --- /dev/null +++ b/LAB_Activity_PracticeWritingPythonCode.ipynb @@ -0,0 +1,529 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "f7c0d886" + }, + "source": [ + "# Activity: Practice writing Python code" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0c066c48" + }, + "source": [ + "## Introduction\n", + "\n", + "Python is a programming language that helps in automating instructions to the computer in a variety of contexts, including security contexts. Writing code in Python is a valuable skill that helps security analysts thrive in their technical work.\n", + "\n", + "In this lab, you'll practice writing your first Python code while learning about a notebook environment. The hands-on practice you engage in throughout the labs will help you apply Python coding skills to your work as a security analyst. You'll benefit the most from the labs if you make sure to not only write in the cells that you're prompted to fill in, but also analyze all the cells thoroughly." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c5e84779" + }, + "source": [ + "

Tips for completing this lab

\n", + "\n", + "As you navigate this lab, keep the following tips in mind:\n", + "\n", + "- `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell.\n", + "- Feel free to open the hints for additional guidance as you work on each task.\n", + "- To enter your answer to a question, double-click the markdown cell to edit. Be sure to replace the \"[Double-click to enter your responses here.]\" with your own answer.\n", + "- You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook.\n", + "- You can download your work locally by clicking File and then Download and then specifying your preferred file format in the menu bar at the top of the notebook.\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cf55e108" + }, + "source": [ + "## Scenario\n", + "\n", + "As a security analyst, you'll often use notebook environments and notebooks to write and run code. This lab will help you get familiar with working in a notebook environment, writing code comments in Python, and displaying strings with the print() function.\n", + "\n", + "In this lab, you'll complete a series of tasks that involve observing and running some pre-written cells of text and code, as well as filling in cells with your own text, Python code, and code comments." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dddf9066" + }, + "source": [ + "## Task 1\n", + "\n", + "The lab environment you're working in is a notebook-based coding environment. Notebooks, such as this one, consist of two types of cells: (1) text cells, also known as markdown cells, and (2) code cells.\n", + "\n", + "Markdown cells allow you to write plain text and format it in the markdown language. Markdown language is used for formatting plain text in text editors and code editors. For example, you can use markdown to make headers, bold or italicize words, format text as code, add hyperlinks, and more.\n", + "\n", + "For this task, write something into the following markdown cell. Be sure to replace the \"[Double-click to edit this markdown cell and write something here.]\" with your own text. When you have finished editing, press the Shift and Enter keys (or on some keyboards, the Shift and Return keys) to display your text." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[Double-click to edit this markdown cell and write something here.]\n", + "\n", + "MY RESPONSES ARE BELOW! -Victoria; VictoriaRaven" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "87404578" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Double-click the markdown cell and replace the placeholder statement with your own. You can write any statement of your choice.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "64d3dfd5" + }, + "source": [ + "## Task 2\n", + "In Python notebooks, code cells allow you to write code comments and code in Python.\n", + "\n", + "To run a code cell, first place your cursor on the cell. Then, you can either click on the play icon, or press the Shift and Enter keys (or on some keyboards, the Shift and Return keys).\n", + "\n", + "For this task, run the following code cell as is and observe the output." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "04268ef8" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world!\n" + ] + } + ], + "source": [ + "# This cell displays \"Hello world!\"\n", + "\n", + "print(\"Hello world!\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "87404578" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Once you click on the code cell, you can run the code by either clicking on the triangular play icon, or press the Shift and Enter keys (or on some keyboards, the Shift and Return keys).\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### **Question 1**\n", + "**What do you observe about the output after you ran the code cell?**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It allows me to run then once it ran, it outputs Hello word! due to the print() function command code. It is like a bash outputs what you want the machine to print out." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5cb83fb8" + }, + "source": [ + "## Task 3\n", + "\n", + "Writing code comments is a way to document the intention behind code. It's a standard that analysts commonly use in their workflow. Writing comments that accompany code allows you to keep track of the technical decisions you've made in your project. This makes it easier for you and your team to read and revisit your code in order to understand what it does and why you took certain approaches.\n", + "\n", + "For this task, run the following code cell as is and observe the output." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "e837a2e5" + }, + "outputs": [], + "source": [ + "# In Python, comments do not get displayed\n", + "# This code cell contains only comments" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a0990885" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Once you click on the code cell, you can run the code by either clicking on the triangular play icon, or press the Shift and Enter keys (or on some keyboards, the Shift and Return keys).\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "94d7173f" + }, + "source": [ + "#### **Question 2**\n", + "**What do you observe about the output after you ran the cell above?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "35cebec3" + }, + "source": [ + "Since this one is a single comment using # not a print function print() previous, it does not output anything." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5dc33cf4" + }, + "source": [ + "## Task 4\n", + "\n", + "To type in a code cell, first click into the cell. Then you can write comments and code inside the cell.\n", + "\n", + "For this task, add a comment at the beginning of the following code cell, describing what the code is doing. Write the comment to say `# This cell displays \"I am using Python.\"`. Be sure to replace the `# YOUR COMMENT HERE` with your own comment before running the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "fdd233bd" + }, + "outputs": [], + "source": [ + "# This cell displays \"I am using Python.\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a0990885" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Once you click into the code cell, replace the placeholder comment with your comment. Recall that comments in Python start with the hash symbol (`#`).\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ffe34587" + }, + "source": [ + "#### **Question 3**\n", + "**What do you observe about the output after you ran the cell above?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8f2a5fa7" + }, + "source": [ + "Since this one is a single comment using # not a print function print() previous, it does not output anything.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "406daa6d" + }, + "source": [ + "## Task 5\n", + "In Python, `print()` helps you to display information to the screen.\n", + "\n", + "For this task, use `print()` to display the message `\"I am a security analyst.\"` by placing that message within the parentheses. Be sure to replace the `### YOUR CODE HERE ###` with your own code before running the following code cell." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "edcd0fe6" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I am a security analyst.\n" + ] + } + ], + "source": [ + "print(\"I am a security analyst.\") " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "26dfef63" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "\n", + "Once you click into the code cell, replace the `### YOUR CODE HERE ###` with `\"I am a security analyst.\"`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bf8d3259" + }, + "source": [ + "#### **Question 4**\n", + "**What do you observe about the output after you ran the cell above?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b5ee2b89" + }, + "source": [ + "It allows me to run then once it ran, it outputs I am a security analyst. due to the print() function command code. It is like a bash outputs what you want the machine to print out." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8f119729" + }, + "source": [ + "## Task 6\n", + "For this task, write a `print()` statement to display the string `\"Python is useful for security!\"` Be sure to replace the `### YOUR CODE HERE ###` with your own code before running the following code cell." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "id": "fe43a1fc" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Python is useful for security!\n" + ] + } + ], + "source": [ + "print(\"Python is useful for security!\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5a669a55" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "Click into the code cell and use `print()` to display the message.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b43c14e3" + }, + "source": [ + "
\n", + "

Hint 2

\n", + "\n", + "Place the message inside the parentheses of `print()`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bf8d3259" + }, + "source": [ + "#### **Question 5**\n", + "**What do you observe about the output after you ran the cell above?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b5ee2b89" + }, + "source": [ + "It allows me to run then once it ran, it outputs \"Python is useful for security!\" due to the print() function command code. It is like a bash outputs what you want the machine to print out." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "563f6d9c" + }, + "source": [ + "## Task 7\n", + "For your final task, you'll combine all the `print()` statements you've encountered and written in this lab up to this point, into one code cell.\n", + "\n", + "Complete the following code with the remaining messages. Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "ddd374cf" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world!\n", + "I am a security analyst.\n", + "Python is useful for security!\n" + ] + } + ], + "source": [ + "print(\"Hello world!\")\n", + "print(\"I am a security analyst.\")\n", + "print(\"Python is useful for security!\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "f131bcb9" + }, + "source": [ + "
\n", + "

Hint 1

\n", + "\n", + "First click into the code cell.\n", + "\n", + "For the third `print()` statement, place the string `\"I am a security analyst.\"` inside the parantheses of `print()`.\n", + "\n", + "For the fourth `print()` statement, place the string `\"Python is useful for security!\"` inside the parantheses of `print()`.\n", + "\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bf8d3259" + }, + "source": [ + "#### **Question 6**\n", + "**What do you observe about the output after you ran the cell above?**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dcbb0f5f" + }, + "source": [ + "It allows me to run then once it ran, it outputs \"Hello world!\" \"I am a security analyst.\" and \"Python is useful for security!\" due to the print() function command code. It is like a bash outputs what you want the machine to print out.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "d47250b6" + }, + "source": [ + "## Conclusion\n", + "**What are your key takeaways from this lab?**\n", + "\n", + "I learned how to use Python with print() and comments. I also learned that Jupyter is a python cloud compiler and runner without needing to install python or the vs / pycharm to go with it." + ] + } + ], + "metadata": { + "colab": { + "provenance": [ + { + "file_id": "1KtE2DAbz5pj_IP34uE9K5nils1RUHVKg", + "timestamp": 1667953394971 + } + ] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/LAB_Activity_PracticeWritingPythonCode.pdf b/LAB_Activity_PracticeWritingPythonCode.pdf new file mode 100644 index 0000000..943aa87 Binary files /dev/null and b/LAB_Activity_PracticeWritingPythonCode.pdf differ diff --git a/Python concepts from module 1.pdf b/Python concepts from module 1.pdf new file mode 100644 index 0000000..0201a83 Binary files /dev/null and b/Python concepts from module 1.pdf differ diff --git a/Python concepts from module 2.pdf b/Python concepts from module 2.pdf new file mode 100644 index 0000000..d29012d Binary files /dev/null and b/Python concepts from module 2.pdf differ diff --git a/Python concepts from module 3.pdf b/Python concepts from module 3.pdf new file mode 100644 index 0000000..9b603c6 Binary files /dev/null and b/Python concepts from module 3.pdf differ diff --git a/Python concepts from module 4.pdf b/Python concepts from module 4.pdf new file mode 100644 index 0000000..a918915 Binary files /dev/null and b/Python concepts from module 4.pdf differ diff --git a/Reference Guide_ Python concepts from Course 7.pdf b/Reference Guide_ Python concepts from Course 7.pdf new file mode 100644 index 0000000..0d46bde Binary files /dev/null and b/Reference Guide_ Python concepts from Course 7.pdf differ