demonstrate_conditional_statements()
# Uncomment to run interactive examples
    # grade_calculator()
    # simple_calculator()
Lab 4: Iterative and Control Transfer Statements
Duration: 2 Hours
Objective
    Understand iterative statements (for, while loops)
    Learn control transfer statements (break, continue, pass)
Theory
Iterative statements allow repetition of code blocks, while control transfer statements modify the flow of
execution within loops.
Complete Lab 4 Program
  python
#!/usr/bin/env python3
"""
Lab 4: Iterative and Control Transfer Statements
Demonstrating loops and control flow statements
"""
def demonstrate_for_loops():
  print("=== FOR LOOP DEMONSTRATIONS ===\n")
  # Basic for loop with range
  print("1. BASIC FOR LOOP WITH RANGE:")
  print("Numbers from 1 to 5:")
  for i in range(1, 6):
      print(f" {i}")
  # For loop with step
  print("\n2. FOR LOOP WITH STEP:")
  print("Even numbers from 0 to 10:")
  for i in range(0, 11, 2):
      print(f" {i}")
  # For loop with list
  print("\n3. FOR LOOP WITH LIST:")
  fruits = ["apple", "banana", "orange", "grape"]
  print("Fruits:")
  for fruit in fruits:
      print(f" {fruit}")
  # For loop with enumerate
  print("\n4. FOR LOOP WITH ENUMERATE:")
  colors = ["red", "green", "blue"]
  for index, color in enumerate(colors):
      print(f" {index}: {color}")
  # Nested for loops
  print("\n5. NESTED FOR LOOPS:")
  print("Multiplication table (1-3):")
  for i in range(1, 4):
      for j in range(1, 4):
        print(f" {i} x {j} = {i * j}")
      print() # Empty line for separation
def demonstrate_while_loops():
  print("=== WHILE LOOP DEMONSTRATIONS ===\n")
  # Basic while loop
  print("1. BASIC WHILE LOOP:")
  count = 1
  print("Counting to 5:")
  while count <= 5:
    print(f" Count: {count}")
    count += 1
  # While loop with condition
  print("\n2. WHILE LOOP WITH CONDITION:")
  number = 1
  print("Powers of 2 less than 100:")
  while number < 100:
    print(f" {number}")
    number *= 2
  # While loop with user input
  print("\n3. WHILE LOOP FOR INPUT VALIDATION:")
  print("Simulating password entry (password: 'python'):")
  attempts = 0
  max_attempts = 3
  correct_password = "python"
  # Simulated inputs for demonstration
  test_inputs = ["abc", "123", "python"]
  input_index = 0
  while attempts < max_attempts:
    if input_index < len(test_inputs):
       password = test_inputs[input_index]
       print(f" Attempt {attempts + 1}: Entered '{password}'")
       input_index += 1
    else:
       break
    if password == correct_password:
       print(" Access granted!")
       break
    else:
       attempts += 1
       print(f" Wrong password. {max_attempts - attempts} attempts remaining.")
  if attempts == max_attempts:
    print(" Account locked!")
def demonstrate_control_transfer():
  print("\n=== CONTROL TRANSFER STATEMENTS ===\n")
# Break statement
print("1. BREAK STATEMENT:")
print("Finding first number divisible by 7:")
for num in range(1, 20):
  if num % 7 == 0:
     print(f" Found: {num}")
     break
  print(f" Checking: {num}")
# Continue statement
print("\n2. CONTINUE STATEMENT:")
print("Printing odd numbers from 1 to 10:")
for num in range(1, 11):
  if num % 2 == 0: # Skip even numbers
     continue
  print(f" {num}")
# Pass statement
print("\n3. PASS STATEMENT:")
print("Demonstration of pass statement:")
for i in range(5):
  if i == 2:
     pass # Placeholder - do nothing
  else:
     print(f" Processing: {i}")
# Break in nested loops
print("\n4. BREAK IN NESTED LOOPS:")
print("Searching for 15 in a 2D pattern:")
found = False
for i in range(1, 6):
  for j in range(1, 6):
     product = i * j
     print(f" {i} x {j} = {product}")
     if product == 15:
          print(" Found 15! Breaking out of loops.")
          found = True
          break
  if found:
     break
# Continue in nested loops
print("\n5. CONTINUE IN NESTED LOOPS:")
print("Skipping multiples of 3:")
for i in range(1, 4):
  print(f" Outer loop: {i}")
  for j in range(1, 6):
          if j % 3 == 0:
            continue
          print(f"   Inner loop: {j}")
def practical_examples():
  print("\n=== PRACTICAL EXAMPLES ===\n")
  # Example 1: Sum of natural numbers
  print("1. SUM OF NATURAL NUMBERS:")
  n = 10
  total = 0
  for i in range(1, n + 1):
    total += i
  print(f" Sum of first {n} natural numbers: {total}")
  # Example 2: Factorial calculation
  print("\n2. FACTORIAL CALCULATION:")
  n=5
  factorial = 1
  for i in range(1, n + 1):
    factorial *= i
  print(f" {n}! = {factorial}")
  # Example 3: Prime number checker
  print("\n3. PRIME NUMBER CHECKER:")
  number = 17
  is_prime = True
  if number < 2:
    is_prime = False
  else:
    for i in range(2, int(number ** 0.5) + 1):
          if number % i == 0:
            is_prime = False
            break
  print(f" {number} is {'prime' if is_prime else 'not prime'}")
  # Example 4: Fibonacci sequence
  print("\n4. FIBONACCI SEQUENCE:")
  n_terms = 8
  a, b = 0, 1
  print(f" First {n_terms} Fibonacci numbers:")
  for i in range(n_terms):
    if i == 0:
          print(f" {a}")
    elif i == 1:
       print(f" {b}")
    else:
       next_term = a + b
       print(f" {next_term}")
       a, b = b, next_term
  # Example 5: Reverse a number
  print("\n5. REVERSE A NUMBER:")
  number = 12345
  reversed_num = 0
  temp = number
  while temp > 0:
    digit = temp % 10
    reversed_num = reversed_num * 10 + digit
    temp //= 10
  print(f" Original: {number}")
  print(f" Reversed: {reversed_num}")
def menu_driven_example():
  print("\n=== MENU-DRIVEN CALCULATOR ===")
  # Simulated menu selections for demonstration
  menu_choices = ['1', '2', '3', '4']
  choice_index = 0
  while choice_index < len(menu_choices):
    print("\n Calculator Menu:")
    print(" 1. Addition")
    print(" 2. Subtraction")
    print(" 3. Multiplication")
    print(" 4. Exit")
    if choice_index < len(menu_choices):
       choice = menu_choices[choice_index]
       print(f" Selected: {choice}")
       choice_index += 1
    else:
       break
    if choice == '4':
       print(" Exiting calculator...")
       break
    elif choice in ['1', '2', '3']:
       a, b = 10, 5 # Sample values
       print(f" Using sample values: a = {a}, b = {b}")
           if choice == '1':
              result = a + b
              print(f" {a} + {b} = {result}")
           elif choice == '2':
              result = a - b
              print(f" {a} - {b} = {result}")
           elif choice == '3':
              result = a * b
              print(f" {a} * {b} = {result}")
      else:
           print(" Invalid choice!")
  if __name__ == "__main__":
    demonstrate_for_loops()
    demonstrate_while_loops()
    demonstrate_control_transfer()
    practical_examples()
    menu_driven_example()
Lab 5: Pattern Printing and String Access
Duration: 2 Hours
Objective
    Learn to print various patterns using loops
    Understand string indexing and slicing operations
Complete Lab 5 Program
  python