|
1 | | -## Nested Loops in Python |
| 1 | +## Nested If/Else Conditions in Python |
2 | 2 |
|
3 | | -A nested loop refers to a loop that exists within another loop. It is commonly used to perform operations on multi-dimensional structures, such as matrices or grids. |
| 3 | +A nested `if/else` condition refers to an `if` or `else` statement inside another `if` or `else` block. This allows for more complex decision-making processes by adding additional conditions within existing ones. |
4 | 4 |
|
5 | | -### Example of a Nested Loop |
| 5 | +### Example of Nested If/Else Conditions |
6 | 6 |
|
7 | | -Below is a basic example of a nested loop in Python: |
| 7 | +Consider the following example to categorize a number: |
8 | 8 |
|
9 | 9 | ```python |
10 | | -for i in range(3): # Outer loop |
11 | | - for j in range(2): # Inner loop |
12 | | - print(f"i = {i}, j = {j}") |
| 10 | +number = int(input("Enter a number: ")) |
| 11 | + |
| 12 | +if number >= 0: |
| 13 | + if number == 0: |
| 14 | + print("The number is zero.") |
| 15 | + elif number > 0 and number <= 10: |
| 16 | + print("The number is between 1 and 10.") |
| 17 | + elif number > 10 and number <= 100: |
| 18 | + print("The number is between 11 and 100.") |
| 19 | + else: |
| 20 | + print("The number is greater than 100.") |
| 21 | +else: |
| 22 | + print("The number is negative.") |
13 | 23 | ``` |
| 24 | +## Practice Questions for Nested If/Else |
| 25 | + |
| 26 | +### 1. Grade Classification |
| 27 | + |
| 28 | +Write a Python program that classifies a student's grade based on the score: |
| 29 | + |
| 30 | +- If the score is 90 or above, print `"Excellent"`. |
| 31 | +- If the score is between 80 and 89, print `"Good"`. |
| 32 | +- If the score is between 70 and 79, print `"Fair"`. |
| 33 | +- If the score is below 70, print `"Needs Improvement"`. |
| 34 | + |
| 35 | +### 2. Age Group |
| 36 | + |
| 37 | +Write a Python program to determine the age group based on the given age: |
| 38 | + |
| 39 | +- If the age is less than 13, print `"Child"`. |
| 40 | +- If the age is between 13 and 19, print `"Teenager"`. |
| 41 | +- If the age is between 20 and 59, print `"Adult"`. |
| 42 | +- If the age is 60 or above, print `"Senior"`. |
| 43 | + |
| 44 | +### 3. Month Days |
| 45 | + |
| 46 | +Write a Python program that determines the number of days in a month: |
| 47 | + |
| 48 | +- Input the month as an integer (1 for January, 2 for February, etc.). |
| 49 | +- Use nested `if/else` to check whether the month is January, February, or has 30 or 31 days. |
| 50 | +- Output the number of days in the month. |
| 51 | + |
| 52 | + |
| 53 | + |
14 | 54 | ## Nested Loops in Python |
15 | 55 |
|
16 | 56 | A nested loop is a loop that exists inside another loop. This technique is particularly useful for iterating over multi-dimensional structures such as matrices or grids. Each loop in the nested structure is referred to as an "inner loop" and an "outer loop." |
17 | 57 |
|
18 | 58 | ### Concept |
19 | 59 |
|
20 | | -- **Outer Loop:** This is the loop that contains one or more inner loops. It controls the overall iteration and determines how many times the inner loops will be executed. |
21 | | -- **Inner Loop:** This loop is nested inside the outer loop and is executed for each iteration of the outer loop. It performs operations on each element of the structure as defined by the outer loop. |
| 60 | +- **Outer Loop:** This loop contains one or more inner loops and controls the overall iteration, determining how many times the inner loops will be executed. |
| 61 | +- **Inner Loop:** This loop is nested inside the outer loop and is executed for each iteration of the outer loop. It performs operations on each structure element as defined by the outer loop. |
22 | 62 |
|
23 | | -### Example |
| 63 | +### Example of Nested Loops |
24 | 64 |
|
25 | | -Consider the following example of nested loops to print values in a 2D grid: |
| 65 | +Consider the following example to print values in a 2D grid: |
26 | 66 |
|
27 | 67 | ```python |
28 | 68 | for i in range(3): # Outer loop |
29 | 69 | for j in range(2): # Inner loop |
30 | 70 | print(f"i = {i}, j = {j}") |
31 | | -``` |
| 71 | +``` |
| 72 | + |
| 73 | +## Practice Questions for Nested Loops |
| 74 | + |
| 75 | +### 1. Multiplication Table |
| 76 | + |
| 77 | +Write a Python program to generate a multiplication table from 1 to 10. Use nested loops to print the table: |
| 78 | + |
| 79 | +- The outer loop should iterate through numbers 1 to 10. |
| 80 | +- The inner loop should iterate through numbers 1 to 10, and for each pair, print the product. |
| 81 | + |
| 82 | +### 2. Number Pyramid |
| 83 | + |
| 84 | +Write a Python program to create a number pyramid. For a given number `n`, print a pyramid with `n` levels: |
| 85 | + |
| 86 | +- For `n = 4`, the output should be: |
| 87 | + |
| 88 | + |
| 89 | +### 3. Pattern of Stars |
| 90 | + |
| 91 | +Write a Python program to print a pattern of stars in the following format: |
| 92 | + |
| 93 | +- For `n = 5`, the output should be: |
| 94 | + |
| 95 | + |
| 96 | +### 4. Chessboard Pattern |
| 97 | + |
| 98 | +Write a Python program to print a chessboard pattern of size `n x n`: |
| 99 | + |
| 100 | +- For `n = 8`, the output should be: |
| 101 | + |
| 102 | + |
| 103 | +### 5. Sum of Multiples |
| 104 | + |
| 105 | +Write a Python program to calculate the sum of multiples of 3 and 5 up to a given number `n`: |
| 106 | + |
| 107 | +- Use nested loops where the outer loop iterates through numbers up to `n` and the inner loop checks if a number is a multiple of 3 or 5. Sum these multiples. |
0 commit comments