Python Session 01: Fundamentals
Starting from zero. This session covers Python’s core data types, collections, and comprehension syntax.
Pre-Session State
-
Can run Python interpreter (
python3) -
Understand basic programming concepts (variables, loops)
-
Know how to exit REPL (
exit()or Ctrl+D)
Setup
Open Python REPL:
python3
Or create a test file:
cat > /tmp/py-test.py << 'EOF'
# Python fundamentals practice
EOF
python3 /tmp/py-test.py
Lesson 1: Basic Types
Concept: Python has strings, integers, floats, and booleans.
Exercise 1.1: String operations
hostname = "kvm-01.inside.domusdigitalis.dev"
print(hostname.upper())
print(hostname.split('.'))
print(hostname.startswith('kvm'))
print(len(hostname))
Exercise 1.2: Numbers
vlan = 10
cpu_percent = 85.5
is_active = True
print(f"VLAN {vlan}, CPU {cpu_percent}%, Active: {is_active}")
print(type(vlan), type(cpu_percent), type(is_active))
Exercise 1.3: Type conversion
port_str = "443"
port_int = int(port_str)
print(port_int + 1)
count = 42
count_str = str(count)
print("Count: " + count_str)
Lesson 2: Lists
Concept: Lists are ordered, mutable collections.
Exercise 2.1: List operations
hosts = ["kvm-01", "kvm-02", "vault-01"]
print(hosts[0]) # First element
print(hosts[-1]) # Last element
print(hosts[1:]) # Slice from index 1
print(len(hosts)) # Length
Exercise 2.2: List modification
hosts = ["kvm-01", "kvm-02"]
hosts.append("vault-01")
hosts.insert(0, "gateway")
hosts.remove("kvm-02")
print(hosts)
Exercise 2.3: List iteration
hosts = ["kvm-01", "kvm-02", "vault-01"]
for host in hosts:
print(f"Checking {host}...")
for i, host in enumerate(hosts):
print(f"{i}: {host}")
Lesson 3: Dictionaries
Concept: Dicts are key-value mappings.
Exercise 3.1: Dict operations
server = {
"name": "kvm-01",
"ip": "10.50.1.110",
"role": "hypervisor",
"active": True
}
print(server["name"])
print(server.get("port", 22)) # Default if missing
print(server.keys())
print(server.values())
Exercise 3.2: Dict modification
server = {"name": "kvm-01"}
server["ip"] = "10.50.1.110"
server.update({"role": "hypervisor", "vlan": 10})
del server["vlan"]
print(server)
Exercise 3.3: Dict iteration
server = {"name": "kvm-01", "ip": "10.50.1.110"}
for key, value in server.items():
print(f"{key}: {value}")
Lesson 4: Comprehensions
Concept: Concise syntax for building lists/dicts.
Exercise 4.1: List comprehension
hosts = ["kvm-01", "kvm-02", "vault-01"]
upper_hosts = [h.upper() for h in hosts]
print(upper_hosts)
# With filter
kvm_hosts = [h for h in hosts if h.startswith("kvm")]
print(kvm_hosts)
Exercise 4.2: Dict comprehension
hosts = ["kvm-01", "kvm-02", "vault-01"]
host_lengths = {h: len(h) for h in hosts}
print(host_lengths)
Exercise 4.3: Nested comprehension
matrix = [[i*j for j in range(1,4)] for i in range(1,4)]
print(matrix) # [[1,2,3], [2,4,6], [3,6,9]]
Summary: What You Learned
| Concept | Syntax | Example |
|---|---|---|
f-string |
|
|
List access |
|
|
Dict access |
|
|
List comp |
|
|
Dict comp |
|
|
Enumerate |
|
|
Exercises to Complete
-
[ ] Create a list of IPs: ["10.50.1.110", "10.50.1.111", "10.50.1.60"]
-
[ ] Build a dict mapping hostnames to IPs
-
[ ] Use comprehension to get only IPs starting with "10.50.1"
-
[ ] Print each host in format "hostname → ip"
Next Session
Session 02: File Operations - Reading, writing, JSON, CSV.
Session Log
| Timestamp | Notes |
|---|---|
Start |
<Record when you started> |
End |
<Record when you finished> |