Python › Module 1 › Lesson 2
Variables, Loops, Functions
Build the syntax you need for scanners and parsers: variables, loops, and reusable functions
Opening
Security scripts are mostly loops over stuff
Ports, log lines, URLs, hashes—almost every tool repeats work. Master variables, for/while loops, and functions, and you can build the rest of this topic.
1. Variables & Types You Will Use
Variables for a mini host checkhost = "127.0.0.1" port = 22 timeout = 1.0 is_lab = True print(host, port, timeout, is_lab)
host = "127.0.0.1" port = 22 timeout = 1.0 is_lab = True print(host, port, timeout, is_lab)
2. Loops
Loop over portsports = [22, 80, 443] for port in ports: print("Checking", port)
ports = [22, 80, 443]
for port in ports:
print("Checking", port)3. Functions
Wrap logic in a functiondef banner(name): return f"[+] scanning {name}" print(banner("lab-target"))
def banner(name):
return f"[+] scanning {name}"
print(banner("lab-target"))Functions keep scanners readable: one function opens a socket, another prints results, main() orchestrates. You will reuse this pattern in the port scanner lab.
Name things honestly
target_host beats x. failed_logins beats data2. Future you is a teammate.
Knowledge Check
Which loop best checks a list of ports?
Multiple choice
Knowledge Check
True or False: Functions help reuse scanner logic without copying code.
True or False
Knowledge Check
In host = "127.0.0.1", the value is a:
Multiple choice