Python Day- 5
(RECAP OF PREVIOUS DAY)
Purpose and types of loops, while loop, for loop, else with loops, Nested loops, break,
continue, and pass statements. Problems on above concepts.
1. Purpose of Loops
Loops are used to execute a block of code multiple times without rewriting it. They:
● Reduce redundancy.
● Improve code readability and efficiency.
● Allow iteration over sequences (e.g., lists, tuples, strings) and perform repetitive tasks.
2. Types of Loops in Python
a. While Loop
● Executes a block of code as long as the condition is True.
Syntax:
while condition:
# code block
Example:
count = 1
while count <= 5:
print(count)
count += 1
b. For Loop
● Iterates over a sequence (e.g., list, tuple, dictionary, string, or range).
Syntax:
for item in sequence:
# code block
Example:
for i in range(1, 6):
print(i)
3. else with Loops
● An else block can be used with loops.
● It executes after the loop finishes normally (i.e., without being terminated by a break).
Syntax:
for item in sequence:
# code block
else:
# code after loop
Example:
for i in range(5):
print(i)
else:
print("Loop completed!")
4. Nested Loops
● Loops inside other loops.
● Useful for working with multidimensional data structures.
Example:
for i in range(3): # Outer loop
for j in range(2): # Inner loop
print(f"i={i}, j={j}")
5. Special Loop Control Statements
a. Break Statement
● Exits the loop immediately, skipping the remaining iterations.
Example:
for i in range(5):
if i == 3:
break
print(i)
# Output: 0, 1, 2
b. Continue Statement
● Skips the current iteration and moves to the next one.
Example:
for i in range(5):
if i == 3:
continue
print(i)
# Output: 0, 1, 2, 4
c. Pass Statement
● A placeholder that does nothing and is used when a statement is syntactically required.
Example:
for i in range(5):
if i == 3:
pass # Placeholder for future code
print(i)
Problems on the Above Concepts
1. Print all numbers from 1 to 10 using a while loop.
n = 1
while n <= 10:
print(n)
n += 1
2. Print all numbers from 1 to 10 using a for loop.
for i in range (1,11):
print (i)
3. Iterate over a list of fruits using a for loop.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
4. Use else with a for loop.
for i in range(3):
print(i)
else:
print("Loop completed successfully!")
5. Nested loops to print a pattern.
for i in range(1, 7):
for j in range(1, i + 1):
print("*", end="")
print() # Newline after each row
Or
#Single loop
rows = int(input("Enter a number: "))
for i in range(1, rows + 1):
print("*" * i)
6. Nested loops to print a pattern.
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
print()
7. Nested loops to print a pattern.
rows = 5 1
num = 1 2 3
for i in range(1, rows + 1): 4 5 6
for j in range(1, i + 1): 7 8 9 10
print(num, end=" ") 11 12 13 14 15
num += 1
print()
8. Nested loops to print a pattern.
rows = 5 A
for i in range(1, rows + 1): AB
for j in range(65, 65 + i): ABC
print(chr(j), end="") ABCD
print() ABCDE
# ASCII values of 'A' start at 65
9. Nested loops to print a pattern.
rows = int(input("Enter a number: "))
for i in range(rows, 0, -1):
print("*" * i)
10. Nested loops to print a pattern.
rows = int(input("Enter a number: "))
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
11. Nested loops to print a pattern.
rows = int(input("Enter a number: "))
for i in range(rows, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
12. Nested loops to print a pattern.
rows = 5
# Upper part of the diamond
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
# Lower part of the diamond
for i in range(rows - 1, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
13. Use break to terminate a loop when a condition is met.
for i in range(10):
if i == 5:
break
print(i)
14. Use continue to skip an iteration when a condition is met.
for i in range(6):
if i == 3:
continue
print(i)
15. Use pass as a placeholder in a loop.
for i in range(5):
if i == 2:
pass # To be implemented later
print(i)
16. Check if a number is prime using a for loop and else.
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(f"{num} is not a prime number.")
break
else:
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
17. Print Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13 . . . .
n = int(input("Enter the number of terms for the Fibonacci series: "))
if n <= 0:
print("Please enter a positive integer.")
else:
# Initialize the first two terms
a, b = 0, 1
print("Fibonacci Series:")
for i in range(n):
print(a, end=" ")
# Update terms
a, b = b, a + b
18. Program to compute the factorial of a given number
num = int(input("Enter a number to compute its factorial: "))
if num < 0:
print("Factorial is not defined for negative numbers.")
elif num == 0:
print("The factorial of 0 is 1.")
else:
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}.")
List of programs to practice (Home work)
19. Write a program to display numbers 1 to 10.
20. Write a program to display all even numbers from 1 to 20
21. Write a program to display all odd numbers from 1 to 20
22. Write a program to print all the Numbers Divisible by 7 from 1 to 100.
23. Write a program to print table of 2.
24. Write a program to print table of 5.
25. Write a program to print table of any number.
26. Write a program to print table of 5 in following format.
5X1=5
5 X 2 = 10
5 X 3 = 15
27. Write a program to Find the Sum of first 50 Natural Numbers using for Loop.
28. Write a program to calculate factorial of a given number using for loop.
29. Write a program to calculate factorial of a given number using while loop.
30. Write a program to calculate factorial of a given number using do-while loop.
31. Write a program to count the sum of digits in the entered number.
32. Write a program to find the reverse of a given number.
33. Write a program to Check whether a given Number is Perfect Number.
34. Write a program to Print Armstrong Number from 1 to 1000.
35. Write a program to Compute the Value of X ^ N
36. Write a program to Calculate the value of nCr
37. Write a program to generate the Fibonacci Series
38. Write a program to Print First 10 Natural Numbers
39. Write a program to check whether a given Number is Palindrome or Not
40. Write a program to Check whether a given Number is an Armstrong Number
41. Write a program to Check Whether given Number is Perfect or Not
42. Write a program to check weather a given number is prime number or not.
43. Write a program to print all prime numbers from 50-500
44. Write a program to find the Sum of all prime numbers from 1-1000
45. Write a program to display the following pattern:
*****
*****
*****
*****
*****
46. Write a program to display the following pattern:
*
**
***
****
*****
47. Write a program to display the following pattern:
1
12
123
1234
12345
48. Write a program to display the following pattern:
1
22
333
4444
55555
49. Write a program to display the following pattern:
A
BB
CCC
DDDD
EEEEE
50. Write a program to display the following pattern:
*****
****
***
**
*
51. Write a program to display the following pattern:
12345
1234
123
12
1
52. Write a program to display the following pattern:
*
***
*****
*******
*********
53. Write a program to display the following pattern (Pascal Triangle):
41. Write a program to display the following pattern:
23
456
7 8 9 10
54. Write a program to display the following pattern:
BAB
ABABA
BABABAB
55. Write a program to display the following pattern:
1
010
10101
0101010
56. Write a program to display the following pattern:
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
57. Write a program to display the following pattern:
**********
**** ****
*** ***
** **
* *
58. Write a program to display the following pattern:
0
01
010
0101
01010
59. Write a program to display the following pattern:
60. Write a program to display the following pattern:
A
BC
DEF
GHIJ
KLMNO
61. Write a program to display the following pattern:
A
BAB
CBABC
DCBABCD
EDCBABCDE
62. Write a program to display the following pattern:
1A2B3C4D5E
1A2B3C4D
1A2B3C
1A2B
1A
63. Write a program to display the following pattern:
101
21012
3210123
432101234
64. Write a program to print the following sequence of integers.
0, 5, 10, 15, 20, 25
65. Write a program to print the following sequence of integers.
0, 2, 4, 6, 8, 10
66. Write a program to Find the Sum of A.P Series.
67. Write a program to Find the Sum of G.P Series.
68. Write a program to Find the Sum of H.P Series.
69. Write a program to print the following sequence of integers.
1, 2, 4, 8, 16, 32
70. Write a program to print the following sequence of integers.
-8, -6, -4, -2, 0, 2, 4, 6, 8
71. Write a program to find the Sum of following Series:
1 + 2 + 3 + 4 + 5 + ... + n
72. Write a program to find the Sum of following Series:
(1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
73. Write a program to find the Sum of following Series:
(1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)
74. Write a program to find the Sum of following Series:
1! + 2! + 3! + 4! + 5! + ... + n!
75. Write a program to find the Sum of following Series:
(1^1) + (2^2) + (3^3) + (4^4) + (5^5) + ... + (n^n)
76. Write a program to find the Sum of following Series:
(1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + ... + (n!/n)
77. Write a program to find the Sum of following Series:
[(1^1)/1] + [(2^2)/2] + [(3^3)/3] + [(4^4)/4] + [(5^5)/5] + ... + [(n^n)/n]
78. Write a program to find the Sum of following Series:
[(1^1)/1!] + [(2^2)/2!] + [(3^3)/3!] + [(4^4)/4!] + [(5^5)/5!] + ... + [(n^n)/n!]
79. Write a program to find the Sum of following Series:
1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... upto n terms
80. Write a program to print the following Series:
1, 2, 3, 6, 9, 18, 27, 54, ... upto n terms
81. Write a program to print the following Series:
2, 15, 41, 80, 132, 197, 275, 366, 470, 587
82. Write a program to print the following Series:
1, 3, 8, 15, 27, 50, 92, 169, 311