01 fundamentals

Python is the first language most security people actually ship in Not because it is cute , because a working tool beats a perfect theory at 3AM

Table of Contents

Setup

Check what you have before installing anything

python3 --version

Expected output:

Python 3.15.2

That is the current stable release The command name varies across systems , use python3 , do not alias it away and forget why

Inside a project I use python3 -m pip so the interpreter and the package manager agree

Running code

The REPL is for poking at an idea before it becomes a file

python3
>>> print("Hello, World!")
Hello, World!
>>> 2 + 2
4

A script is just a file you execute

python3 my_script.py

Common mistake: running the file with python on a system where that name is missing Fix: python3 my_script.py

Types

Python decides the type at runtime You still need to know what you are holding

name = "0x1RIS"      # str
port = 443           # int
version = 1.1        # float
is_active = True     # bool
notes = None         # NoneType

True and False are capitalized None is not a string and not zero

Data structures

Four structures cover most work

# list , ordered , mutable
ports = [22, 80, 443]
ports.append(8080)
print(ports[1])  # 80

# dict , key to value
headers = {"User-Agent": "MyScanner/1.0", "Accept": "application/json"}
print(headers["User-Agent"])

# set , unique only
unique_ports = {80, 443, 80, 80}
print(unique_ports)  # {80, 443}

# tuple , ordered , fixed
target = ("127.0.0.1", 22)

set throws duplicates away silently That is useful for port lists and dangerous when order matters

Operators

Arithmetic, comparison, and logic

# arithmetic
2 ** 3       # 8
7 // 2       # 3
7 % 2        # 1

# comparison
port == 443
port != 80
port >= 443

# logic
is_active and port == 443
not is_active

// drops the decimal ** is exponent % gives the remainder

Control flow

Indentation is the block syntax Four spaces, no tabs mixed in

port = 443
if port == 443:
    print("HTTPS")
elif port == 80:
    print("HTTP")
else:
    print("Other")

for p in [22, 80, 443]:
    print(f"Checking port {p}")

count = 0
while count < 3:
    print(count)
    count += 1

Mix tabs and spaces and Python will throw TabError Fix: show whitespace in your editor and use spaces only

Functions

def creates a function The docstring is not decoration , it is the first line of documentation someone will read

def greet(name):
    """Return a greeting for the given name"""
    return f"Hello, {name}!"

message = greet("World")
print(message)  # Hello, World!

Returning a value beats printing inside the function You can test a return , you cannot test a side effect as easily

Common errors

  • python: command not found -> use python3
  • IndentationError -> check spaces , check the line before the failure
  • NameError -> the variable was never assigned or the spelling changed
  • KeyError -> the dictionary key is not there , use dict.get() when absence is normal

Read the actual error message Seriously , read it , do not panic and reinstall Python

Posted