For loop

For loop


for loop is the most comfortable loop. It is also an entry check loop. The condition is checked in the beginning and the body of the loop(statements-block 1) is executed if it is only True otherwise the loop is not executed.

Syntax:

for counter_variable in sequence:

statements-block 1

[else: # optional block

statements-block 2]

The counter_variable mentioned in the syntax is similar to the control variable that we used in the for loop of C++ and the sequence refers to the initial, final and increment value. Usually in Python, for loop uses the range() function in the sequence to specify the initial, final and increment values. range() generates a list of values starting from start till stop-1.

Example : #program to illustrate the use of for loop - to print single digit even number

for i in range (2,10,2):

print (i, end=' ')

Output:

2 4 6 8

Example  : #program to illustrate the use of for loop - to print single digit even number with else part

for i in range(2,10,2):
print (i,end=' ')
else:
print ("\nEnd of the loop")

Output:

2 4 6 8

End of the loop

Example : # program to calculate the sum of numbers 1 to 100

n = 100
sum = 0
for counter in range(1,n+1):
sum = sum + counter
print("Sum of 1 until %d: %d" % (n,sum))

Output:

Sum of 1 until 100: 5050

Example : program to illustrate the use of string in range() of for loop

for word in 'Computer':
print (word,end=' ')
else:
print ("\nEnd of the loop")

Output

C o m p u t e r

End of the loop

Python : For loop Program List




Program [1]

WAP to calculate compound simple interest after taking the principle, rate and time.


View Solution


Program [2]

WAP that searches for prime numbers from 15 through 25.


View Solution


Program [3]

WAP to test if given number is prime or not.


View Solution


Program [4]

WAP to check whether square root of a given number is prime or not.


View Solution


Program [5]

WAP to print first n odd numbers in descending order.


View Solution


Program [6]

WAP to find the average of the list of the numbers entered through keyboard.


View Solution


Program [7]

WAP to find the largest number from the list of the numbers entered through keyboard.


View Solution


Program [8]

WAP to find the sum of n natural numbers.


View Solution


Program [9]

WAP to find the sum of first n even numbers.


View Solution


Program [10]

WAP to find the sum of first n odd numbers.


View Solution


Program [11]

WAP a program to generate a list of elements of Fibonacci Series.


View Solution


Program [12]

Write a program to input any number and to print all natural numbers up to given number.


View Solution


Program [13]

Write a program to input any number and to find sum of all natural numbers up to given number.


View Solution


Program [14]

Write a Python program to accept a integer number form user and find the factorial value of input number.

The factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 is 1*2*3*4*5*6 = 720 . Factorial is not defined for negative numbers, and the factorial of zero is one, 0! = 1 .

Sample:

Input : 5

Output : 120


View Solution


Program [15]

Write a Python program to input any number and print all factors of input number.

A factor is a number that divides into another number exactly and without leaving a remainder.

Sample:

Input : 12

Output : Factor of 12 are : 1,2,3,4,6 ,12 


View Solution


Program [16]

Write a Python program to accept a integer number from user and check number is composite Number or not.

Composite number is a whole numbers that have on 1 or more than 1 factors excluding 1 and itself.  


View Solution


Program [17]

write a program to print the table of a given number. The number has to be entered by the user.


View Solution


Program [18]

write a program to calculate the sum of odd numbers divisible by 5 from the range 1..100


View Solution


Program [19]

Write a program to print Floyd's triangle as shown below:

1

2  3

4  5  6

7  8  9  10

11 12 13 14 15


View Solution