p

python tutorial - Control Statements | Loops and Control Statements (continue, break and pass) in Python - learn python - python programming



 python while loop

Learn Python - Python tutorial - python while loop - Python examples - Python programs

Python programming language provides following types of loops to handle looping requirements.

python sequence

While Loop

Syntax :

while expression:
    statement(s)

In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.

while loop

python - Sample - python code :

# prints Hello Wikitechy 3 Times
count = 0
while (count < 3):    
    count = count+1
    print("Hello Wikitechy")

python programming - Output :

Hello Wikitechy
Hello Wikitechy
Hello Wikitechy

For an example where while loop is used for iterators. As mentioned in the article, it is not recommended to use while loop for iterators in python.

 

For in Loop

In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages.

for loop

Syntax:

for iterator_var in sequence:
    statements(s)

It can be used to iterate over iterators and a range.

for loop coding


python - Sample - python code :

# Iterating over a list
print("List Iteration")
l = ["wikitechy", "best e-learning", "website"]
for i in l:
    print(i)
      
# Iterating over a tuple (immutable)
print("\nTuple Iteration")
t = ("wikitechy", "best e-learning", "website")
for i in t:
    print(i)
      
# Iterating over a String
print("\nString Iteration")    
s = "Wikitechy"
for i in s :
    print(i)
      
# Iterating over dictionary
print("\nDictionary Iteration")   
d = dict() 
d['xyz'] = 123
d['abc'] = 345
for i in d :
    print("%s  %d" %(i, d[i]))

python programming - Output :

List Iteration
wikitechy
best e-learning
website

Tuple Iteration
wikitechy
best e-learning
website

String Iteration
W
i
k
i
t
e
c
h
y

Dictionary Iteration
xyz  123
abc  345

We can use for in loop for user defined iterators.

 

Nested Loops

Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.

Syntax:

for iterator_var in sequence:
    for iterator_var in sequence:
        statements(s)
        statements(s)

The syntax for a nested while loop statement in Python programming language is as follows:

Syntax:

while expression:
    while expression: 
        statement(s)
        statement(s)

A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.

python - Sample - python code :

from __future__ import print_function
for i in range(1, 5):
    for j in range(i):
         print(i, end=' ')
    print()

python programming - Output :

1
2 2
3 3 3
4 4 4 4

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

Continue Statement
It returns the control to the beginning of the loop.

continue statement

python - Sample - python code :

# Prints all letters except 'e' and 's'
for letter in 'wikitechy': 
    if letter == 'i' or letter == 'y':
         continue
    print 'Current Letter :', letter
    var = 10

python programming - Output :

Current Letter : w
Current Letter : k
Current Letter : t
Current Letter : e
Current Letter : c
Current Letter : h

Break Statement

break statement

It brings control out of the loop

break statement

python - Sample - python code :

for letter in 'wikitechy': 
 
    # break the loop as soon it sees 'i' 
    # or 'c'
    if letter == 'i' or letter == 'c':
         break
 
print 'Current Letter :', letter

python programming - Output :

Current Letter : i

Pass Statement

pass statement


We use pass statement to write empty loops. Pass is also used for empty control statement, function and classes.

pass statement

python - Sample - python code :

# An empty loop
for letter in 'wikitechy':
    pass
print 'Last Letter :', letter

python programming - Output :

Last Letter : c

Wikitechy tutorial site provides you all the learn python , learn python programming online , best python course online , best online course for python , python online learning

Related Searches to Loops and Control Statements (continue, break and pass) in Python