Python for loops in range - While loops condition

In this  section we learn about loops. Mainly loops are two types they are while loops and for loops. In python programming mainly use for loops because for loops easy to use. While loops need any condition such as not equal , equal to. In program when you run loops and if any condition does not match in your loop condition then the program never stop than you computer ram crash. You can use for loop using range such as 0 to 9. While loops is use with the help of condition statement. Also use if elif and else in while loop condition system. Loops has  mainly two statement that is continue statement and break statement. Continue statement is use when for want to skip any value , number ,alphabet from your condition and range. The break statement is stop your loop and exit your loop. When your condition is match a multiple value or alphabet but you only print specific value then your use break statement. your value is match with break statement then exit from loops. 

For loop

num = int(input("enter your multiplication number"))
for i in range(1,11):
    print(str(num),"x",str(i),"=", str(num*i))

It is a for loops example. First get user input type casting in int starting a for loop range from 1 to 11. starting loop foe 1 to 10 at last range number always -1.  It is a multiplication generate problem use input five than automatically generate multiplication of 5.


enter your multiplication number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

User input 5 and print the multiplication table of five. 

While Loop Condition

user = int(input("enter your multiplication number"))
i = 11
while i>0:
    i = i -1

This while loop program is similar then for loop. We use range system in for loop. Here we use condition in while loop. Get input from user and define the i variable with 11 value. Apply the while condition i is greater than 0 and i = i-1.


enter your multiplication number: 5
5x10= 50
5x9= 45
5x8= 40
5x7= 35
5x6= 30
5x5= 25
5x4= 20
5x3= 15
5x2= 10
5x1= 5
5x0= 0

Printing the multiplication value of 5 using a while loop condition. Now another example of while loop with condition.
fruits = ['mango','banana', 'papaya', 'watermelon']
i = 0
while i greater sing here len(fruits):
    i = i + 1

Some html error in while loop condition i greater sing here in that sentences i<len(fruits). If  loops never end that crash your computer ram. To save from computer ram crash user iter(your  variable) and print nxt() your vaiable. It is print only one step of loop again prin nxt() than run second step of loops.



Break statement and Continue Statement
for i in range (-8,8):
    if i == 5:
    print("break statement cannot work")

Star for loop from -8 to 8 Hence -8 -7 -6 -5.....4 5 6 7. Print i with if  condition i is star print -8 to 5 then program loop automatic break. Exit the loop. At last using else any problem in forloop and for loop cannot work or break statement than print else condition.



Now try to write the program with continue statement. Continue statement is escape the value which is meet you condition.
for i in range (8):
    if i == 3:

For loop stat with range 8. The range 8 is means 0 to 8. Set only one digit in range function the range always start from 0. When i is 3 than continue statement escape the 3 and loop run again.


One practice problem to you. How can you find the prime number. Prime number is a greater than 1 that is not multiply by two natural number. How to makes a this types of programm. Write a program and send me in comment box. 
yournum = int(input("enter your number: "))
prime = True
for i in range(2,yournum):
        prime = False
if prime:
    print("it is prime")
    print("it is not prime")

First we get input from user. Define the prime variable with True. starting a range from 2 to user input. If condition is check user input value and range start value i is divide and answer is 0 than prime makes false and loop break. Than those value has prime true that print the if condition other all print else condition. you also use dictionary to keep all prime and  not prime number.


enter your number: 45
it is not prime

enter your number: 2
it is prime

Post a Comment