Procedural programming follows a step-by-step approach to breaking down tasks into routines and subroutines
It emphasises modular design, where code is grouped into functions and procedures for reuse and clarity
Variables hold state and control structures that determine the flow of execution in the program
Variables
Storing data values that can change
x = 10print(x) # Output: 10
Constants
Storing values that remain unchanged
PI = 3.1415 print(PI) # Output: 3.1415
Selection
Decision-making constructs
x = 7 if x > 5: print("Greater") # Output: Greater else: print("Smaller")
Iteration
Using loops to repeat actions
for i in range(3): print(i) # Output: 0, 1, 2
Sequence
Executing statements sequentially
x = 5 y = x + 10 print(y) # Output: 15
Subroutines
Organising code into reusable parts
def greet(name): return "Hello, " + name greeting = greet("Alice") print(greeting) # Output: Hello, Alice
String Handling
Operations on character strings
name = "Alice" upper_name = name.upper()print(upper_name) # Output: ALICE
File Handling
Reading from and writing to files
with open('file.txt', 'w') as file: file.write("Hello, World!") with open('file.txt', 'r') as file: content = file.read() print(content) # Output: Hello, World!
Boolean Operators
Logical operations
x = 7 y = 5 is_valid = x > 5 and y < 10 print(is_valid) # Output: True
Arithmetic Operators
Basic mathematical operations
x = 5 y = 3 sum_value = x + y product = x * y print(sum_value, product) # Output: 8, 15