Chapter 8
Chapter 8
Loops in Python
Sometimes there may be a need to repeat a set of statements more than once based on a
certain condition. This process of repetition is called a loop or iteration in programming.
For example:
FOR ... loop WHILE ... loop
FOR loop
The FOR ... structure is used to repeat a loop a specific number of times. It uses a counter
variable which is incremented or decremented with each repetition of the loop.
WHILE loop
A WHILE loop executes a block of code repeatedly as long as the test/control condition of the
loop is true. It is useful when the number of iterations are not known prior to the execution of the
loop.
Program 2: Write a program using the FOR loop to print the first ten multiples of 2.
Program 3: Write a program using the FOR loop to print the first ten natural numbers in reverse
order.
Program 4: Write a program using the FOR loop to print the multiplication table of a number N.
In the above example, the value of the variable N is accepted from the user. The sample
program is executed for the input value N=5.
Program 5: Write a program using the FOR loop to print X raised to the power of Y.
Sum = 0
for i in range(1, 11):
N = int(input("Enter a number: "))
Sum += N
print("The sum of 10 numbers is:", Sum)
Program 7: Write a program using the FOR loop to display factors of number N.
N = int(input("N="))
Fact = 1
for Count in range(1, N + 1):
Fact *= Count
print(N, '!=', Fact)
Program 9: Write a program using the FOR loop with else to print multiples of 2.
Program 10: Write a program using the WHILE loop to print the first five natural numbers.
Count = 1
while Count <= 5:
print(Count, end=", ")
Count = Count + 1
Program 11: Write a program using the WHILE loop to display the digits of a number in reverse
order.
Num=int(input(“Number”))
Count=1
While Num>0:
R=Num%10
print(R)
Num//=10
Program 12: Write a program using the WHILE loop to display the factors of a number.
Program 13: Write a program using the WHILE loop to display the Fibonacci series to the Nth
term, i.e. 0,1,1, 2, 3, 5, . .. , N.
Program 15: Write a program to implement the WHILE loop with break.
i=1
while i <= 5:
print(i)
if i == 3:
break
i=i+1
print("The end")
Program 16: Write a program to implement the WHILE loop with continue.
i=1
while i < 5:
i += 1
if i == 3:
continue
print(i)
print("The end")
.Activity
1.
For x in range(1,11)
Print x
End
Ans:
for x in range(1, 11):
print(x)
2.
Num==1
While
Num<=5)
print(Num*5)
Num+1=Num
Ans:
Num = 1
while Num <= 5:
print(Num * 5)
Num += 1
3.
Num=int("Enter Num=")
for I in range[0,11]
if x=y
print x+y
else
print x-y
Ans:
Num = int(input("Enter Num="))
for i in range(0, 11):
if x == y:
print(x + y)
else:
print(x - y)
.Exercise
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 are not known prior to the execution
of the loop.
Ans:
for x in range(, 10, 2):
print(x + 10)
2. for = while
for x in range(5, 20, 5):
print(x)
Ans:
x = 5 while x < 20:
print(x)
x += 5
C. Give the output for the following codes.
1.
Num = 0
while (Num < 10):
Num += 1
if Num == 5:
break
print(Num, end=",")
Ans: 1,2,3,4,
2.
for val in "String":
if val == "I":
break
print(val)
print("Over")
Ans:
S
t
r
i
n
g
Over
D. Write Python programs for the following.
1. Write a program to accept a number from the user and display if it is a palindrome or
not. [Hint: A palindrome number is where the reverse of the number is the same as the
number itself.]
Input
Num=1221
Num=231
Output
Palindrome Number
Not a Palindrome Number
Ans:
Input
n = int(input("Enter number: "))
temp = n
rev = 0
while n > 0:
dig = n % 10
rev = rev * 10 + dig
n = n // 10
if temp == rev:
print("Palindrome Number")
else:
print("Not a Palindrome Number")
Ans:
n = int(input("Enter number: ")) # Fixed quotes
sum = 0
3. Write a program to accept a number from the user and check if it ends with 4 or 8. If it
ends with 4, print "Ends with 4", if it ends with 8, print "Ends with 8", otherwise print
"Ends with neither".
Ans:
n = int(input("Enter number:"))
mod = n % 10
if mod == 8:
print("ends with 8")
elif mod == 4:
print("ends with 4")
else:
print("ends with neither")
4. Write a program to calculate and print the sum of even and odd integers of the first N
natural numbers.
Ans:
n = int(input("Enter a number: "))
sum1 = 0
while(n > 0):
sum1 = sum1 + n
n=n-1
print("The sum of first n natural numbers is", sum1)
E. Answer the following questions.
def sum_series_4(n):
return sum(math.factorial(i) for i in range(1, n + 1))