Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Loops Statement In Python 2

 

For loop

 

For loop as the ability to iterate what are the items of any sequence, such as list or a string.

 

Syntax :

for iterating_var in sequence:

statement(s)

 

Example :

words=['Dog','Cat','Puppy','Lion']

for w in words:

print('Animal:',w)

O/P -

Animal:Dog

Animal:Cat

Animal:Puppy

Animal:Lion

 

Nested for loop

 

Syntax :

for iterating_var in sequence:

statements(s)

for iterating_var in sequence:

statements(s)

 

Example :

words=['Cat','Dog','Puppy','Lion']

for w in words:

print(w,len(w))

for a in words:

print(a,w)

 

For loop with range()

 

The built-in range function in Python is very usable to generate sequences of numbers in the form of a list.

 

Syntax :

range(start,stop[step])

 

Example :

for item in range(1,10,1):

print(item)

O/P -

1

2

3

4

5

6

7

8

9

10

 

Example :

'''

1

1 2

1 2 3

1 2 3 4

'''

for i in range(1,5,1):

for j in range (1,(i+1),1):

print(j," ",end=" ")

print(' ')

O/P :

1

1 2

1 2 3

1 2 3 4

 

Control Statements

 

Loop control statements change execution from its normal sequence.

 

Python supports the following control statements –

  • Break
  • Continue
  • Pass

 

Break statement

 

It terminates the current loop and resumes execution at the next statement.

 

Example :

words=['Cat','Dog','Puppy','Lion']

for w in words:

if(w=='Dog')

break

print(w,len(w))

O/P - Cat 3

 

Continue statement

 

The continue statement rejects all the remaining statements in the current iteration of the loop up and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.

 

Example :

words=['Cat','Dog','Puppy','Lion']

for w in words:

if(w=='Dog')

continue

print(w,len(w))

O/P -

Cat 3

Puppy 5

Lion 4

 

Pass statement

 

Example :

words=['Cat','Dog','Puppy','Lion']

for w in words:

if(w=='Dog')

pass

else print(w,len(w))

FAQs

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere