Create a range within a for in range() loop?

Asked

Viewed 45 times

2

In a tuple of items, for example:

for x in range(2,numero+1):

1) Suppose the variable numero has been defined before as 7. Thus, how do I not include the 7 in this tuple, wanting to go through all of its items, except the 7, using the method range python?

What I place within the range to satisfy the above condition?

for x in range():

1 answer

5

There are several ways to do this. The most obvious is this:

for x in range(2, numero):
    fazer_alguma_coisa()
    fazer_outra_coisa()

If you want to skip some number within the range, you can do this:

for x in range(a, b):
    if x == numero_que_nao_gosto:
        continue
    fazer_alguma_coisa()
    fazer_outra_coisa()

If it’s multiple numbers being skipped over the range:

for x in range(a, b):
    if x in numeros_que_nao_gosto:
        continue
    fazer_alguma_coisa()
    fazer_outra_coisa()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.