---
title: Introduction
description:
duration: 200
card_type: cue_card
---
# **Iteration - For loops**
[Class
recording](https://www.scaler.com/meetings/i/beginner-iteration-for-loops-6/archive)
[lecture
notes](https://drive.google.com/file/d/1vSpvqKFa7lOixxlQ4gY_sdtS6rQlt0IA/view?usp=share_link)
## **Content**
1. Need for `for` loops
2. list
3. range()
4. Jumps in range()
## **Meanwhile when people are joining (5-7 mins)**
- Instructor's welcome.
- Breaking ice with students.
- casual conversation to bond.
---
title: Motivation for `for` loop
description:
duration: 200
card_type: cue_card
---
## **Need for `for` loops**
- While loop will run infinitely when we donot give an exit condition

- Instead of a condition, we need a loop that runs for a range

- While loop ends when condition becomes false, a for loop ends when
the range is exhausted.
``` python
a = input()
while a:
if len(a) == 6:
a = ""
print("END!")
```
---
title: List
description:
duration: 200
card_type: cue_card
---
### List
- Multiple values stored in the same variable, in other words, a
collection of variables.

---
title: Explaining range in Python
description:
duration: 200
card_type: cue_card
---
## `range()`

- `[]` are inclusive

- `()` exclusive
- `range()` has mixed range, it incldes start, but excludes end.

- Goes from start to end-1 , \[start, end)
- If start is not given, 0 is start
- To see all the numbers in the range, use list
- Length of range is end-start
- Range does not work with float numbers.
Code
``` python=
print(list(range(5)))
```
>Output
```bash=
[0, 1, 2, 3, 4]
```
Code
``` python=
print(list(range(2,10)))
```
>Output
```bash=
[2, 3, 4, 5, 6, 7, 8, 9]
```
- Works with negative ranges
Code
``` python=
print(list(range(-1,3)))
```
>Output
```bash=
[-1, 0, 1, 2]
```
``` python=
print(list(range(-9, -1)))
```
>Output
```bash=
[-9, -8, -7, -6, -5, -4, -3, -2]
```
- By default increment is 1, therefore
``` python=
print(list(range(-1,-9)))
```
>Output
```bash=
[]
```

---
title: Jumps or difference in ranges
description:
duration: 300
card_type: cue_card
---
### **Jumps or difference in ranges**
- range(start,end,jump)

Code
``` python=
print(list(range(2,10,2)))
```
>Output
```bash=
[2, 4, 6, 8]
```

Code
``` python=
print(list(range(1,30,3)))
```
>Output
```bash=
[1, 4, 7, 10, 13, 16, 19, 22, 25, 28]
```
- Using for loop in reverse by jumping with negative value

Code
``` python=
print(list(range(-1,-9,-1)))
```
>Output
```bash=
[-1, -2, -3, -4, -5, -6, -7, -8]
```
Code
``` python=
print(list(range(2,5,10)))
```
>Output
```bash=
[2]
```
Code
``` python=
print(list(range(5,-10,-2)))
```
>Output
```bash=
[5, 3, 1, -1, -3, -5, -7, -9]
```
---
title: Quiz-1
description: Quiz-1
duration: 60
card_type: quiz_card
---
# Question
What will be the output of `print(list(range(7, 8)))`?
# Choices
- [x] [7]
- [ ] [7, 8]
- [ ] []
---
title: Quiz-2
description: Quiz-2
duration: 60
card_type: quiz_card
---
# Question
What will be the output of `print(list(range(8, 8)))`?
# Choices
- [ ] [7]
- [ ] [7, 8]
- [x] []
---
title: Quiz-3
description: Quiz-3
duration: 60
card_type: quiz_card
---
# Question
How many numbers are included in the below range?
``` python=
range(1, 11, 2)
```
# Choices
- [ ] 4
- [x] 5
- [ ] 6
- [ ] 7
---
title: Quiz-4
description: Quiz-4
duration: 60
card_type: quiz_card
---
# Question
How many numbers are included in the below range?
``` python=
range(11)
```
# Choices
- [x] 11
- [ ] 10
- [ ] 12
---
title: Revising previous concepts
description:
duration: 600
card_type: cue_card
---
## **Question**: Calculate sum of all numbers from 1 to N using a for loop.
Code
``` python=
number = int(input("Please enter number - "))
final_sum = 0
for value in range(1, number + 1):
final_sum += value
print(final_sum)
```
>Output
```bash=
Please enter number - 6
21
```
## **Question**: Print all numbers from an input N to 1 where N \> 1.
Code
``` python=
number = int(input("Enter the number - "))
for i in range(number, 0, -1):
print(i)
```
>Output
```bash=
Enter the number - 5
5
4
3
2
1
```
## **Question**: Print all numbers from an input N to 1 where N \> 1 using for loop. Solve this without using a negative jump!
Code
``` python=
n = int(input("Enter the number - "))
for i in range(1, n+1):
print(n-i+1)
```
>Output
```bash=
Enter the number - 5
5
4
3
2
1
```
OR
Code
``` python=
n = int(input("Enter the number - "))
for i in range(-n, 0):
print(-i)
```
>Output
```bash=
Enter the number - 5
5
4
3
2
1
```