In [ ]:


In [5]:
for letter in 'Python':     # First Example
    print 'Current Letter :', letter

fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # Second Example
    print 'Current fruit :', fruit

print "Good bye!"


Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!

Python supports an else statement associated with a loop statement

  • If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.
  • If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

In [6]:
for num in range(10,20):  #to iterate between 10 to 20
    for i in range(2,num): #to iterate on the factors of the number
        if num%i == 0:      #to determine the first factor
            j=num/i          #to calculate the second factor
            print '%d equals %d * %d' % (num,i,j)
            break            #to move to the next number, the #first FOR
    else:                  # else part of the loop
        print num, 'is a prime number'


10 equals 2 * 5
11 is a prime number
12 equals 2 * 6
13 is a prime number
14 equals 2 * 7
15 equals 3 * 5
16 equals 2 * 8
17 is a prime number
18 equals 2 * 9
19 is a prime number

In [9]:
i = 2
while(i < 100):
    j = 2
    while(j <= (i/j)):
        if not(i%j): 
            break
        j = j + 1
    if (j > i/j) : 
        print i, " is prime"
    i = i + 1

print "Good bye!"


2  is prime
3  is prime
5  is prime
7  is prime
11  is prime
13  is prime
17  is prime
19  is prime
23  is prime
29  is prime
31  is prime
37  is prime
41  is prime
43  is prime
47  is prime
53  is prime
59  is prime
61  is prime
67  is prime
71  is prime
73  is prime
79  is prime
83  is prime
89  is prime
97  is prime
Good bye!

In [10]:
count = 0
while (count < 9):
    print 'The count is:', count
    count = count + 1

print "Good bye!"


The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

In [11]:
count = 0
while count < 5:
    print count, " is  less than 5"
    count = count + 1
else:
    print count, " is not less than 5"


0  is  less than 5
1  is  less than 5
2  is  less than 5
3  is  less than 5
4  is  less than 5
5  is not less than 5

In [12]:
for i in range(2):
    print(i)
else:
    print('completed for-loop')


0
1
completed for-loop

In [13]:
for i in range(2):
    print(i)
    break
else:
    print('completed for-loop')


0

In [14]:
i = 0
while i < 2:
    print(i)
    i += 1
else:
    print('in else')


0
1
in else

In [15]:
i = 0
while i < 2:
    print(i)
    i += 1
    break
else:
    print('completed while-loop')


0

In [ ]:
# don't run this code

# flag = 10
#while (flag): print 'Given flag is really true!'
#print "Good bye!"

Exception handling with lists


In [22]:
a_list=[0,1,2,3,4,5]
try:
    print('first element:', a_list[0])
except IndexError:
    print('raised IndexError')
else:
    print('no error in try-block')


('first element:', 0)
no error in try-block

In [23]:
try:
    print('third element:', a_list[2])
except IndexError:
    print('raised IndexError')
else:
    print('no error in try-block')


('third element:', 2)
no error in try-block