Chapter 22: Programming, Web Development, the Internet of Things, and New Trends
Programming with Python
Programming turns an algorithm (a finite, unambiguous sequence of steps) into code that a computer executes. Python is a high-level language with clear syntax (Python Software Foundation, n.d.; Matthes, 2023). Core ideas:
Variables and types: int, float, str, bool, list, tuple, dict, set.
Control flow: if/elif/else, for, while.
Functions: reusable blocks that take parameters and return values.
Data structures: lists (ordered, mutable), dictionaries (key–value pairs).
File handling: reading and writing text files.
Exceptions: handling errors with try/except.
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
primes = [n for n in range(1, 31) if is_prime(n)]
print(primes) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]Working with files and errors:
try:
with open("marks.txt") as f:
marks = [int(line) for line in f]
print("Average:", sum(marks) / len(marks))
except FileNotFoundError:
print("File not found")
except ValueError:
print("File contains non-numeric data")Algorithms and complexity
Searching: linear search checks each item (); binary search halves a sorted list each step ().
def binary_search(a, x):
lo, hi = 0, len(a) - 1
while lo <= hi:
mid = (lo + hi) // 2
if a[mid] == x:
return mid
elif a[mid] < x:
lo = mid + 1
else:
hi = mid - 1
return -1Sorting: bubble sort and insertion sort are ; merge sort is . Big-O notation describes how running time grows with input size. Good programs are correct, readable, tested, and efficient, in that order. Test with normal, boundary, and invalid inputs; use meaningful names and comments; and learn to read error messages and to debug by tracing values.
Web development
The web uses a client–server model: a browser (client) sends an HTTP request and a server returns a response. HTML5 structures content (headings, paragraphs, lists, tables, forms); CSS controls presentation (selectors, the box model, layout, responsive design with media queries); JavaScript adds behaviour in the browser. Server-side languages such as PHP process forms and connect to databases like MySQL.
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Marks</title>
<style>
table { border-collapse: collapse; }
td, th { border: 1px solid #444; padding: 4px 8px; }
</style></head>
<body>
<h1>Student Marks</h1>
<table>
<tr><th>Name</th><th>Marks</th></tr>
<tr><td>Nimal</td><td>78</td></tr>
</table>
</body>
</html>Key principles are accessibility, security (validate input, use prepared statements, HTTPS), usability, and responsive design for phones and desktops.
The Internet of Things (IoT)
IoT connects physical devices with sensors, actuators, and communication to the Internet. A typical system has sensors (temperature, light, motion, soil moisture), a microcontroller such as an Arduino that reads the sensors and controls actuators (LEDs, motors, relays), and a network that sends data to a server or cloud for storage, analysis, and control. Applications include smart agriculture, home automation, health monitoring, and smart cities. Issues are power use, reliability, security (default passwords, unpatched devices), and privacy.
ICT in business and new trends
E-business and e-commerce include business-to-consumer (B2C), business-to-business (B2B), and consumer-to-consumer (C2C) models; online payments; supply chains; and customer-relationship management. New trends include artificial intelligence and machine learning (systems that learn patterns from data), big data, cloud computing (infrastructure, platform, and software as a service), blockchain, cybersecurity, virtual and augmented reality, and robotics. Students should look at both the benefits and the ethical risks, including bias in algorithms, job impacts, surveillance, and the energy use of data centres.
The ICT project
The ICT syllabus includes a practical project in which students analyse a real problem and build a working solution (itguru.lk, n.d.-b). A good project follows the SDLC: define the problem and requirements; model the system (DFDs, ER diagram); design the database, interface, and algorithms; implement in Python, SQL, or HTML/PHP; test with planned test cases; and document and present the system. Choose a small, real problem (for example, a school library record system or a farm sensor monitor) and make it work well.
Common mistakes
Off-by-one errors in loops and indices (Python starts at 0).
Not handling invalid input.
Writing the whole program before testing any part of it.
Using string concatenation to build SQL queries (a security hole).
Practice questions
Write a Python function that returns the largest number in a list without using max.
What is the output of print([x**2 for x in range(5)])? [[0, 1, 4, 9, 16]]
Explain the role of a microcontroller in an IoT system.
State two ethical issues of using AI systems.