The document contains several Python code snippets demonstrating the use of loops. It includes examples of printing sequences of numbers, calculating sums of even and odd numbers, and utilizing both for and while loops. The code snippets illustrate basic programming concepts such as iteration and conditionals.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
Comp Pratical
The document contains several Python code snippets demonstrating the use of loops. It includes examples of printing sequences of numbers, calculating sums of even and odd numbers, and utilizing both for and while loops. The code snippets illustrate basic programming concepts such as iteration and conditionals.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
# 1) Use the for loop to print the numbers
8,11,14,17,20,........83,86,89 for i in range (8,90,3): print(i,end=",")
#2)Use the for loop to print the numbers
100,98,96,.....4,2. for n in range (1,20): print(n)
#3)print 1 to 100 using while loop
n=1 while n<=100: print(n,end=",") n=n+1 #4) find out the sum of numbers within the range 1 to 100 using while loop num = 100 sum = 0 while num>0: sum += sum num -= 1 print("the sum is", sum )
#5) Calculate the sum of first even numbers
by using while loop and for loop. num = int(input('Enter a number: ')) sum = 0 i = 0 while i <= num: if i % 2 == 0: print(i) sum+=i i+=1 print(f"Sum of all the even numbers is {sum}")
#Using for loop
num = int(input('Enter a number: ')) sum = 0 for i in range(0, num+1): if i % 2 == 0: print(i) sum+=i
print(f"Sum of all the even numbers is
{sum}")
#6)Print first 20 odd numbers in Asending
order using while loop for i in range(1,40): if i%2!=0: print(i,end=",") i=i-1