0% found this document useful (0 votes)
7 views3 pages

Python Loop Constructs Explained

Uploaded by

shibtain3545
Copyright
© All Rights Reserved
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% found this document useful (0 votes)
7 views3 pages

Python Loop Constructs Explained

Uploaded by

shibtain3545
Copyright
© All Rights Reserved
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

HANDWRITTEN STYLE NOTES (PRINTABLE)

Q(a) Discuss the use of loop constructs in Python.

Loop constructs help repeat a block of code multiple times, reducing repetition and
improving efficiency. Python supports for and while loops.

Q(b) Difference between for loop and while loop.

for loop: Used when number of iterations is known.

while loop: Used when repetition depends on a condition.

Q(c) Explain break and continue with examples.

break: Exits loop immediately.

continue: Skips current iteration.

Q(d) How do you exit a for loop prematurely?

Using break statement.

Q(e) When is while loop used instead of for loop?

When number of iterations is not fixed and depends on a condition.

Q(f) Explain pass statement.

pass does nothing; used as a placeholder.

Q(g) Note on Nested Loops.

A loop inside another loop, used for patterns and matrices.

Q(h) What does range() do in for loop?

Generates a sequence of numbers used for iteration.

PROGRAMS:

1(a) Print first 20 odd numbers.

for i in range(1, 40, 2):

print(i)
1(b) Display squares of first 5 natural numbers.

for i in range(1, 6):

print(i*i)

1(c) Display numbers from given number to 0.

n = int(input())

for i in range(n, -1, -1):

print(i)

1(d) Sum of n natural numbers using while loop.

n=int(input())

s=0;i=1

while i<=n:

s+=i;i+=1

print(s)

1(e) Palindrome number checker.

n=int(input())

temp=n;rev=0

while temp>0:

rev=rev*10+temp%10

temp//=10

print("Palindrome" if rev==n else "Not Palindrome")

2(a) Armstrong number.

n=int(input())

temp=n;s=0

while temp>0:

d=temp%10;s+=d**3;temp//=10

print("Armstrong" if s==n else "Not Armstrong")


2(b) Reverse number.

n=int(input())

rev=0

while n>0:

rev=rev*10+n%10;n//=10

print(rev)

2(c) Sum & product of list.

lst=[2,3,4,5]

s=0;p=1

for i in lst:

s+=i;p*=i

print(s,p)

2(d) Count vowels.

s=input()

v="aeiouAEIOU";c=0

for ch in s:

if ch in v:c+=1

print(c)

2(e) Pattern.

for i in range(1,6):

print("*"*i)

You might also like