jeudi 30 juillet 2020

for loop for finding prime numbers in python [duplicate]

I'm new to python. I have a medium level background in c++. I was using the official documentation to learn python and came across this example which prints the prime numbers. Here is the example I'm talking about -

data = list(range(2,10))

for item in data :
    for div in range(2,item) :
        if item % div == 0 :                 /*  
            print(item, "not a prime")           not the same if-else statement
            break
    else :                                       
        print(item, "prime")                 */

here is where I'm confused- As far as I know, you can only have a matching if-else statement in c++. But in this case if-else statements are different as if statement is under for div in range(2,item) and else statement is under for item in data.

any help is appreciated. Thanks

Aucun commentaire:

Enregistrer un commentaire