.. _module2_assignment: Python Assignment Questions: Decision Making and Looping ======================================================== **1. Age Checker** Write a program that takes an age as input and prints "Adult" if age >= 18, otherwise "Minor". *Hint:* Use `if-else` statement. **2. Even or Odd** Take a number as input and print "Even" if it's even, "Odd" if it's odd. *Hint:* Use modulo operator `%` with `if-else`. **3. Grade Calculator** Input a score (0-100) and print the grade: A (90-100), B (80-89), C (70-79), D (60-69), F (below 60). *Hint:* Use `if-elif-else` chain. **4. Positive, Negative, or Zero** Take a number and print whether it's positive, negative, or zero. *Hint:* Use nested `if` statements or `elif`. **5. Sum of First N Numbers** Input N and print the sum of first N natural numbers using a loop. *Hint:* Use `for` loop with `range()`. **6. Factorial Calculation** Take a number N and calculate its factorial using a loop. *Hint:* Use `while` loop or `for` loop, initialize result = 1. **7. Multiplication Table** Input a number and print its multiplication table from 1 to 10. *Hint:* Use `for` loop with `range(1, 11)`. **8. Count Digits** Take a number and count how many digits it has using a loop. *Hint:* Use `while` loop dividing by 10. **9. Sum of Even Numbers** Input N and print sum of all even numbers from 1 to N. *Hint:* Use `for` loop with `if` to check even. **10. Reverse a String** Take a string and print it in reverse using a loop. *Hint:* Use `for` loop iterating backwards or build new string. **11. Prime Number Check** Take a number and check if it's prime. *Hint:* Use loop to check divisibility, use `break` if divisible. **12. Fibonacci Series** Input N and print first N Fibonacci numbers. *Hint:* Use loop to generate sequence. **13. List Iteration with Index** Take a list of names and print each with its index. *Hint:* Use `enumerate()` function. **14. Zip Two Lists** Take two lists of equal length and print pairs. *Hint:* Use `zip()` function. **15. Dictionary from Two Lists** Take two lists (keys and values) and create a dictionary. *Hint:* Use `zip()` and `dict()`. **16. Loop with Else** Search for an item in a list and print message if not found. *Hint:* Use `for` loop with `else`. **17. Break on Condition** Print numbers from 1 to 100, but stop when reaching a multiple of 7. *Hint:* Use `for` loop with `if` and `break`. **18. Continue Example** Print numbers from 1 to 10, skipping multiples of 3. *Hint:* Use `for` loop with `if` and `continue`. **19. Pass Statement** Create a loop that does nothing for certain conditions. *Hint:* Use `pass` in `if` block. **20. List Comprehension** Create a list of squares of numbers from 1 to 10. *Hint:* Use list comprehension `[x**2 for x in range(1, 11)]`. **21. Even Numbers Comprehension** Create a list of even numbers from 1 to 20 using comprehension. *Hint:* Add condition `if x % 2 == 0`. **22. Dictionary Comprehension** Create a dictionary with numbers as keys and their squares as values. *Hint:* Use `{x: x**2 for x in range(1, 6)}`. **23. String Lengths** Take a list of strings and create a list of their lengths. *Hint:* Use list comprehension with `len()`. **24. Nested Loop Pattern** Print a pattern of stars using nested loops. *Hint:* Use two `for` loops, one for rows, one for columns. **25. Matrix Operations** Take two 2x2 matrices and print their sum. *Hint:* Use nested loops to iterate through elements. **26. Exception Handling Division** Take two numbers and divide them, handle division by zero. *Hint:* Use `try-except` with `ZeroDivisionError`. **27. ValueError Handling** Take input and convert to int, handle invalid input. *Hint:* Use `try-except` with `ValueError`. **28. File Reading with Exception** Try to read a file and handle if it doesn't exist. *Hint:* Use `try-except` with `FileNotFoundError`. **29. Multiple Exceptions** Take two numbers, divide, handle both ValueError and ZeroDivisionError. *Hint:* Use multiple `except` blocks. **30. Finally Block** Open a file, read content, and ensure it's closed. *Hint:* Use `try-except-finally`.