Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
4.4k views
in Python Programming by Goeduhub's Expert (8.3k points)
edited by
In this article you will learn  Basic Loop concepts in python.  For loop and  While loop with   Break , Continue and Pass statements.
by (100 points)
Great study material.

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence || Labeled as Highest Rated Course by Udemy

Apply Coupon

2.

Complete Machine Learning & Data Science with Python| ML A-Z Apply Coupon

3.

Complete Python Programming from scratch | Python Projects Apply Coupon
    More Courses

4 Answers

1 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
 
Best answer

 Loops in Python

Python provides different types of loop in python programming. 

  • for loop
  • while loop
  • nested loops 

for loop in python

For loops iterate over a given sequence.In sequence either (dictionary, tuple, set, list and string ) 

for loop syntax (for variable in sequence or range)

for loop concept

Examples

#For loop in list  

color_list=['red','green','white','purple','pink','marron']

for i in color_list:

   print(i)


#For loop in string 

fr_string="Goeduhub technologies"

for i in fr_string:

    print(i)


#For loop in dictionary 

fr_dict={1:'q',2:'w',3:'e',4:"r"}

for i in fr_dict:

    print(i)


#Sum of all the list elements 

Num=[1,2,3,4,5,6,7,8,9,10]

Sum=0

for i in Num:

    Sum=Sum+i

print(Sum)

Output

red green white purple pink marron


G

o e d u h u b t e c h n o l o g i e s


1 2 3 4


55

For loop with range()  Function

range() syntax

range(a) - Generates a sequence started from 0 to a , where a is excluded 

range(a,b)- Generates a sequence started from a ,end at b , where b is excluded 

range(a,b,c)- Generates a sequence started from a, end at (b-1) and c is difference (steps) 

Note:-By default range() function take difference of 1, if difference is not given.

Example

color_list=['red','green','white','purple','pink','marron']

for i in range(len(color_list)):

print(color_list[i])


s=0

for i in range(1,14,3):

    print(i)

    s=s+i

print("sum is =",s)

Output

red green white purple pink marron


1

4

7 10 13 

sum is= 35

For loop with else statement

Example

for i in range(5):

    print(i)

else:

    print("Finally finished!")

Output

0 1 2 3 4

 Finally finished!

Nested for loop

color_list=['red','green','white']

for i in range(2):

    for j in color_list:

           print(i,j)

Output

0 red 0 green 0 white 1 red 1 green 1 white

While Loop in python

While loop also work like for loop and iterate a sequence , with a condition.

Python while loop concept flow chart 

Syntax of while loop 

while expression or condition:
   statement(s)

Example

n=int(input("A Number ="))

i=0

while i <=n:

    i=i+1

    s=i+n

print(s)

Output

A Number =4 5 6 7 8 

9 

Loop control statements

Pass statement-If we have empty for  loop , put in the pass statement to avoid getting an error.

Continue statement- The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. 

Break statement- Terminates the loop statement. 

While loop with break statement 

Example

n=int(input("A Number ="))

b=5

i=0

while i <=n:

    i=i+1

    if i>b:

        break

print(i)

Output

A Number =10 

1 2 3 4 

5 

While loop with continue statement

 num=int(input("Num value="))                    

while num > 0:              

    num = num -1

    if num == 3:

        continue

        print(num)

    print ('Number=', num)

Output 

Num value=4 

Number= 2 

Number= 1 

Number= 0 

For loop with break statement 

Example

List =[1,5,6,8,77,9,6,2.,7]

for i in List:

    if i==77:

        break

    print(i)

Output

1 5 6 8

For loop with continue statement 

List =[1,5,6,8,77,9,6,2,7] 

for i in List: 

 if i==77: 

     continue 

print(i)

Output

1 5 6 8 9 6 2 

7

For loop with pass statement 

numbers = [ 1, 2, 4, 3, 6, 5, 7,8, 9,10,11,12,13,14,15,16 ]

for number in numbers:

    if number % 2 == 0:

        pass

    else:

        print (number)

Output 

1 3 5 7 9 11 13 

15 


Part-1

Part-2

Machine Learning Tutorial

Free Online Tutorials

Artificial Intelligence(AI) Training in Jaipur 

Machine Learning(ML) Training in Jaipur  

by (101 points)
good study website
by (294 points)
in this chapter i have learned loop in python like as for,while  and nested loop.
by (123 points)
21-04-2020,
In this module i have learned about loops in paython with their example and syntax.
by (442 points)
edited by
# the following code not works properly with input { 1.11,1.12,1.13,1.14} Why
n=float(input("Enter n "))
i=int(n)
while n!=i:
  n*=10
  i=int(n)
print("Number Without Decimal Point ",i)
by (200 points)
In this module ,I have studied about different loops in python like ,for loop (for loop with different conditions),while loop and nested loops.
by (614 points)
if there is float value
by (422 points)
Loops are explained in detail.
by (294 points)
In  this chapter I learned python loops
Like as for,while and nested loop.
And also learned how it's implement
by Goeduhub's Expert (2.2k points)
@Manish Your Program can be written like this...this will work for all cases...
# the following code not works properly with input { 1.11,1.12,1.13,1.14} Why
n=float(input("Enter n "))
str1=str(n)
list1=str1.split('.')
d=len(list1[1])
n1=round(n,d)
i=int(n)
n2=n1
while n2!=i:
  n1*=10
  n2=round(n1,d)
  i=int(n2)
  print("i=",i,"n=",n2)
print("Number Without Decimal Point ",i)
by (135 points)
In this section we learn about loops-
For loop
While loop
Nested loop
And with different different statements.
by (100 points)
In this article, i learned about python loops. Syntax for while and for loops, range function, break, continue, pass statements. This article contains a good theory about loops and their uses.
0 like 0 dislike
by (391 points)
21/04/2020

Topic: Python Loops

The python loops has different types which has different uses. When i have few condition which are in orders i can use loops. It must code with range () function and i also learn three statements and how they get you different outcomes.
0 like 0 dislike
by (182 points)
Date:- 22/04/2020          module:- loops in python

In this module i have study about basic loop concept in python and i have study about for loop ,while loop with break , pass and continue statement also. Now i know how loops work and how we can apply.

Thank you !
0 like 0 dislike
by (294 points)

in this module i learned learn  Basic Loop concepts in python.  For loop and  While loop with   Break , Continue and Pass statements.

3.3k questions

7.1k answers

394 comments

4.6k users

Related questions

0 like 0 dislike
1 answer 240 views
0 like 0 dislike
1 answer 266 views
1 like 0 dislike
1 answer 283 views
0 like 0 dislike
5 answers 4.1k views
asked Oct 11, 2019 in Python Programming by Goeduhub Goeduhub's Expert (8.3k points)

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...