Saturday 27 April 2019

Loops


There are two types of loops in Python, for and while.

"For" Loop

For loops iterate over a given sequence. Here is an example:
primes = [2, 3, 5, 7]
for prime in primes:
        print prime

The range() Function

The built-in function range() is the right function to iterate over a sequence of numbers. It generates lists of arithmetic progressions:
Example:

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

range(n) generates the progression of integer numbers starting with 0 and ending with (n -1)
range() can also be called with two arguments:

range(begin,end)

The above call produces the list of numbers starting with begin (inclusive) and ending with one less than the number "end".
So far the increment of range() has been 1. We can specify a different increment with a third argument. The increment is called the "step". It can be both negative and positive, but not zero:

range(begin,end, step)

for x in xrange(5): # or range(5)
    print x
# Prints out 3,4,5
for x in xrange(3, 6): # or range(3, 6)
    print x

# Prints out 3,5,7
for x in xrange(3, 8, 2): # or range(3, 8, 2)
    print x

"While" Loops

While loops repeat as long as a certain boolean condition is met.
For example:
  
 count = 0
while count < 5:
    print count
    count += 1  # This is the same as count = count + 1

The else Part

While Loop with else partSimilar to the if statement, the while loop of Python has also an optional else part. This is an unfamiliar construct for many programmers of traditional programming languages.
The statements in the else part are executed, when the condition is not fulfilled anymore. Some might ask themselves now, where the possible benefit of this extra branch is. If the statements of the additional else part were placed right after the while loop without an else, they would have been executed anyway, wouldn't they. We need to understand a new language construct, i.e. the break statement, to obtain a benefit from the additional else branch of the while loop.
The general syntax of a while loop looks like this:


while condition:
 statement_1
 ...
 statement_n
else:
 statement_1
 ...
 statement_n

"Break" and "Continue"

Break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement. A few examples:
count = 0
while True:
    print count
    count += 1
    if count >= 5:
        break

# Prints out only odd numbers - 1,3,5,7,9
for x in xrange(10):
    # Check if x is even
    if x % 2 == 0:
        continue
    print x

unlike languages like C,C++. we can use else for loops. When the loop condition of "for" or "while" statement fails then code part in "else" is executed. If break statement is executed inside for loop then the "else" part is skipped. Note that "else" part is executed even if there is a continue statement.
Here are a few examples:
count=0
while(count<5):
    print count
    count +=1
else:

    print "count value reached %d" %(count)

# Prints out 1,2,3,4
for i in xrange(1,10):
    if(i%5==0):
         break
    print i
else:<
    print "this is not printed because for loop is terminated because of break but not due to fail in condition"

No comments:

Post a Comment

Online Compiler - Techie Delight TECHIE DELIGHT </> Bash (4.4) Bash (4.0) Basic (fbc 1.05.0) ...