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:
- The
loop_condition
the variable is set toTrue
- The
while
loop checks to see ifloop_condition
isTrue
. It is, so the loop is entered. - The
print
the statement is executed. - The variable
loop_condition
is set toFalse
. - The
while
loop again checks to see ifloop_condition
isTrue
. 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:
- The loop condition cannot possibly be false (e.g.
while 1 != 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 theif
, you writebreak
, 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 12
, 54
, 99
, and then the loop will exit when there are no more values in the list.
numbers = [7, 9, 12, 54, 99]
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 = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
''' Compare each pair of elements and print the larger of the two.'''
for a, b in zip(list_a, list_b):
# Add your code here!
print max(a, b)
For / else
Just like with
while
, for
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.- #tushargahora.blogspot.comfruits = ['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 )breakprint 'A', felse:print 'A fine selection of fruits!'
It's really Good Article.Thanks for sharing!
ReplyDeleteDevOps Training
DevOps Online Training