# Activity: Define and call a function

#   


<div class="cell border-box-sizing text_cell rendered" id="bkmrk-">  
</div>## Introduction

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.

In this lab, you'll practice defining and calling functions in Python.

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--1">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-tips-for-completing-"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>## Tips for completing this lab

As you navigate this lab, keep the following tips in mind: - `### YOUR CODE HERE ###` indicates where you should write code. Be sure to replace this with your own code before running the code cell. - Feel free to open the hints for additional guidance as you work on each task. - 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. - You can save your work manually by clicking File and then Save in the menu bar at the top of the notebook. - 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.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--2">  
</div>## Scenario

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.

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--3">  
</div>## Task 1

The following code cell contains a user-defined function named `alert()`.

For this task, analyze the function definition, and make note of your observations.

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.

<div class="cell border-box-sizing code_cell rendered" id="bkmrk--4"><div class="input">  
</div></div>```
# Define a function named `alert()` 

def alert():
    print("Potential security issue. Investigate further.")
```

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--5">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-1-when-analyzin"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 1**

When analyzing the function definition, make sure to observe the function body, which is the indented block of code after the function header. The function body tells you what the function does.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--6">  
</div>#### **Question 1**

Summarize what the user-defined function above does in your own words. Think about what the output would be if this function were called.

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--7">  
</div>it prints Potential security issue. Investigate further. IF the def function is called

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--8">  
</div>## Task 2

For this task, call the `alert()` function that was defined earlier and analyze the output.

Be sure to replace the `### YOUR CODE HERE ###` with your own code before running the following cell.

<div class="cell border-box-sizing code_cell rendered" id="bkmrk--9"><div class="input">  
</div></div>```
# Define a function named `alert()` 

def alert():
    print("Potential security issue. Investigate further.")

# Call the `alert()` function

alert()
```

<div class="output_wrapper" id="bkmrk--10"><div class="output"><div class="output_area">  
</div></div></div>```
Potential security issue. Investigate further.
```

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--11">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-1-to-call-the-f"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 1**

To call the function, write `alert()` after the function definition. Note that the function can be called only after it's defined.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--12">  
</div>#### **Question 2**

What are the advantages of placing this code in a function rather than running it directly?

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--13">  
</div>its re-usable

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--14">  
</div>## Task 3

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.

For this task, call the new `alert()` function and observe the output.

Be sure to replace the `### YOUR CODE HERE ###` with your own code before running the following cell.

<div class="cell border-box-sizing code_cell rendered" id="bkmrk--15"><div class="input">  
</div></div>```
# Define a function named `alert()`

def alert(): 
    for i in range(3):
        print("Potential security issue. Investigate further.")

# Call the `alert()` function

alert()
```

<div class="output_wrapper" id="bkmrk--16"><div class="output"><div class="output_area">  
</div></div></div>```
Potential security issue. Investigate further.
Potential security issue. Investigate further.
Potential security issue. Investigate further.
```

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--17">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-1-to-call-the-f-1"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 1**

To call the function, write `alert()` after the function definition. Note that the function can be called only after it's defined.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--18">  
</div>#### **Question 3**

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?

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--19">  
</div>its able to be called 3 times in a row instead of once in the 2nd example

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--20">  
</div>## Task 4

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.

For this task, start defining a function named `list_to_string()`. Write the function header.

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.

<div class="cell border-box-sizing code_cell rendered" id="bkmrk--21"><div class="input">  
</div></div>```
# Define a function named `list_to_string()`

def list_to_string():
    string = "string"
    print(string)
list_to_string
```

<div class="output_wrapper" id="bkmrk--22"><div class="output"><div class="output_area">  
</div></div></div>```
<function __main__.list_to_string()>
```

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--23">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-1-to-write-the-"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 1**

To write the function header, start with the `def` keyword, followed by the name of the function, parentheses, and a colon.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--24">  
</div>## Task 5

Now you'll begin to develop the body of the `list_to_string()` function.

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.

Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell.

<div class="cell border-box-sizing code_cell rendered" id="bkmrk--25"><div class="input">  
</div></div>```
# Define a function named `list_to_string()`

def list_to_string():

  # Store the list of approved usernames in a variable named `username_list`

    username_list = ["elarson", "bmoreno", "tshah", "sgilmore", "eraab", "gesparza", "alevitsk", "wjaffrey"]
    for username in username_list:
        print(username)
  # Write a for loop that iterates through the elements of `username_list` and displays each element


# Call the `list_to_string()` function

list_to_string()
```

<div class="output_wrapper" id="bkmrk--26"><div class="output"><div class="output_area">  
</div></div></div>```
elarson
bmoreno
tshah
sgilmore
eraab
gesparza
alevitsk
wjaffrey
```

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--27">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-1-the-%60for%60-loo"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 1**

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.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--28">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-2-in-each-itera"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 2**

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.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--29">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-3-to-call-the-f"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 3**

To call the function, write `list\_to\_string()` after the function definition. Recall that the function can be called only after it's defined.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--30">  
</div>#### **Question 4**

What do you observe from the output above?

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--31">  
</div>IT prints all the usernames in the list

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--32">  
</div>## Task 6

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.

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`.

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.

Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell.

<div class="cell border-box-sizing code_cell rendered" id="bkmrk--33"><div class="input">  
</div></div>```
# Define a function named `list_to_string()`

def list_to_string():

  # Store the list of approved usernames in a variable named `username_list`

    username_list = ["elarson", "bmoreno", "tshah", "sgilmore", "eraab", "gesparza", "alevitsk", "wjaffrey"]

  # Assign `sum_variable` to an empty string

    sum_variable = ""

  # Write a for loop that iterates through the elements of `username_list` and displays each element

    for username in username_list:
        sum_variable += username

    # Add a comma and space after each username, except the last one
        if username != username_list[-1]:
            sum_variable += ", "
    # Display the value of `sum_variable`
        print(sum_variable)

# Call the `list_to_string()` function

list_to_string()
```

<div class="output_wrapper" id="bkmrk--34"><div class="output"><div class="output_area">  
</div></div></div>```
elarson, 
elarson, bmoreno, 
elarson, bmoreno, tshah, 
elarson, bmoreno, tshah, sgilmore, 
elarson, bmoreno, tshah, sgilmore, eraab, 
elarson, bmoreno, tshah, sgilmore, eraab, gesparza, 
elarson, bmoreno, tshah, sgilmore, eraab, gesparza, alevitsk, 
elarson, bmoreno, tshah, sgilmore, eraab, gesparza, alevitsk, wjaffrey
```

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--35">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-1-inside-the-%60f"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 1**

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 `(+)`.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--36">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-2-use-the-%60prin"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 2**

Use the `print()` function to display the value of `sum\_variable`. Make sure to pass in `sum\_variable` to the call to `print()`.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--37">  
</div>#### **Question 5**

What do you observe from the output above?

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--38">  
</div>Username\_list\[-1\] represents the end of the list

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--39">  
</div>## Task 7

In this final task, you'll modify the code you wrote previously to improve the readability of the output.

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.

Be sure to replace each `### YOUR CODE HERE ###` with your own code before running the following cell.

<div class="cell border-box-sizing code_cell rendered" id="bkmrk--40"><div class="input">  
</div></div>```
# Define a function named `list_to_string()`

def list_to_string():

  # Store the list of approved usernames in a variable named `username_list`

    username_list = ["elarson", "bmoreno", "tshah", "sgilmore", "eraab", "gesparza", "alevitsk", "wjaffrey"]

  # Assign `sum_variable` to an empty string

    sum_variable = ""

  # Write a for loop that iterates through the elements of `username_list` and displays each element

    for username in username_list:
        sum_variable += username

    # Add a comma and space after each username, except the last one
        if username != username_list[-1]:
            sum_variable += ", "
    # Display the value of `sum_variable`
        print(sum_variable)

# Call the `list_to_string()` function
list_to_string()
```

<div class="output_wrapper" id="bkmrk--41"><div class="output"><div class="output_area">  
</div></div></div>```
elarson, 
elarson, bmoreno, 
elarson, bmoreno, tshah, 
elarson, bmoreno, tshah, sgilmore, 
elarson, bmoreno, tshah, sgilmore, eraab, 
elarson, bmoreno, tshah, sgilmore, eraab, gesparza, 
elarson, bmoreno, tshah, sgilmore, eraab, gesparza, alevitsk, 
elarson, bmoreno, tshah, sgilmore, eraab, gesparza, alevitsk, wjaffrey
```

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--42">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-1-inside-the-%60f-1"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 1**

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. To complete this step, place `", "` after the last addition operator (`+`).

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--43">  
</div><div class="cell border-box-sizing text_cell rendered" id="bkmrk-hint-2-to-call-the-f"><div class="inner_cell"><div class="text_cell_render border-box-sizing rendered_html"><details>#### **Hint 2**

To call the function, write `list\_to\_string()` after the function definition. Note that the function can be called only after it's defined.

</details></div></div></div><div class="cell border-box-sizing text_cell rendered" id="bkmrk--44">  
</div>#### **Question 6**

What do you notice about the output from the function call this time?

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--45">  
</div>its the same but the original difference was that it didnt contain the comma

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--46">  
</div>## Conclusion

**What are your key takeaways from this lab?**

<div class="cell border-box-sizing text_cell rendered" id="bkmrk--47">  
</div>def functions are cool, but also so is the if username != username\_list\[-1\]: syntax for if you wanna do something when list ends