How To Use Python Loops Explain Briefly Details

How To Use Python Loops Explain Briefly Details
How To Use Python Loops Explain Briefly Details 


How To Use Python Loops Explain Briefly Details

Python Loop: A loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. 

Iterating over a sequence is called traversal. For loop is used for sequential traversal. It can be used to traverse strings or arrays.


Some Others Languages Loops

👉C++ Loops 

👉Java Loops 

👉C Loops


Syntax of for Loop

for variable in sequence:

//Statement


Here, the variable is that takes the value of the item inside the sequence on each iteration. 
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.


How To Use for Loop in Python?

list=[1,2,4,5,6,7,8,9]


for i in list:


    print(i)


*****OUTPUT*****


1 2 3 4 5 6 7 8 9


Python Range() for Loop

A range function generate a sequence of number using range function.like range(5) will generate number 0 to 4 number.sequence of range function is range(start,stop,step_size).


Syntax of for range Loop

for variable in range(value):

//Statement


How To Use for range Loop in Python?

n=int(input("Enter any number:"))


for i in range (n):


    print(i)


*****OUTPUT*****


Enter any number:10


0 1 2 3 4 5 6 7 8 9


How To Use for Loop string in Python?

n=(input("Enter Your Name:"))


for i in str (n):


    print(i)


*****OUTPUT*****


Enter Your Name:icoderweb


I c o d e r w e b



How To Use for Loop List in Python?

names=["Ravi","Pradeep","Rahul","Jitendra"]


for I in names:


    print(i)


*****OUTPUT*****


Ravi


Pradeep


Rahul


Jitendra


How To  Use for Loop Table in Python?

n=int((input("Enter any Number:")))


print("Your Table printed Below")


for i in range (1,11):


    print(i*n)


*****OUTPUT*****


Enter any Number:2


Your Table printed Below


2


4


6


8


10


12


14


16


18


20


Python While Loop: A while loop in python is used to iterate over a block of code as long as the test condition is true.

Syntax of while Loop

while condition:

//Statement


How To  Use while Loop in Python?

n=int((input("Enter any Number:")))


i=1


f=1


while i<=n:


    f=f*i


    i=i+1


print("factorial number:",n,"is",f)


*****OUTPUT*****


Enter any Number:5


factorial number: 5 is 120


Python Break and Continue Using Loop

Break Statement: A break statement is used to terminate the loop containing. 

Is Control of the program flowing to the statement immediately after the body of the loop?


Syntax of Break Statement

break

//Statement


How To Break Statement in Python?

name="Ravi","Pradeep","Rahul","Jitendra"


for n in the name:


    if n=="Jitendra":


        break


    print(n)


print("Loop is Terminate")


*****OUTPUT*****


Ravi


Pradeep


Rahul


Loop is Terminate


Continue Statement: A continue Statement is used to skip the value in this continue statement. it is a very most and useful statement in the python language.

Syntax of continue Statement

continue

//Statement


How To  Use Continue Statement in Python?

name="Ravi","Jitendra","Rahul","Vijay","Sunil"


for n in the name:


    if n=="Jitendra":


        continue


    print(n)


print(""Jitendra Name Not Print Because skip"")


*****OUTPUT*****


Ravi


Rahul


Vijay


Sunil


Jitendra Name Not Print Because skip


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.