[go: up one dir, main page]

0% found this document useful (0 votes)
12 views10 pages

Loop

The document provides an overview of Python loops, specifically while and for loops, including their syntax and examples for printing numbers from 1 to 10. It also explains the use of break and continue statements to control loop flow, with examples demonstrating their functionality. Overall, it serves as a basic guide to understanding and implementing loops in Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Loop

The document provides an overview of Python loops, specifically while and for loops, including their syntax and examples for printing numbers from 1 to 10. It also explains the use of break and continue statements to control loop flow, with examples demonstrating their functionality. Overall, it serves as a basic guide to understanding and implementing loops in Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Loops

Python has two primitive loop commands:

 while loops
 for loops

Loop Flow diagram


while loop Syntax

while expression:
statements

For Loop Syntax

for var in iterable:


statements

Q) Write a python program to print 1 to 10.

print(“1”)
print(“2”)
print(“3”)
print(“4”)
Print(“5”)
print(“6”)
print(“7”)
print(“8”)
print(“9”)
Print(“10”)
Using while loop –
n=1
while n <= 10:
print(number)
n=n+1

Using For loop –


for n in range(10):
print(n)

Q) Write a program to print all the odd number till 100.


Python break and continue
In programming, the break and continue statements are used to alter the
flow of loops:
 break exits the loop entirely
 continue skips the current iteration and proceeds to the next one

Working of Python break Statement


Example –
for i in range(5):
if i == 3:
break
print(i)
Output -
0
1
2
Working of continue Statement in Python
Example –
for i in range(5):
if i == 3:
continue
print(i)
Output-
0
1
2
4

You might also like