Write Python program to demonstrate use of looping statements: ‘while’ loop, ‘for’ loop and Nested loop
A loop statement allows us to execute a statement or group of statements multiple
times. Python programming language provides following types of loops to handle
looping requirements.
Write a Python program to print Fibonacci series |
a) while loop: A while loop statement in Python programming language repeatedly
executes a target statement as long as a given condition is true.
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.
Example
#!/usr/bin/Python
count = 0
while (count < 5):
print 'The count is ', count
count = count + 1
print "Good bye!"
Output:
The count is 0
The count is 1
The count is 2
The count is 3
The count is 4
Good bye!
#totalprogrammingcode.blogspot.com
b) for loop: It has the ability to iterate over the items of any sequence, such as a list or
a string.
for iterating_var in sequence: statements(s)
If a sequence contains an expression list, it is evaluated first. Then, the first item in the
sequence is assigned to the iterating variable iterating_var. Next, the statements block
is executed. Each item in the list is assigned to iterating_var, and the statement(s)
block is executed until the entire sequence is exhausted.
Example
#!/usr/bin/Python
for letter in 'Python': # First Example
print 'Current Letter :', letter
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # Second Example
print 'Current fruit :', fruit
print "Good bye!"
Output:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
c) nested loops: Python programming language allows to use one loop inside another
loop. Following section shows few examples to illustrate the concept.
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
The syntax for a nested while loop statement in Python programming language is as
follows –
while expression:
while expression: statement(s)
statement(s)
A final note on loop nesting is that you can put any type of loop inside of any other
type of loop. For example a for loop can be inside a while loop or vice versa.
Example
The following program uses a nested for loop to find the prime numbers from 2 to 100
#!/usr/bin/Python
i = 2
while(i < 20):
j = 2
while(j <= (i/j)):
if not(i%j): break
j = j + 1
if (j > i/j) : print i, " is prime"
i = i + 1
print "Good bye!"
Output:
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
Good bye!
Examples
1. Print the following patterns using loop:
*
**
***
****
for i in range(4):
for y in range(i+1):
print("*",end="")
print()
2.Print the following patterns using loop:
*
***
*****
***
*
no = 2
for i in range(no):
print(" " * (no-i),"*" *(2*i+1))
for z in range(no, -1,-1):
print(" " *(no-z),"*" * (2*z+1))
3. Print the following patterns of 1,0 using loop:
1010101
10101
101
1
i=4
while(i>0):
j=2*i-1
print(" "*(4-i),end="")
while(j>0):
if(j%2==0):
print("0",end="")
else:
print("1",end="")
j=j-1
print("")
i=i-1
4. Write a Python program to find the sum of first 10 natural numbers using for loop
no=0
for i in range(1,11):
no= no + i
print("The sum of first 10 natural numbers :",no)
5. Write a Python program to print Fibonacci series.
count= 0
num1=0
num2=1
num=int(input("Enter the number :"))
while(count<=num):
print(num1)
sum=num1+num2
num1=num2
num2=sum
count+=1
6.Write a Python program that takes a number and checks whether it is a palindromeor not.
no =int(input("Enter a number:"))
sum =0
temp = no
while(no > 0):
digit = no % 10
sum = sum * 10 + digit
no = int(no / 10)
if(temp == sum):
print("no is palindrome")
else:
print("no is not palindrome")
7. Write a Python program to calculate factorial of a number
no = int(input("Enter the number :"))
for i in range(1,no):
no = no*i
print(no)