Python Assignment– 2

Control Flow Statements

Basic Questions

  1. Read an integer and use an ‘if’ to print ‘positive’ only when the number is greater than zero.
  2. Read an integer and use ‘if-else’ to print ‘even’ or ‘odd’.
  3. Read an integer and use ‘if-elif-else’ to print ‘negative’, ‘zero’, or ‘positive’.
  4. Use a ‘for’ loop with ‘range(1, 6)’ to print numbers from 1 to 5 on one line separated by spaces.
  5. Use a ‘while’ loop to print numbers from 5 down to 1.
  6. Iterate ‘for n in range(1, 11)’ and ‘break’ as soon as ‘n’ equals 7; print the last printed number.
  7. Iterate ‘for n in range(1, 11)’ and ‘continue’ whenever ‘n’ is divisible by 3; print the numbers you actually processed.
  8. Write a placeholder ‘if’ block that currently does nothing using ‘pass’.
  9. Use nested ‘if’ statements to check: an input number is ‘positive’ and also ‘even’; print both conditions met.
  10. Use a nested ‘for’ loop over ‘range(1, 4)’ for rows and ‘range(1, 4)’ for columns to print a 3×3 grid of ‘*’.
  11. Use ‘for ch in “python”‘ and a ‘break’ when ‘ch’ equals ‘h’; print the characters visited.
  12. Use ‘for ch in “python”‘ and ‘continue’ when ‘ch’ is a vowel; print only consonants.
  13. Use ‘for i in range(5)’ with an ‘else’ clause to print ‘done’ after the loop finishes without ‘break’.
  14. Use ‘for i in range(5)’ with a ‘break’ and show that the ‘else’ clause does not execute.
  15. Use a ‘while’ loop that counts up from 1 to 5 and has an ‘else’ that prints ‘completed’ after normal termination.
  16. Iterate with ‘enumerate(“abcd”, start=1)’ and print index and character pairs.
  17. Iterate with ‘zip(“abc”, “123”)’ and print paired characters like ‘a-1’, ‘b-2’, ‘c-3’.
  18. Use two nested ‘for’ loops with ‘range(1, 4)’ to print a right-angled triangle pattern using numbers.
  19. Read two integers and use ‘if-elif-else’ to print which one is larger or if they are equal.
  20. Use a ‘for’ loop to sum numbers from 1 to 10, skipping 4 and 7 using ‘continue’, and print the final sum.

Intermediate Questions

  1. Read three integers and use nested ‘if’ blocks to find and print the largest value without using built-in functions.
  2. Use ‘for i in range(1, 21)’ to print all numbers until the first multiple of 7, then ‘break’ and print ‘stopped at i’.
  3. Use ‘for i in range(1, 21)’ to print all numbers except multiples of 4 using ‘continue’; print the count of printed numbers in the end.
  4. Build a simple menu loop using ‘while True’ that reads a single-character command (‘a’ to add 1 to a counter, ‘r’ to reset, ‘q’ to quit) and uses ‘break’ to exit on ‘q’; print the counter after each command.
  5. Use nested ‘for’ loops to print a multiplication table for numbers 1 to 5 in a grid.
  6. Use ‘for i in range(2, 20)’ to find the first prime candidate by trial dividing with an inner ‘for d in range(2, i)’ and using ‘break’/’else’ to detect primality; print the first prime found.
  7. Iterate ‘for idx, ch in enumerate(“mississippi”)’ and print the first index where ‘ch’ equals ‘s’, then ‘break’; if not found, the ‘else’ of the loop should print ‘not found’.
  8. Given two equal-length strings, iterate with ‘zip’ and count positions where the characters match; print the match count.
  9. Use a ‘while’ loop to read numbers until a zero is entered; use ‘continue’ to ignore negative numbers; print the sum of accepted positive numbers and end with ‘else’ printing ‘done’.
  10. Create a 5×5 hollow square using nested loops where border cells print ‘#’, inner cells print spaces.
  11. Read an integer ‘n’ and print the sum of all even numbers from 1 to ‘n’ using a ‘for’ loop and ‘continue’ to skip odd numbers.
  12. Use ‘for i in range(1, 6)’ and inside it a nested ‘for j in range(1, i + 1)’ to print increasing sequences like ‘1’, ‘1 2’, ‘1 2 3’, etc.
  13. Use ‘while’ to reverse the string ‘abcdef’ by iterating indices from the end to the start; print the reversed result.
  14. Use ‘for a, b in zip(range(1, 6), range(10, 5, -1))’ to print ‘a + b’ for each pair and then the total sum.
  15. Implement a countdown ‘while n > 0’ that prints ‘n’, but ‘break’ if ‘n’ equals 3; ensure the ‘else’ block would print ‘blast off’ only when no ‘break’ happens.
  16. Read a positive integer and use nested ‘if’ blocks to print ‘multiple of 2 and 3’, ‘multiple of 2 only’, ‘multiple of 3 only’, or ‘neither’.
  17. Use a ‘for’ loop to compute the factorial of an input integer; if a negative is provided, print ‘invalid’ using a single ‘if-else’ guard at the top.
  18. With ‘enumerate(“banana”)’, print the indices of all occurrences of ‘a’ in one line separated by commas.
  19. Use ‘zip(range(1, 6), “ABCDE”)’ to print pairs formatted as ‘1-A’, ‘2-B’, etc., and then print them reversed as ‘A-1’, ‘B-2’, etc., by swapping variables in the loop.
  20. Read an integer ‘n’ and use nested loops to print all pairs ‘(i, j)’ where ‘1 <= i < j <= n’.

Advanced Questions

  1. Build a number searcher: read an integer ‘target’ and use ‘for i in range(1, 101)’ to search for ‘target’; ‘break’ when found and print ‘found at i’; if not found, the ‘else’ of the loop should print ‘not found’.
  2. Create a simple password attempt loop: ‘while’ asks for input up to 3 times; ‘break’ on a correct attempt; use ‘else’ of the ‘while’ to print ‘locked’ if all attempts fail.
  3. Use nested loops to generate all triples ‘(a, b, c)’ with ‘1 <= a <= 5’, ‘1 <= b <= 5’, ‘1 <= c <= 5’ where ‘a + b + c == 9’; print the count and the triples.
  4. Print a centered pyramid of height ‘h’ using nested loops: each row uses ‘range’ for spaces and stars; no built-ins for formatting.
  5. Build a mini alignment checker: read two equal-length strings and use ‘for i, (c1, c2) in enumerate(zip(s1, s2))’ to print indices where they differ; if none differ, the ‘else’ on the loop prints ‘identical’.
  6. Generate the first ‘k’ Fibonacci numbers using a ‘for’ loop and tuple-like updates; use a single ‘if’ inside to handle ‘k == 1’; print the sequence on one line.
  7. Simulate a basic parser: read a line and verify balanced parentheses using a counter with a ‘for’ loop; ‘break’ on a negative count; after the loop, use ‘else’ to confirm balance when the counter is zero.
  8. Build a triangular multiplication display using nested loops where each row ‘r’ (1-based) prints products ‘r1′ to ‘rr’; skip printing products that end with digit ‘5’ using ‘continue’.
  9. Create a rectangle border with dimensions ‘h’ and ‘w’ using nested loops; ‘break’ early if either ‘h’ or ‘w’ is less than 2 and print ‘invalid size’; otherwise print the border and then ‘done’ from a loop ‘else’.
  10. Use ‘for idx, ch in enumerate(“abracadabra”)’ to find the second occurrence index of ‘a’ by counting hits; if found, print the index and ‘break’; if there is no second occurrence, print ‘no second a’ from the loop ‘else’.