Python program to display numbers which are divisible by 5 and if you find number greater than 150 stop loop iteration





We have to solve it by traditionally way, because  we have to those numbers which are divided by 5 and with in it not more than 150. when 150 is came break the loop iteration.

In python , there are many ways to create program like list compression,while,for loop’s, but we can’t use both the condition’s in all these loop statement’s.



If you want to know about for loop in python , loop statement allows us to execute a statement or group of statements multiple times.basically it has the ability to iterate over the items of any sequence, such as a list or a string.


The syntax of the loop statement ss :-

For iterating_var in sequence:
statement’s(s)

Now, we have to create this by for loop  first,give it a variable name like I have selected x you can give it your value name and you can change it’s limit.

for x in range(200):

After that create a if statement, if statement contains a logical expresiion using which data is compared  and a decision is made based on result of the comparision.
The syntax of the if statement is :-

if expression :
statement(s)

Now, create a condition and divided it by 5 by x which will (==) 0.(==) is a assignment operator,it a assign a value.
At last , print value of x.

if x%5==0:
print(x)



Now, our second condition is came , if x is greater than 150 we declare a (>)break statement  now the loop is breaked and again, print the values of loop. The break  statement in python terminates the current loop and resumes execution at the next statement.


if x>150:
break
print(x)

Basically there are three types of statement’s in python break,continue and pass. Continue is just opposite of break mean’s it will start the loop and after that loop is start it’s iteration normally.



Now, pass is basically use when nothing we want to do or code to execute. Then,pass is also useful in places where your code will eventually go, but has not been written yet(e.g., in stubsfor example).

Full code the problem is :-

for x in range(200):
                  if x%5==0:
                            print(x)
                  if x>150:
                            break
                            print(x)


Reactions

Post a Comment

0 Comments