Python
Python
The ________ statement skips the rest of the loop statements and causes the next iteration of the loop to take
place.
2. The ________ statement enables a program to skip over a part of the ________.
3. The ________ part of the while loop is executed only when the while loop completes all its iterations.
4. Condition is checked for True or False, and the statements are executed only if the condition is ________.
5. The ________ loop is helpful when the number of iterations are not known prior to the execution of the loop.
1. The **`continue`** statement skips the rest of the loop statements and causes the next iteration of the loop to
take place.
2. The **`break`** statement enables a program to skip over a part of the code.
3. The **`else`** part of the while loop is executed only when the while loop completes all its iterations.
4. Condition is checked for True or False, and the statements are executed only if the condition is **True**.
5. The **`while`** loop is helpful when the number of iterations is not known prior to the execution of the loop.
2. Explain the `range()` function with the help of an example.
The `range()` function generates a sequence of numbers, typically used for looping a specific number of times. It can
be used with one, two, or three arguments: `range(stop)`, `range(start, stop)`, and `range(start, stop, step)`.
Example: Output:
```python ```
for i in range(1, 6): 1
print(i) 2
``` 3
4
5
In this example, `range(1, 6)` generates numbers from 1 to 5
(stopping before 6).
`while` loop: Used when the number of iterations is not
3. Explain the difference between `for` and `while`
known in advance. It continues as long as a condition remains
loops.
true.
`for` loop: Used when the number of iterations is
Example of a `while` loop:
known, or when iterating over a sequence. It is concise
and straightforward for iterating through elements. ```python
Example of a `for` loop: i=0
```python while i < 5:
for i in range(5): print(i)
print(i) i += 1
``` ```
Output: Output:
``` ```
0 0
1 1
2 2
3 3
4 4
4. What are jump statements?
Jump statements control the flow of loops by "jumping" over specific sections based on conditions.
Converted Code:
Converted Code:
```python
```python
x=5
for x in range(5, 10, 2):
while x < 20:
print(x + 10)
print(x)
```
x += 5
**C. Give the output for the following codes**
1. **Code:**
```python
Num = 0
**Output:**
```
1,2,3,4
```
2. **Code:**
```python
for val in "String":
if val == "l":
break
print(val)
print("Over")
```
**Output:**
```
S
t
r
i
n
g
Over
```
1. What is the significance of using iterations in Python?
Iterations in Python allow repetitive execution of a set of instructions, making tasks more efficient and the code easier to
manage. They are especially useful when working with large data sets or performing repetitive tasks, like processing lists or
generating sequences.
Example: Output:
```python ```
numbers = [1, 2, 3, 4, 5] Sum of numbers: 15
total = 0
for number in numbers:
total += number
print("Sum of numbers:", total)
```