For loop isused to execute a block of
statements repeatedly until a given
range or sequence is completed
For Loop
for i in range(0,10):
print(“hello”)
Specifies range
of i from 0-9
print “Hello” 10 times
Last
element
?
Execute code
inside for
No
Yes
Loop finish
4.
For loop
for iin range(0,10,1):
print(“hello”)
Here i goes from 0 to 9 and increasing i by stepsize 1
The range() function generates a sequence of numbers
The syntax for range() function is,
range(start , stop , step)
start → indicates the beginning of the sequence. If the start argument is not specified, then the
sequence of numbers start from zero by default.
stop → Generates numbers up to this value but not including this number itself.
step → indicates the difference between every two consecutive numbers in the sequence. The
step value can be both negative and positive but not zero.
Note:Both start and step arguments are optional
5.
Demonstrate for LoopUsing range() Function
Display counting from 0 to 100 using different arguments in range()
a) Using only stop
for i in range(100):
print(i, end=“ “)
b) Using Both ''start'' and ''stop'' argument values specified in range function"
for i in range(0, 101):
print(i, end=“ “)
c) Using all three arguments ''start'', ''stop'' and ''step'' specified in range function")
for i in range(0,101, 1):
print(i, end=“ “)
6.
Write a programto print table of a number
n = int(input("Enter a number"))
sum=0
for i in range(1,n+1):
sum=sum+I
print(sum)
Write a program to sum of n natural numbers
n = int(input("Enter a number"))
for i in range(1, 11):
print(n,” x ”,i,” =“, n*i)
7.
Program to displayEach Character in the String Using for Loop
for ch in "Blue":
print(ch)
Write a Program to Find the Sum of All Odd and Even Numbers up to a Number Specified
by the User.
number = int(input("Enter a number"))
even = 0
odd = 0
for i in range(number):
if i % 2 == 0:
even = even + i
else:
odd = odd + i
print(f"Sum of Even numbers are {even} and Odd numbers are {odd}")
8.
number = int(input('Entera number'))
factorial = 1
if number < 0:
print("Factorial doesn't exist for negative numbers")
elif number == 0:
print('The factorial of 0 is 1’)
else:
for i in range(1, number + 1):
factorial = factorial * i
print(f"The factorial of number {number} is {factorial}")
Output
● Enter a number 5
● The factorial of number 5 is 120
Write a Program to Find the Factorial of a
Number using for loop
9.
While Loop
The whileloop starts with the while keyword and ends with a colon. With a while statement, the first thing
that happens is that the Boolean expression is evaluated before the statements in the while loop block is
executed. If the Boolean expression evaluates to False, then the statements in the while loop block are never
executed. If the Boolean expression evaluates to True, then the while loop block is executed. After each
iteration of the loop
block, the Boolean expression is again checked, and if it is True, the loop is iterated again.
10.
While loop isused to execute a
block of statements repeatedly
until a given a condition is
satisfied
Syntax:
while(condition):
# do something1
# do something2
While Loop
Start=1
While(start<=10):
print(“hello”)
start=start+1
intialize
condition
increment
To print “Hello” 10 times
11.
Write Python Programto Display First 10 natural nsumbers Using
while Loop Starting from 0
i = 1
while i < 11:
print(i)
i = i + 1
12.
Write a Programto Find the Average of n Natural Numbers
Where n Is the Input from the User
n = int(input("Enter a number up to which you want to find the average"))
i = 0
sum = 0
while i < n:
sum = sum + I
i = i + 1
average = sum/n
print(f"The average of {number} natural numbers is {average}")
13.
Write a Programto Find the Factorial of a
Number using while loop
n = int(input("Enter a number "))
i=n
fact=1
if n < 0:
print("Factorial doesn't exist for negative numbers")
elif number == 0:
print('The factorial of 0 is 1’)
else:
while(i>0):
fact=fact*I
i=i-1
print(“Factorial of ”,n,”is”,fact)
14.
When the conditionof loop remains true always then loop goes on execution
without stopping i.e Infinite Loop.
Example:
num=1
while(num==1):
print(“Hmm”)
Above code will go to infinite loop until condition is changed or variable is
changed.
Infinite Loop using while
15.
Break and continuestatements
● The break and continue statements provide greater control over the execution of code in a loop.
Whenever the break statement is encountered, the execution control immediately jumps to the
first instruction following the loop.
● To pass control to the next iteration without exiting the loop, use the continue statement. Both
continue and break statements can be used in while and for loops.
16.
for i inrange(1,11):
if(i==5):
break
else:
print(i,end=“ “)
break is used to exit a for loop or a
while loop,
Break & Continue
continue is used to skip the current
block, and return to the "for" or
"while" statement
for i in range(1,11):
if(i==5):
continue
else:
print(i)
Output: 1 2 3 4 Output: 1 2 3 4 6 7 8 9 10
17.
Write a programto display Fibonacci series
Write a program to find number is prime or
not
Using Loops