Python Basics & Environment Setup
Install Python, set up a virtual environment, and write your first scripts using variables, types, and input/output.
Learning Objectives
- → Install Python 3 and verify the installation
- → Create and activate a virtual environment
- → Use the Python interactive shell (REPL)
- → Declare variables and understand Python's basic data types
- → Read user input and print formatted output
Why Python for Cybersecurity?
Python is the go-to language in security because it's fast to write, ships with powerful libraries, and runs everywhere. From scanning networks to parsing logs to automating report generation, most security tools start as a Python script.
Installing Python 3
On Debian/Ubuntu:
sudo apt update && sudo apt install python3 python3-pip python3-venv -y
python3 --version # should print 3.10 or higher
On Arch:
sudo pacman -S python python-pip
Verify with:
python3 -c "import sys; print(sys.version)"
Virtual Environments
Never install packages globally. Use venv:
mkdir ~/cybertools && cd ~/cybertools
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install --upgrade pip
Your prompt should now show (venv). Deactivate with deactivate.
The REPL
The Python REPL (Read-Eval-Print Loop) lets you experiment instantly:
>>> 2 + 2
4
>>> "hello".upper()
'HELLO'
>>> import os; os.getcwd()
'/home/user/cybertools'
Exit with exit() or Ctrl+D.
Variables and Data Types
name = "Alice" # str
age = 30 # int
score = 98.5 # float
active = True # bool
nothing = None # NoneType
print(type(name)) # <class 'str'>
Python is dynamically typed — variables hold references, not values. The type follows the object.
String Operations
target = "192.168.1.1"
print(target.split(".")) # ['192', '168', '1', '1']
print(f"Scanning {target}...") # f-strings (preferred)
print("IP: " + target) # concatenation
# Multi-line
banner = """
=== Recon Tool ===
Target: {}
""".format(target)
print(banner)
User Input
host = input("Enter target host: ")
port = int(input("Enter port: "))
print(f"Will scan {host}:{port}")
input() always returns a string — cast with int(), float() etc.
Numbers and Math
total = 255
subnet = total // 16 # integer division → 15
remain = total % 16 # modulo → 15
power = 2 ** 8 # exponentiation → 256
Running Scripts
Save to hello.py:
#!/usr/bin/env python3
name = input("Your name: ")
print(f"Hello, {name}! Welcome to CyberLearn.")
Run:
python3 hello.py
chmod +x hello.py && ./hello.py # after adding shebang
Mini Project – IP Info Script
#!/usr/bin/env python3
import socket
target = input("Enter hostname or IP: ").strip()
try:
ip = socket.gethostbyname(target)
hostname = socket.gethostbyaddr(ip)[0]
print(f"IP : {ip}")
print(f"Hostname : {hostname}")
except socket.gaierror as e:
print(f"Error: {e}")
This uses only the standard library — no installs needed.
Run python3 --version and pip3 --version in your terminal. Then create a virtual environment in a new directory called cybertools.
What command creates a virtual environment named 'venv'?
What command activates the virtual environment on Linux?
Open the Python REPL and create variables of each basic type: str, int, float, bool, None. Use type() to confirm each one.
What does type(3.14) return?
What function do you use to get user input in Python?
Write the IP Info script shown in the lesson. Test it with both a valid hostname (e.g. google.com) and an invalid one. Handle the exception gracefully.
Which Python module is used for hostname/IP resolution?