Coding Principles – With Examples
Use this as a quick exam guide with patterns and examples.
1. Clarity & Readability
Use meaningful names and clean formatting.
age = 20 # good
a = 20 # bad
2. DRY Principle
Avoid repeating code.
for i in range(2):
print('Hello')
3. KISS Principle
Keep logic simple.
if age >= 18:
print('Adult')
4. Input Validation
Ensure correct input.
value = int(input())
while value < 0:
value = int(input())
5. Standard While Loop Pattern (VERY IMPORTANT)
Used in most tests.
value = int(input())
while value != 0:
print(value)
value = int(input())
6. Running Total Example
Sum values until negative.
total = 0
value = float(input())
while value >= 0:
total += value
value = float(input())
print(total)
7. Sentinel Value
A value that stops the loop (e.g., 0 or negative).
8. Nested Loops (Diagram)
Outer loop = rows, Inner loop = columns.
for i in range(3):
for j in range(5):
print('*', end='')
print()
9. Algorithm Thinking
Steps: INPUT → PROCESS → LOOP → OUTPUT
10. Auto-Marker Rules
No prompts. Exact structure matters.