Skip to content

03. Data Types

Introduction

Data types are the foundation of Python. In cybersecurity, they help us store, process, and manipulate information like:

  • IP addresses & ports (strings, integers, tuples)

  • Credentials & payloads (strings, dictionaries, bytes)

  • Network logs & results (lists, sets, dicts)

  • Encryption, packet data, session info (bytes, booleans, None)


Core Data Types in Python (with Cybersecurity Examples)

1. Strings (str)

  • Store text → usernames, payloads, log entries, URLs.
payload = "' OR 1=1 --"
print("SQL Injection Payload:", payload)

Usage: SQLi payloads, URLs, wordlists, log analysis.


2. Integers (int)

  • Store whole numbers → ports, counts, retries.
port = 22
print("Scanning port:", port)

Usage: Port scanning, failed login counters.


3. Floats (float)

  • Store decimal values → delays, timings.
ping_time = 0.534
print("Response time:", ping_time, "sec")

Usage: Latency in ping sweeps, timing attacks.


4. Booleans (bool)

  • True/False values → conditions.
is_alive = True
if is_alive:
    print("Target is up!")

Usage: Service status (open/closed), login success/failure.


5. Lists (list)

  • Ordered, changeable collections → multiple IPs, payloads.
targets = ["192.168.1.1", "192.168.1.2"]
for ip in targets:
    print("Scanning:", ip)

Usage: Target lists, usernames, passwords.


6. Tuples (tuple)

  • Ordered, immutable collections → fixed data like IP-port pairs.
target = ("192.168.1.5", 443)
print("IP:", target[0], "Port:", target[1])

Usage: Host-port mapping.


7. Sets (set)

  • Unordered, unique values → no duplicates.
ips = {"192.168.1.1", "192.168.1.1", "192.168.1.2"}
print("Unique IPs:", ips)

Usage: Unique IPs in logs, deduplication in scans.


8. Dictionaries (dict)

  • Key-value pairs → configs, credentials, structured results.
creds = {"username": "admin", "password": "toor"}
print("User:", creds["username"])

Usage: Login credentials, HTTP headers, scan reports.


9. Bytes (bytes)

  • Store raw binary data → packets, hashes, encrypted content.
data = b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
print("Raw Request:", data)

Usage:

  • Crafting packets with Scapy

  • Handling hashes (MD5/SHA256)

  • Working with encrypted data


10. NoneType (None)

  • Represents no value / null.
session = None
if session is None:
    print("No active session.")

Usage: No session, no connection, placeholder for empty values.


Data Type Conversions

Cybersecurity scripts often need conversions (e.g., from strings to integers).

ip_str = "192.168.1.1" port_str = "80" port = int(port_str)   
# 03. Data Types

Cybersecurity Mini Example (Mixed Types)

target = {
    "ip": "192.168.1.10",       # string
    "ports": [22, 80, 443],     # list of ints
    "status": True,             # bool
    "response_time": 0.21,      # float
    "banner": b"SSH-2.0-OpenSSH", # bytes
    "session": None             # None
}
print(target)

Quick Reference Table

Data Type Example Cybersecurity Usage
String "admin", "http://example.com" Payloads, URLs, logs, wordlists
Integer 80, 443 Port numbers, counters
Float 0.532 Response times, delays
Boolean True, False Service status, login checks
List ["192.168.1.1", "192.168.1.2"] Target lists, wordlists
Tuple ("192.168.1.10", 22) Host-port mapping
Set {"192.168.1.1", "192.168.1.2"} Unique IPs, deduplication
Dict {"user":"admin","pass":"1234"} Credentials, headers, reports
Bytes b"SSH-2.0-OpenSSH" Packets, hashes, encryption
None None No session, null value

Practice Section — Cybersecurity Exercises

Strings

  • Create a variable with a SQL injection payload and print it.
  • Extract the domain name from "http://example.com/login".

Integers

  • Store port numbers in variables and print them.
  • Write a loop that prints ports from 20 to 25.

Floats

  • Simulate a ping sweep storing different response times as floats.
  • Print if latency is > 1.0 sec = "High Latency", else "OK".

Booleans

  • Write a script that checks if a given port is 22. If yes, print "SSH detected".

Lists

  • Store a list of IP addresses and iterate over them.
  • Write a script that checks if "192.168.1.5" exists in the list.

Tuples

  • Store an IP and port pair in a tuple. Print both.
  • Try modifying the tuple → notice it’s immutable.

Sets

  • Store IPs with duplicates in a set. Print the unique IPs.
  • Combine two sets of IPs using union().

Dictionaries

  • Store credentials in a dictionary and print the username.
  • Store open ports of an IP in a dictionary → {"192.168.1.1": [22, 80]}.

Bytes

  • Convert the string "Hello" into bytes and print it.
print("Hello".encode())
  • Create a fake HTTP request in bytes and print it.

NoneType

  • Create a variable session = None.
  • Check if session is None and print "No active session".

Tip: Try combining datatypes. Example:

scan_result = {
    "ip": "192.168.1.5",
    "ports": [22, 80],
    "status": True,
    "banner": b"SSH-2.0-OpenSSH",
    "latency": 0.34,
    "session": None
}
print(scan_result)

Takeaway:
Each Python data type has a direct role in cybersecurity scripting → from handling payloads and credentials to crafting packets and analyzing logs.