# python for loop

In this post:

- Basic loop - for x in range(3, 5):
- for loop based on list
- for loop list all files recursively
- for loop and else clause
- for loop with continue
- for loop example get perfect numbers

## Basic loop for in python

The syntax of for loop is straight forward and at the same time give you many options. You can see the some basic example below:

## for loop from range

```
for x in range(3, 5):
print ("%d" % (x))
```

result:

3

4

```
for x in range(5):
print ("%d" % (x))
```

result:

0

1

2

3

4

## for loop based on list

```
days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]
for day in days:
print (day)
```

result:

SUN

MON

TUE

WED

THU

FRI

SAT

## for loop list all files recursively

You can use for loop to list all files and folders recursively by:

```
import os
indir = 'C:\\Users\\user\\'
for root, dirs, filenames in os.walk(indir):
for f in filenames:
filename = indir + f
print filename
```

result:

test.txt

## for loop and else clause

```
for x in xrange(3):
print 'We start in %d' % (x)
else:
print 'We are live'
```

result:

We start in 0

We start in 1

We start in 2

We are live

## for loop with step

If you need to skip several steps you have the option of skipping n steps. Let say that you want odd numbers then you can do:

```
for x in range(1, 10, 2):
print(x)
```

result:

1

3

5

7

9

## for loop with continue

Continue can be used within for loop. The continue statement is used to continue with the next iteration of the loop. It's useful in several situations:

#### filtering an input

For loop and continue can be used to remove special characters from a string:

```
str = "find ;;;divisors of...... a ,,number without the number"
for x in str:
if (x in [";",',','.']):
continue
print(x, end="")
```

result:

find divisors of a number without the number

#### preventing errors

Errors can be easily avoided with continue statement. In the example below error:

TypeError: unsupported operand type(s) for /: 'int' and 'str'

will be produced without continue statement:

```
for num in [1,4,5,'c',7,8,9,'b']:
if not isinstance(num, int): continue
print (1/num)
```

## for loop example get perfect numbers

By definition perfect number is a number that is equal to it's divisors sum. So 6 and 1, 2, 3 are the first perfect number. Below you can find example of for loop and continue in order to stop the second check if the number is perfect.

```
import math
from numpy import prod
#find divisors of a number without the number
def divisorGenerator(n):
divs = [1]
for i in range(2,int(math.sqrt(n))+1):
if n%i == 0:
divs.extend([i,n/i])
divs.extend([])
return list(set(divs))
for x in range(1, 30):
divisors = list(divisorGenerator(x))
suma = sum(divisors)
product = prod(divisors)
#check is it perfect number
if(suma == x and divisors != [1]):
print (x)
print (divisors)
continue # if we remove it we will get the second result
# check if the sum is equal to product
if (suma == product and divisors != [1]):
print(x)
print(divisors)
```

result:

6

[1, 2, 3]

28

[1, 2, 4, 14, 7]

second result without continue:

6

[1, 2, 3]

6

[1, 2, 3]

28

[1, 2, 4, 14, 7]