- While Loop
- For Loop
- Nested Loops
- Break and Continue
- Loop Else Clause
- List Comprehension
- Summary
- Tasks
The while
loop repeatedly executes a block of code as long as a specified condition is true.
i = 1
while (i <= 5):
print(f"{i}. Python")
i += 1
print("END")
Output:
1. Python
2. Python
3. Python
4. Python
5. Python
END
n = int(input("n: ")) # 4
i = 1
sum = 0
while(i <= n):
sum += i
i += 1
print(sum)
Output:
Enter n: 4
10
s = input("S: ")
i = 0
while(i < len(s)):
print(s[i])
i += 1
Output:
Enter S: Python
P
y
t
h
o
n
h = int(input("H: "))
row_no = 1
while(row_no <= h):
print('*' * row_no)
row_no += 1
Output:
Enter H: 4
*
**
***
****
n = int(input("N: "))
i = 1
f = 1
while(i <= n):
f = f * i
i += 1
print(f)
Output:
Enter N: 5
120
n = int(input("N: "))
divisor_count = 0
i = 1
while(i <= n):
if(n % i == 0):
divisor_count += 1
i += 1
if(divisor_count == 1):
print('Neither prime nor composite')
else:
print('Prime' if divisor_count == 2 else 'Composite')
Output:
Enter N: 7
Prime
The for
loop iterates over a sequence (such as a list, tuple, or string) and executes a block of code for each item in the sequence.
l = [1, 2, 3]
for a in l:
print(a)
Output:
1
2
3
d = {
'(1)': 'one',
'(2)': 'two',
'(3)': 'three'
}
for a in d:
print(a, d[a])
Output:
(1) one
(2) two
(3) three
s = "Python"
for a in s:
print(a)
Output:
P
y
t
h
o
n
The range
function generates a sequence of numbers, which is useful for iterating a specific number of times.
for i in range(1, 10):
print(i)
Output:
1
2
3
4
5
6
7
8
9
n = int(input('N: '))
is_prime = True
for i in range(2, n):
if(n % i == 0):
is_prime = False
print('Prime' if is_prime else 'Composite')
Output:
Enter N: 7
Prime
You can use loops inside other loops. These are called nested loops.
for i in range(1, 5):
for j in range(1, 4):
print(i, j)
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
A matrix is a two-dimensional array. You can use nested loops to iterate over each element.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
for i in range(len(matrix)):
for j in range(len(matrix[0])):
print(f"matrix[{i}][{j}] =", matrix[i][j])
Output:
matrix[0][0] = 1
matrix[0][1] = 2
matrix[0][2] = 3
matrix[1][0] = 4
matrix[1][1] = 5
matrix[1][2] = 6
matrix[2][0] = 7
matrix[2][1] = 8
matrix[2][2] = 9
The break
statement is used to exit a loop prematurely, and the continue
statement is used to skip the current iteration and continue with the next iteration.
for i in range(1, 11):
print(i)
if(i == 4):
break
print("after break")
Output:
1
after break
2
after break
3
after break
4
for i in range(1, 11):
if(i % 2 == 0):
continue
print(i)
Output:
1
3
5
7
9
n = int(input("N: "))
for i in range(2, n):
if(n % i == 0):
print("COMPOSITE")
break
else:
print("PRIME")
Output:
Enter N: 7
PRIME
The else
clause in a loop is executed when the loop completes normally (i.e., not terminated by a break
statement).
i = 1
while(i <= 10):
print(i)
if(i == 5):
break
i += 1
else:
print("ELSE")
Output:
1
2
3
4
5
for i in range(1, 6):
print(i)
if(i == 3):
break
else:
print("ELSE")
Output:
1
2
3
List comprehension provides a concise way to create lists. It consists of brackets containing an expression followed by a for
clause.
l = [i for i in range(1, 11)]
print('LIST:', l)
Output:
LIST: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
You can add an if
condition to filter items.
numbers = [4, 30, 23, 36, 87, 59, 102, 114, 56, 78, 26]
new_list = [i for i in numbers if i % 2]
print(new_list)
Output:
[23, 87, 59]
In this chapter, we covered while loops, for loops, nested loops, break and continue statements, the loop else clause, and list comprehensions.
- Write a program that prints the first 10 natural numbers using a while loop.
- Write a program that prints the multiplication table of a given number using a for loop.
- Write a program that uses nested loops to print a pattern.
- Write a program that demonstrates the use of break and continue statements.
- Write a program that uses list comprehension to create a list of squares of the first 10 natural numbers.