Python Forum
break Statements and else Clause on Loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
break Statements and else Clause on Loops
#2
for n in range(2, 10):
this is going to iterate over a range from 2 to 10
so n will be equal to 2, 3, ... up to 9 (1 less than final number because range is zero based)
    for x in range(2, n):
this does the same from 2 to n, and will fully execute for each iteration of the outer loop
but this time terminates on n - 1, and output goes to x
if n % x == 0:
this statement used the '%' modulo operator.
It says, if n mod x == 0 which is true if both x and n are equal
modulo division yields the remainder from the division of the first argument by the second

print n, 'equals', x, '*', n/x
so if n and x are the same, above gets printed

     else:
            print n, 'is a prime number'
and if not equal prints this
Reply


Messages In This Thread
RE: break Statements and else Clause on Loops - by Larz60+ - Mar-01-2018, 04:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 970 Sep-18-2023, 07:52 AM
Last Post: buran
  How to use the LIKE clause in Python Columbo 9 1,729 Oct-09-2022, 10:22 PM
Last Post: Larz60+
  SQL Query is not executing WHERE clause hammer 7 2,444 Nov-15-2021, 01:44 PM
Last Post: hammer
  Break out of nested loops muzikman 11 3,495 Sep-18-2021, 12:59 PM
Last Post: muzikman
  How does this if clause work? Pedroski55 3 2,348 Jun-10-2021, 06:31 AM
Last Post: Gribouillis
  How to break out of nested loops pace 11 5,519 Mar-03-2021, 06:25 PM
Last Post: pace
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 3,048 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  can i raise an exception in a try clause? Skaperen 14 5,910 Dec-19-2019, 12:29 AM
Last Post: Skaperen
  pass captured value from input() to where clause metro17 5 3,352 Sep-09-2019, 05:24 AM
Last Post: metro17
  finally clause Skaperen 6 3,967 Jun-02-2019, 09:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020