Looping statement in python

Basic looping structure in python


Python supports basic loop structures through iterative statements.iterative statements are decision control statements that are used to repeat the execution of a list of statements.python language supports two types of iterative statements - while loop and for loop.


While loop:

The while loop provides a mechanism to repeat one or more statements while a particular condition is true.

Syntax:
statement x
while (condition):
    block of statement
statement y 


A while loop is also referred to as a top-checking loop since control condition is places as the first line of the code. if the condition evaluates to false, then the statement enclosed in the loop are never executed ...
now look following examples:

Q. write a program in python to calculate the sum and average of first 10 number.


i = 0
s = 0
while(i<=10):
     s = s+i
     i=i+1
avg = float(s)/10
print("the sum of first 10 number is :",s)
print("the average of first 10 number is :",avg)

OUTPUT
print("the sum of first 10 number is :55
print("the average of first 10 number is :5.5

Q.write a program in python to print 20 horizontal asterisks.

i = 1
while(i<=20):
     print("*",end="")
     i = i+1

OUTPUT
***********




For loop :
The for...in statement is a looking statement used in python to iterate over a sequence of objects i.e.. go through each item in a sequence, here by sequence we mean just an ordered collection of items,the for loop is execute for each item in the sequence.

Syntax:

The range(): function.
The range() produces a sequence of numbers starting with beg(inclusive)and ending with one less than number end. The step arguments is optional(that is why it is placed in brackets) by default every in the range is incremented by 1 but we can specify a different increment
using step.

Step can be either a positive value or negative but it cannot be equal to zero.

if range function is given a single argument, it produces an object with values from 0 to argument -1 for example : range(10) is equal to writing range(0,10).
for i in range(10):
     print(i,end=' ')
OUTPUT:
0 1  2  3  4  5 6  7  8 9 
if range() is called  with two arguments, it produces values from the first to the second. for examples, range(0,10).
for i in range(0,10):
     print(i,end=' ')
OUTPUT:
0 1  2  3  4  5 6  7  8 9 
if range() has three arguments, then the third  argument specifies the interval of the sequence produced.In this case the third argument must be an integer for examples, range(0,20,3)
for i in range(0,20,3):
     print(i,end=' ')
OUTPUT:
0 3 6 9 12 15 18



EXERCISE:

Q. write a program using for loop to calculate the average of first n natural number.

n = int(input("enter any number :"))
av=0.0
s=0
for i in range(1,n+1):
     s=s+i
avg=s/i
print("sum of the first",n," natural numbers is", s)
print("Average of the first",n," natural numbers is", avg)
OUTPUT
sum of the first 10  natural numbers is 55
Average of the first 10  natural numbers is 5.5

Q. write a program in java using for loop  to calculate factorial of a number.

n = int(input("enter any number :"))
if(n==0):
     fact=1
fact=1
for i in range(1,n+1):
     fact=fact*i
print("factorial of ",n,"is",fact) 
OUTPUT
enter any number :5
factorial of  5 is 120
Q. write a program to check prime and composite number.

number= int(input("enter any number :"))
iscomposit=0
for i in range(2, number):
     if(number%i==0):
          iscomposit=1
          break
if(iscomposit==1):
     print("number is composite")
else:
    print("number is prime")
OUTPUT
enter any number : 12
number is composite

Q.write a program to sum of the series--1/1+2^2/2+3^3/3+..............+n^n/n

n= int(input("enter the value of  n:"))
s=0.0
for i in range(1,n+1):
     a=float(i**i)/i
     s=s+a
print("the sum of series :",s)
OUTPUT
enter the value of  n:  6
the sum of series : 8477.0
Q.write a program to calculate sum of cubes of number from 1 to-n.

n= int(input("enter the value of  n:"))
s=0
for i in range(1,n+1):
     a=i**3
     s=s+a
print("the sum of series :",s)
OUTPUT
enter the value of  n: 5
the sum of series : 225



Thank you..........
please like and follow my Facebook page.....
Let's_code
Decision making statement in python
function_in_python
Pattern program 

Comments

Popular posts from this blog

python pattern programming

Decision making statement in python