Goeduhub Technologies Free Online Tutorials || COVID-19 Projects(AI-ML) || Go to your University  ||  Placement Preparation  
  Register for Project Based Online Training in Artificial Intelligence(AI) & Machine Learning(ML): From 15-08-2020 || Last Batch Student's Projects
0 like 0 dislike
563 views
in Tutorial & Interview questions by Goeduhub's Expert (7.9k 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.

4 Answers

1 like 0 dislike
by Goeduhub's Expert (2.8k 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 

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 (1.6k 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 (411 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.

Related questions

0 like 0 dislike
1 answer 32 views
0 like 0 dislike
1 answer 34 views
1 like 0 dislike
1 answer 40 views
0 like 0 dislike
5 answers 274 views
1 like 0 dislike
1 answer 14 views
  Realize your learning potential with courses starting at ₹ 420
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Online Free Training:  MACHINE LEARNINGPython Programming | Database Management System(DBMS) | Object Oriented Programming(OOPs) using C++ | Data Structures and Algorithms(DSA) | Java Programming | Linux/Unix | C Programming
Exams: List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...