Python For Loop & While Loop: Beginners' Guide

Published: 21 March 2024
on channel: pythonbuzz
362
like

0:07 why we need loops?
0:57 types of loops in python
1:01 for loop
2:41 range function
4:18 else block in for
6:10 while loop
8:02 else block in while

#for loop#python for loop#for loop in python#python#loops in python#python for beginners#python loops#for loop python#for loops in python#while loop in python#python tutorial for beginners#nested for loop in python#python programming#python while loop#python loops tutorial#for loops#python tutorial#python for loops#for loops python#python basics#python loop#python 3#learn python#python loop input#python loops explained#python while#python while loops#python while loop tutorial#nested while loop in python#python loops tutorial#while loops#python 3#for loops in python#python for beginners#learn python#for loop#python basics#python tutorial for beginners#while loops python#pythonforbeginners #forloop


Python, loops are used to iterate over a sequence of elements, repeating a block of code multiple times. There are mainly two types of loops in Python: `for` loop and `while` loop.

For Loop:

A `for` loop iterates over a sequence (such as a list, tuple, string, or range) and executes a block of code for each element in the sequence.

Syntax:
```python
for item in sequence:
code block to be executed
```

Example:
```python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
```

Output:
```
apple
banana
cherry
```

While Loop:

A `while` loop repeats a block of code as long as a condition is true.

Syntax:
```python
while condition:
code block to be executed
```

Example:
```python
i = 1
while i == 5:
print(i)
i += 1
```

Output:
```
1
2
3
4
5
```

In Python, you can include an `else` block after a `for` loop. The `else` block is executed when the loop completes normally, i.e., it is not terminated by a `break` statement.

Syntax:
```python
for item in sequence:
code block to be executed
else:
code block to be executed after the loop completes normally
```

Example:
```python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
else:
print("No more fruits left.")
```

Output:
```
apple
banana
cherry
No more fruits left.
```

In this example, the `else` block is executed after the `for` loop iterates through all the elements in the `fruits` list without encountering a `break` statement.

It's important to note that the `else` block associated with a `for` loop will not be executed if the loop is terminated prematurely using a `break` statement.

In Python, the `range()` function is used to generate a sequence of numbers. It's commonly used with loops, particularly `for` loops, to iterate over a sequence of numbers a specified number of times.

The syntax of the `range()` function is:

```python
range(start, stop, step)
```

Where:
`start` (optional): The starting value of the sequence. If omitted, it defaults to 0.
`stop` (required): The stopping value of the sequence (exclusive). The sequence will go up to, but not include, this value.
`step` (optional): The step or increment between each number in the sequence. If omitted, it defaults to 1.

Here are a few examples to illustrate how the `range()` function works:

1. Using only the `stop` parameter:
```python
for i in range(5):
print(i)
```
Output:
```
0
1
2
3
4
```

2. Using both `start` and `stop` parameters:
```python
for i in range(2, 7):
print(i)
```
Output:
```
2
3
4
5
6
```

3. Using all parameters (`start`, `stop`, and `step`):
```python
for i in range(1, 10, 2):
print(i)
```
Output:
```
1
3
5
7
9
```

In these examples, the `range()` function generates sequences of numbers that are then iterated over using a `for` loop. It's important to note that the `stop` value is not included in the generated sequence. If you want to include it, you need to specify it as the next number after the desired end value.


In Python, you can also use an `else` block with a `while` loop. Similar to `for` loops, the `else` block associated with a `while` loop is executed when the loop condition becomes false.

Here's the syntax:

```python
while condition:
Code block to be executed repeatedly
else:
Code block to be executed when the loop condition becomes false
```

```python
i = 0
while i == 5:
print(i)
i += 1
else:
print("Loop completed normally")
```

Output:
```
0
1
2
3
4
5
Loop completed normally
```

In this example, the `else` block is executed after the `while` loop completes its iterations without the condition `i ==5` being false.

However, if the `while` loop is terminated prematurely using a `break` statement, the `else` block will not be executed. Here's an example illustrating this:

```python
i = 0
while i == 5:
print(i)
if i == 3:
break
i += 1
else:
print("Loop completed normally")
```

Output:
```
0
1
2
3
```

In this case, the loop terminated prematurely when `i` became 3, so the `else` block is not executed.


On this page of the site you can watch the video online Python For Loop & While Loop: Beginners' Guide with a duration of hours minute second in good quality, which was uploaded by the user pythonbuzz 21 March 2024, share the link with friends and acquaintances, this video has already been watched 362 times on youtube and it was liked by like viewers. Enjoy your viewing!