Python Programming in Cybersecurity
Python is a general-purpose programming language which is not limited to a specific field. It is used in cybersecurity to automate manual tasks. This writeup is a log of my completed Jupyter Notebook labs for Course 7 (Automate Cybersecurity Tasks with Python) in the Google Cybersecurity Certificate. It contains the problems and cell execution outputs covering Python conditionals (if/elif/else) and loops (for/while) used for security task automation.
Scenarios for automation:
Following are some of the scenarios where Python can be used to automate cybersecurity tasks, using conditional statements and iterative statements.
Conditional Statements in Python: (if, elif, else)
Problem Statement#1:
Check if the device OS matches the approved version (OS 2) using an if statement.
Solution:
Problem Statement#2:
Test the baseline conditional against different OS strings (OS 1, OS 2, OS 3) to observe output changes.
Solution:
Problem Statement#3:
Add an else branch to trigger an "update needed" prompt for any non-OS 2 environment.
Solution:
Problem Statement#4:
Refactor with multiple elif statements to specifically flag OS 1 and OS 3 while ignoring invalid string inputs.
Solution:
Problem Statement#5:
Optimize the OS check into a concise statement using the logical or operator.
Solution:
Problem Statement#6:
Validate an incoming login attempt against two individual authorized user variables using an if/or condition.
Solution:
Problem Statement#7:
Check if a username exists inside an approved user list using Python’s in membership operator.
Solution:
Problem Statement#8:
Check access timing by evaluating a boolean variable (organization_hours) to flag after-hours logins.
Solution:
Problem Statement#9:
Combine user allowlist verification and organization hour checks into a multi-variable test run.
Get Tehreem’s stories in your inbox
Join Medium for free to get updates from this writer.
Solution:
Problem Statement#10:
Consolidate user authorization and login hour checks into a single concise condition using the logical and operator.
Solution:
Iterative Statements in Python: (for, while)
Problem Statement#1:
Use a for loop with range() to print a connection failure alert 3 times.
Solution:
Problem Statement#2:
Parameterize loop execution by passing a dynamic connection_attempts variable into range().
Solution:
Problem Statement#3:
Replicate the 3-attempt connection alert using a condition-controlled while loop.
Solution:
Problem Statement#4:
Iterate through a list of login attempt IP addresses using a for loop.
Solution:
Problem Statement#5:
Implement an if/else check inside the IP loop to verify incoming addresses against a static allowlist.
Solution:
Problem Statement#6:
Add a break statement to halt the loop immediately and trigger an alert if an unauthorized IP is detected.
Solution:
Problem Statement#7:
Use a while loop to auto-generate unique employee IDs between 5000 and 5150 (both included) and only the ones divisible by 5.
Solution:
Problem Statement#8:
Add an if threshold check inside the ID generation loop to alert when 10 remaining IDs are left.
Solution:
This was the beginning of what one can do with Python. Next up, I’ll be exploring the Python functions and beyond.







