Extensive Programming Notes and
Question-Answers
1. Logical Tests and Choice
A logical test is a comparison that results in either True or False. Programs use logical tests
in conditional structures (if/else) to make decisions. In Scratch, conditional structures are
blocks, while in Python they are text-based commands that require colons and indentation.
Examples:
3 + 4 == 7 → True
4 > 8 → False
Relational Operators in Python:
== : Equal to
!= : Not equal to
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Less than or equal to
2. Loops
Loops allow repetition of commands. Two main types:
1. Counter Loop (fixed loop): Repeats a known number of times.
- Scratch: repeat 10
- Python: for i in range(10):
2. Conditional Loop: Repeats until a condition is met.
- Scratch: repeat until condition is true
- Python: while condition:
3. Variables and Accumulation
Accumulation means repeatedly adding to a total. The pattern is:
total = total + number
In Python, the shorthand is: total += number
Common mistakes:
- Not initializing total before using it.
- Resetting total inside the loop.
- Forgetting to convert input to int().
4. Errors: Syntax vs Logical
Syntax Errors: Mistakes in Python rules that prevent program from running.
- Examples: missing colon, using = instead of ==, forgetting indentation.
Logical Errors: The program runs but gives wrong output.
- Example: Forgetting to update input inside a loop → infinite loop.
- Example: Adding 1 instead of adding user input.
5. Bird Counter Program
A program to count birds seen by typing 'Y' each time.
total = 0
visitor = input('Type Y if you see a bird (or anything else to stop):
').strip().upper()
while visitor == 'Y':
total += 1
visitor = input('Type Y if you see another bird: ').strip().upper()
print('Total birds seen:', total)
6. Bird Addition Program
Counts group visits per minute, ends with sentinel value (99).
total = 0
visits = int(input('Visits in one minute (99 to stop): '))
while visits != 99:
total += visits
visits = int(input('Visits in one minute (99 to stop): '))
print('The total number of visits was:', total)
7. User-Friendly and Readable Programs
User-friendly programs have clear prompts, short inputs, and explanatory outputs.
Readable programs have good variable names and comments.
Example of good output:
print('Total number of birds:', total)
Use comments (#) to explain code.
8. Practice Questions and Answers
1. Q1. What is a conditional structure?
Answer: A structure that allows different actions depending on whether a logical test is
True or False.
2. Q2. What is the difference between = and == in Python?
Answer: = assigns a value, == compares values.
3. Q3. Give two relational operators.
Answer: >, <
4. Q4. When to use counter vs conditional loop?
Answer: Counter loop if repetitions are known, conditional loop if repetitions depend on a
condition.
5. Q5. Why is a logical error harder to spot?
Answer: Because the program runs without crashing but gives incorrect results.