The while loop in Python repeatedly executes the block of code as long as a given condition is true. It checks if the condition is true before executing the code block. The code block is executed and then the condition is checked again. This continues until the condition becomes false, at which point the code continues execution at the line after the while loop. Python uses indentation to group statements that are part of the while loop code block rather than brackets.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
112 views4 pages
While Loop
The while loop in Python repeatedly executes the block of code as long as a given condition is true. It checks if the condition is true before executing the code block. The code block is executed and then the condition is checked again. This continues until the condition becomes false, at which point the code continues execution at the line after the while loop. Python uses indentation to group statements that are part of the while loop code block rather than brackets.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
A
while loop statement in Python programming language repeatedly executes a target
statement as long as a given condition is true.
Syntax
The syntax of a while loop in Python programming language is −
while expression: statement(s) Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop. In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.
Example
count = 0 while (count < 9): print 'The count is:', count count = count + 1
print "Good bye!"
Using else Statement with While Loop
Python supports to have an else statement associated with a loop statement. If the else statement is used with a while loop, the else statement is executed when the condition becomes false. The following example illustrates the combination of an else statement with a while statement that prints a number as long as it is less than 5, otherwise else statement gets executed. count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5" Exercise Question 1: Print First 10 natural numbers using while loop Solution : i=1 while i<=10: print (I,end=” “) i=i+1
Exercise Question 2: Print the following pattern
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Solution : i=1 while i<=5: j=1 while j<=5: print(j,end=” “) j =j+1 print() i=i+1 Exercise Question 3: Accept number from user and calculate the sum of all number between 1 and given number Solution : num=int(input("Enter the number ")) sum=0 i=1 while i<=num: sum=sum+i i=i+1 print("The sum of natural number from",1, "to ",num," is ",sum)
Exercise Question 4: Print multiplication table of given number
Solution : num=int(input("Enter the number ")) i=1 while i<=10: print(num,"x",i,"=",i*num) i=i+1
Exercise Question 5: Display Fibonacci series up to 10 terms
A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8.... The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n-1)th and (n-2)th term Solution : USING WHILE LOOP num=int(input("Enter the limit ")) a=0 b=1 c=0 print(a," ",b,end=" ") i=1 while i<=num: c=a+b print(c,end=" ") a=b b=c i=i+1
USING FOR LOOP
num=int(input("Enter the limit ")) a=0 b=1 c=0 print(a," ",b,end=" ") for i in range(1,num+1): c=a+b print(c,end=" ") a=b b=c
Exercise Question 6: Write a loop to find the factorial of any number
The factorial (symbol: !) means to multiply all whole numbers from our chosen number down to 1. Solution : num=int(input("Enter the NUMBER ")) i=num fact=1 while i>=1: fact=fact*i i=i-1 print("The factorial of ",num,"is ",fact) Eg : if num is 5 then = 5 x 4 x 3 x 2 x 1 = 120
Exercise Question 7: Display the cube of the number up to a given integer
Solution : num=int(input("Enter the NUMBER ")) i=1 while i<=num: print(i*i*i,end=" ") i=i+1 Exercise Question 8: Find the sum of the series 2 +22 + 222 + 2222 + .. n terms Solution : num=int(input("Enter the limit ")) start = 2 sum = 0 i=1 while i<=num: print(start, end=" ") sum += start start = (start * 10) + 2 i=i+1 print("\nSum of above series is:", sum)