0% found this document useful (0 votes)
2 views

Python

The document explains various loop statements in Python, including 'continue', 'break', and 'else' in while loops. It also discusses the 'range()' function for generating sequences and compares 'for' and 'while' loops. Additionally, it highlights the significance of iterations for efficient code management and provides examples for clarity.

Uploaded by

ehwut370
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python

The document explains various loop statements in Python, including 'continue', 'break', and 'else' in while loops. It also discusses the 'range()' function for generating sequences and compares 'for' and 'while' loops. Additionally, it highlights the significance of iterations for efficient code management and provides examples for clarity.

Uploaded by

ehwut370
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

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.

- `break`: Exits the loop immediately.


- `continue`: Skips the current iteration and proceeds to the next one.
- `pass`: Does nothing and is often a placeholder.```

Example of `break` and `continue`:


```python Output:
for i in range(5): ```
if i == 3: 0
break # stops the loop when i equals 3 2
elif i == 1: ```
continue # skips printing when i equals 1 Here, the loop stops completely when `i` reaches 3 due to
`break`, and `continue` skips printing `i` when it equals 1.
print(i)
2. Convert `for` to `while
B. Convert the following loops as directed
Original Code:
1. **Convert `while` to `for`** ```python
for x in range(5, 20, 5):
Original Code: print(x)
```python
x=5
while(x < 10):
print(x + 10)
x += 2
```

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

while (Num < 10):


Num += 1
if Num == 5:
break
print(Num, end="")
```

**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)
```

You might also like