Python Assignment– 2
Control Flow Statements
Basic Questions
- Read an integer and use an ‘if’ to print ‘positive’ only when the number is greater than zero.
- Read an integer and use ‘if-else’ to print ‘even’ or ‘odd’.
- Read an integer and use ‘if-elif-else’ to print ‘negative’, ‘zero’, or ‘positive’.
- Use a ‘for’ loop with ‘range(1, 6)’ to print numbers from 1 to 5 on one line separated by spaces.
- Use a ‘while’ loop to print numbers from 5 down to 1.
- Iterate ‘for n in range(1, 11)’ and ‘break’ as soon as ‘n’ equals 7; print the last printed number.
- Iterate ‘for n in range(1, 11)’ and ‘continue’ whenever ‘n’ is divisible by 3; print the numbers you actually processed.
- Write a placeholder ‘if’ block that currently does nothing using ‘pass’.
- Use nested ‘if’ statements to check: an input number is ‘positive’ and also ‘even’; print both conditions met.
- Use a nested ‘for’ loop over ‘range(1, 4)’ for rows and ‘range(1, 4)’ for columns to print a 3×3 grid of ‘*’.
- Use ‘for ch in “python”‘ and a ‘break’ when ‘ch’ equals ‘h’; print the characters visited.
- Use ‘for ch in “python”‘ and ‘continue’ when ‘ch’ is a vowel; print only consonants.
- Use ‘for i in range(5)’ with an ‘else’ clause to print ‘done’ after the loop finishes without ‘break’.
- Use ‘for i in range(5)’ with a ‘break’ and show that the ‘else’ clause does not execute.
- Use a ‘while’ loop that counts up from 1 to 5 and has an ‘else’ that prints ‘completed’ after normal termination.
- Iterate with ‘enumerate(“abcd”, start=1)’ and print index and character pairs.
- Iterate with ‘zip(“abc”, “123”)’ and print paired characters like ‘a-1’, ‘b-2’, ‘c-3’.
- Use two nested ‘for’ loops with ‘range(1, 4)’ to print a right-angled triangle pattern using numbers.
- Read two integers and use ‘if-elif-else’ to print which one is larger or if they are equal.
- Use a ‘for’ loop to sum numbers from 1 to 10, skipping 4 and 7 using ‘continue’, and print the final sum.
Intermediate Questions
- Read three integers and use nested ‘if’ blocks to find and print the largest value without using built-in functions.
- Use ‘for i in range(1, 21)’ to print all numbers until the first multiple of 7, then ‘break’ and print ‘stopped at i’.
- 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.
- 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.
- Use nested ‘for’ loops to print a multiplication table for numbers 1 to 5 in a grid.
- 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.
- 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’.
- Given two equal-length strings, iterate with ‘zip’ and count positions where the characters match; print the match count.
- 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’.
- Create a 5×5 hollow square using nested loops where border cells print ‘#’, inner cells print spaces.
- 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.
- 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.
- Use ‘while’ to reverse the string ‘abcdef’ by iterating indices from the end to the start; print the reversed result.
- 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.
- 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.
- 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’.
- 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.
- With ‘enumerate(“banana”)’, print the indices of all occurrences of ‘a’ in one line separated by commas.
- 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.
- Read an integer ‘n’ and use nested loops to print all pairs ‘(i, j)’ where ‘1 <= i < j <= n’.
Advanced Questions
- 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’.
- 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.
- 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.
- Print a centered pyramid of height ‘h’ using nested loops: each row uses ‘range’ for spaces and stars; no built-ins for formatting.
- 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’.
- 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.
- 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.
- 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’.
- 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’.
- 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’.