Variables help security analysts 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.
In this lab, I'll practice assigning values to variables and determining their data types.
I am 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, I'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.
Throughout this lab, I'll assign these variables and check the data types of the variables.
In my work as an analyst, imagine there is a device only users specified on an allow list can access, and its device ID is "72e08x0".
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.
Now that the variable device_id is defined, I can return its data type.
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.
72e08x0 gets outputted when you display 'device_id_type'
As I continue my work, I'm 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".
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.
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.
Now, imagine that I'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. I'm given the updated list of usernames with access, including the new employee, as follows: "madebowa", "jnguyen", "tbecker", "nhersh", "redwards", and "lpope".
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.
I can see that Python outputs the first list and then the updated list afterward because of how the commands were ordered.
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.
The output above shows that the data type of max_logins is int, which means that max_logins stores an integer value.
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.
The output above shows that the data type of login_attempts is int, which means that login_attempts stores an integer value.
In this task, I'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.
The output above is True, which indicates that login_attempts is less than or equal to max_logins. In other words, the current number of attempts the user has made to log in has not yet exceeded the maximum number of attempts allowed.
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, I might choose a value that is higher than the maximum number of attempts allowed. Observe how the output changes.
The Boolean value in the output changes depending on the value assigned to login_attempts. For example, when login_attempts is assigned to 5, the output is False, which indicates that login_attempts is not less than or equal to max_logins. In other words, the current number of login attempts the user has made has exceeded the maximum number of attempts allowed.
Finally, I can also assign a Boolean value of True or False to a variable.
In this task, I'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.
The output above shows that the data type of the login_status is bool, which means that login_status stores a Boolean value.
What are the key takeaways from this lab?
- Many useful operators in Python help you work with variables.
- The
=
assignment operator allows you to assign or reassign a specific value to a variable. - The
<=
comparison operator allows you to compare the value of one variable to the value of another.
- The
- The
type()
function in Python helps you to determine the data type of an object.- If you pass in a variable to
type()
, it will output the data type of the value stored in the variable.
- If you pass in a variable to
- The
print()
function in Python allows you to display information.- It can take in a value directly, a variable that stores a value, or a comparison between variables that evaluates to a Boolean value.