The purpose of a for loop is to retrieve or extract data from any iterable object.
for varname in iterable_object:
# Block of statements
# Other statements in the program
for varname in iterable_object:
# Block of statements
else:
# Else block of statements
# Other statements in the program
# Write a program to find given number is prime or not
num = int(input("enter the given number = "))
for var in range(2, num):
if num % var == 0:
print("Not Prime number")
else:
print("Prime Number")
# Write a program to find the factorial of a given number
number = int(input("enter the given number = "))
fact = 1
for i in range(1,number+1):
fact = fact*i
print(fact)
The keywords for, in, and else are used.
varname is a programmer-defined variable.
An iterable_object is any object containing multiple values, such as a sequence (string, tuple), list, set, or dictionary.
The execution process of a for loop involves selecting each element from the iterable_object, assigning it to varname, and executing the block of statements. This process repeats for a finite number of times (equal to the length of the iterable_object) until all elements are processed.
When all elements of the iterable_object are processed, the for loop condition becomes false. If an else block is present, its statements are executed, followed by other statements in the program.
The else block is optional.
Note 1: For iterating over iterable objects, it is recommended to use a for loop.
Note 2: For repeating non-iterable objects, it is recommended to use a while loop.
# string
str = "Hello"
for var in str:
print(var)
# Write a program to generate 1 to 20 even number
num = int(input("Enter the given number = "))
for var in range(0,num,2):
print(var, end = " ")
# Write a program to a given number is a palindrome or not
num = int(input("Enter the given number = "))
if num < 0:
print("Please enter a non-negative number.")
else:
len1 = len(str(num))
reversed_num = 0
num1 = num
for i in range(len1):
d = num1 % 10
reversed_num = reversed_num * 10 + d
num1 = num1 // 10
if reversed_num == num:
print("The given number is a palindrome.")
else:
print("The given number is not a palindrome.")
# Write a program to generate numbers from 1 to 10
num = int(input("enter the number = "))
for var in range(10):
print(var)
A nested for loop is a for loop inside another for loop, where the inner loop runs completely for each iteration of the outer loop. With strings, nested for loops are used to
Compare characters (e.g., finding duplicates).
Generate patterns (e.g., printing substrings).
Process combinations (e.g., anagrams or string matching).
for var1 in iterable_object1:
# Outer loop block
for var2 in iterable_object2:
# Inner loop block
# Optional outer else
else:
# Optional outer else block
# Other statements
# Write a program to generate 1 to 100 even number
for i in range(2,101):
for j in range(2,i):
if i%j == 0:
break
else:
print(i,end = " ")
# Write a program to find the
num = int(input("Enter the number = "))
If the test condition is True, then execute the block of statements, and once again evaluate the test condition. If it’s True, execute the block of statements again. This process will be repeated a finite number of times until the test condition becomes false.
If the test condition becomes false, then execute the else block of statements (if we write one) and also execute other statements in the program.
Writing the else block is optional.
When working with while loops in Python, ensure the following three components are included.
Initialization part
Conditional part
Updation part (increment or decrement)
# syntax
while condition:
# Block of statements to execute as long as the condition is True
statement1
statement2
# ...
else:
# Optional block of statements to execute when the condition becomes False
statement3
statement4
# ...
count = 1
while count <= 3:
print(f"Count is {count}")
count += 1
else:
print("Loop finished!")
# Write a program to generate even numbers
number = 2
while number <= 10:
print(number)
number += 2
else:
print("Finished printing even numbers!")
# prime number
number = 10
is_prime = True
divisor = 2
while divisor <= number // 2:
if number % divisor == 0:
is_prime = False
break
divisor += 1
if number > 1 and is_prime:
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")