Loops in Python
Loops in Python are used to repeat actions efficiently.
Types of Loops
The main types are For loops (counting through items) and While loops (based on conditions).
While Loop
while loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.
Ex.
count = 1
while count <= 3:
count = count + 1
print("Hello World")
Output
Hello World
Hello World
Hello World
Example
1) Generate Counting 1 to 10.
num = 1
while num<= 10:
print(num)
num = num + 1
else:
print("loop finished")
Output
1
2
3
4
5
6
7
8
9
10
loop finished
2) Create table using with Input Method
val = int(input("Enter a Number to Create a table:"))
no=1
while no<= 10:
print(no*val)
num = num + 1
else:
print("loop finished")
Output
Enter a Number to Create a table: 5
5
10
15
20
25
30
35
40
45
50
loop finished
3) Create Program Print Natural Number
a=int(input("Enter any no :"))
no = 1
while no<= 10:
print(no)
no = no + 1
else:
print("loop finished")
Output
Enter any no : 10
1
2
3
4
5
6
7
8
9
10
loop finished
4) Create Reverse Counting (10 to 1) Printing Program.
a=int(input("Enter any no :"))
no = 10
while no>= 1:
print(no)
no = no - 1
else:
print("loop finished")
Output
Enter any no : 10
10
9
8
7
6
5
4
3
2
1
loop finished
5) The continue Statement
With the continue statement we can stop the current iteration, and continue with the next:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
# Note that number 3 is missing in the result
Output
1
2
4
5
6
6) The else Statement
With the else statement we can run a block of code once when the condition no longer is true:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Output
1
2
3
4
5
i is no longer less than 6
7) Continue Statement Multiple Conditions
i = 0
while i < 10:
if i == 3:
i += 1
continue
elif i == 6:
i += 1
continue
elif i == 8:
i += 1
continue
print(i)
i += 1
Output
8) Break Statement
i = 1
while i < 6:
print(i)
if (i == 3):
break
i += 1
Output
For Loops
Python for loops are used for iterating over sequences like lists, tuples, strings and ranges.
A for loop allows you to apply the same operation to every item within the loop.
Using a for loop avoids the need to manually manage the index.
A for loop can iterate over any iterable object, such as a dictionary, list or custom iterator.
Ex.
s = ["One", "Two", "Three"]
for i in s:
print(i)
Output
One
Two
Three
1) Iterating over characters of strings
This code uses a for loop to iterate over a string and print each character on a new line.
s = "AD COMPUTER CAMPUS"
for i in s:
print(i)
Output
A
D
C
O
M
P
U
T
E
R
C
A
M
P
U
S
2) Using range() with For Loop
i) Printing Number with One (Stop) Argument
for i in range(7)
print(i)
Output
0
1
2
3
4
5
6
ii) Printing Number with Two (Start, Stop) Argument
for i in range(7,13)
print(i)
iii) Printing Number with Three (Start, Stop,Step) Argument
for i in range(10,50,3)
print(i)
Output
10
15
20
25
30
35
40
45
3) Printing 1.1 1.2 1.3 .................................1.10 Series.
for i in range(1,11):
print("1.%d"%i)
Output
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10
4) Printing Name With Serial Number
for i range(1,6):
print(i,". Radhe")
Output
1 . Radhe
2 . Radhe
3 . Radhe
4 . Radhe
5 . Radhe
5) Sum of n Natural Number
sum=0
no=int(input("Enter N Value "))
for i in range(1,no+1):
sum=sum+i
i=i+1
print("sum of N Natural Number is", sum)
Output
Enter N value 10
sum of N Natural Number is 55.
6) Print the Fibonacci Sequence
a= 0
b = 1
c= 0
n=int(input("Enter n value"))
print(a)
print(b)
while c<=n:
c=a+b
print(c)
a=b
b=c
Output
Enter n value50
0
1
1
2
3
5
8
13
21
34
55
7) Sum of Digit
a=int(input("Enter any number:"))
sum=0
while a>0:
rem=a%10
sum=sum+rem
a=a//10
print("sum=",sum)
Output
Enter any number:134
sum= 8
8) Sum of Digit Square
a=int(input("Enter any number :"))
sum=0
while a>0
rem=a%10
sum=sum+rem*rem
a=a//10
print("sum= ",sum)
Output
Enter any number :123
sum= 14
9) Reverse of Digit
a=int(input("Enter any number: "))
temp=a
rev=0
while a>0:
rem=a%10
rev=rev*10+rem
a=a//10
print("Reverse Digits= ",rev)
Output
Enter any number: 456
Reverse Digits= 654
10) Krishnamurthy Number
no = int(input("Enter any no: "))
total_sum = 0
temp = no
while no > 0:
rem = no % 10
fact = 1
# Calculate factorial of the current digit
while rem > 0:
fact = fact * rem
rem = rem - 1
# Add the digit's factorial to the total sum
total_sum = total_sum + fact
# Move to the next digit
no = no // 10
if temp == total_sum:
print("The number is Krishnamurthy")
else:
print("The no is not Krishnamurthy")
Output
Enter any no: 145
The number is Krishnamurthy
11) Check Prime Number
num = int(input("Enter a number: "))
for i in range(2, num):
if (num % i == 0):
print("is not a prime number")
break
else:
print("your no is prime number")
Output
Enter a number: 12
is not a prime number
Check GCD (Greatest Common Divisor)
num1=int(input("enter first number:"))
num2=int(input("enter second number:"))
for i in range(1,num1 and num2):
if num1%i==0 and num2%i==0:
gcd=i
print("GCD is",gcd)
Output
enter first number:10
enter second number :30
GCD is 10
LCM by GCD
num1=int(input("enter first number:"))
num2=int(input("enter second number:"))
for i in range(1, num1 and num2):
if num1%i==0 and num2%i==0;
gcd=i
Icm=(num1*num2)/gcd
print("Icm",Icm)
Output
enter first number:10
enter second number:2
Icm 20.0
Match case Statement
print("1. Red")
print("2. Yellow")
print("3. Green")
choice=int(input("Enter Your Choice"))
match choice:
case 1:
print("Red")
case 2:
print("Yellow")
case 3:
print("Green")
case:
print("Wrong Choice")
Output
1. Red
2. Yellow
3. Green
Enter Your Choice 2
Yellow
No comments: