Sitemap
Tehreem

Tehreem

Hey everyone! I'll be documenting my cybersecurity journey here. What I learn, what I understand. Feel free to reach out and tag along :)

Python Programming in Cybersecurity

4 min readAug 23, 2026

--

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:

Press enter or click to view image in full size

Problem Statement#2:

Test the baseline conditional against different OS strings (OS 1, OS 2, OS 3) to observe output changes.

Solution:

Press enter or click to view image in full size

Problem Statement#3:

Add an else branch to trigger an "update needed" prompt for any non-OS 2 environment.

Solution:

Press enter or click to view image in full size

Problem Statement#4:

Refactor with multiple elif statements to specifically flag OS 1 and OS 3 while ignoring invalid string inputs.

Solution:

Press enter or click to view image in full size

Problem Statement#5:

Optimize the OS check into a concise statement using the logical or operator.

Solution:

Press enter or click to view image in full size

Problem Statement#6:

Validate an incoming login attempt against two individual authorized user variables using an if/or condition.

Solution:

Press enter or click to view image in full size

Problem Statement#7:

Check if a username exists inside an approved user list using Python’s in membership operator.

Solution:

Press enter or click to view image in full size
Press enter or click to view image in full size

Problem Statement#8:

Check access timing by evaluating a boolean variable (organization_hours) to flag after-hours logins.

Solution:

Press enter or click to view image in full size
Press enter or click to view image in full size

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:

Press enter or click to view image in full size
Press enter or click to view image in full size

Problem Statement#10:

Consolidate user authorization and login hour checks into a single concise condition using the logical and operator.

Solution:

Press enter or click to view image in full size
Press enter or click to view image in full size

Iterative Statements in Python: (for, while)

Problem Statement#1:

Use a for loop with range() to print a connection failure alert 3 times.

Solution:

Press enter or click to view image in full size

Problem Statement#2:

Parameterize loop execution by passing a dynamic connection_attempts variable into range().

Solution:

Press enter or click to view image in full size

Problem Statement#3:

Replicate the 3-attempt connection alert using a condition-controlled while loop.

Solution:

Press enter or click to view image in full size

Problem Statement#4:

Iterate through a list of login attempt IP addresses using a for loop.

Solution:

Press enter or click to view image in full size

Problem Statement#5:

Implement an if/else check inside the IP loop to verify incoming addresses against a static allowlist.

Solution:

Press enter or click to view image in full size

Problem Statement#6:

Add a break statement to halt the loop immediately and trigger an alert if an unauthorized IP is detected.

Solution:

Press enter or click to view image in full size
the very first ip was not allowed, so the loop was exited

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:

Press enter or click to view image in full size

Problem Statement#8:

Add an if threshold check inside the ID generation loop to alert when 10 remaining IDs are left.

Solution:

Press enter or click to view image in full size
Press enter or click to view image in full size
the statement is printed just when 10 ids were left

This was the beginning of what one can do with Python. Next up, I’ll be exploring the Python functions and beyond.

--

--

Written by Tehreem

Hey everyone! I'll be documenting my cybersecurity journey here. What I learn, what I understand. Feel free to reach out and tag along :)