Learn Python : LOOPS






While

The while the loop is similar to an if statement: it executes the code inside of it if some condition is true. The difference is that the while loop will continue to execute as long as the condition is true. In other words, instead of executing if something is true, it executes while that thing is true.

Condition

The condition is the expression that decides whether the loop is going to continue being executed or not. There are 5 steps to this program:
  1. The loop_condition the variable is set to True
  2. The while loop checks to see if loop_condition is True. It is, so the loop is entered.
  3. The print the statement is executed.
  4. The variable loop_condition is set to False.
  5. The while loop again checks to see if loop_condition is True. It is not, so the loop is not executed a second time.

Infinite loops





An infinite loop is a loop that never exits. This can happen for a few reasons:
  1. The loop condition cannot possibly be false (e.g. while 1 != 2)
  2. The logic of the loop prevents the loop condition from becoming false.

Break

The break is a one-line statement that means “exit the current loop.” An alternate way to make our counting loop exit and stop executing is with the break statement.
  • First, create a while with a condition that is always true. The simplest way is shown.
  • Using an if statement, you define the stopping condition. Inside the if, you write break, meaning “exit the loop.”
The difference here is that this loop is guaranteed to run at least once.

While / else




Something completely different about Python is the while/else construction. while/else is similar to if/else, but there is a difference: the else the block will execute anytime the loop condition is evaluated to False. This means that it will execute if the loop is never entered or if the loop exits normally. If the loop exits as the result of a break, the else will not be executed.

Example:

 The most common use of for loops is to go through a list.
On each iteration, the variable num will be the next value in the list. So, the first time through, it will be 7, the second time it will be 9, then 125499, and then the loop will exit when there are no more values in the list.

numbers  = [79125499]

print "This list contains: "

for num in numbers:
  print num

''' Adding loop below for loop that goes through the numbers list and prints each element squared, each on its own line.'''
for num in numbers:
  print num ** 2

Multiple lists

It’s also common to need to iterate over two lists at once. This is where the built-in zip function comes in handy.
zip will create pairs of elements when passed two lists, and will stop at the end of the shorter list.
zip can handle three or more lists as well!

example:


list_a = [39171519]
list_b = [2481030405060708090]
''' Compare each pair of elements and print the larger of the two.'''
for ab in zip(list_alist_b):
  # Add your code here!
    print max(ab)





For / else

Just like with whilefor loops may have an else associated with them.
In this case, the else statement is executed after the for, but only if the for ends normally—that is, not with a break. This code will break when it hits 'tomato', so the else block won’t be executed.

  1. #tushargahora.blogspot.com
    fruits = ['banana''apple''orange''tomato''pear''grape']



    print 'You have...'
    for f in fruits:
      if f == 'tomato':
        print 'A tomato is not a fruit!' # (It actually is, yes tomato is a fruit but here we assumed that tomato is not a fruit )
        break
      print 'A'f
    else:
      print 'A fine selection of fruits!'



1 comment:

Powered by Blogger.