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

Practpython

The document discusses the break and continue statements in Python. It provides examples of using break to terminate a loop when a condition is met, and using continue to skip the remaining statements in the current loop iteration and continue to the next one. It also includes programs demonstrating the use of functions and recursion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Practpython

The document discusses the break and continue statements in Python. It provides examples of using break to terminate a loop when a condition is met, and using continue to skip the remaining statements in the current loop iteration and continue to the next one. It also includes programs demonstrating the use of functions and recursion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

The break Statement:

The break statement in Python terminates the current loop and resumes execution at the next
statement, just like the traditional break found in C.

The most common use for break is when some external condition is triggered requiring a hasty
exit from a loop. The break statement can be used in both while and for loops.

Example:

for letter in 'Python': # First Example


if letter == 'h':
break
print 'Current Letter :', letter

var = 10 # Second Example


while var > 0:
print 'Current variable value :', var
var = var -1
if var == 5:
break

print "Good bye!"

This will produce the following result:

Current Letter : P
Current Letter : y
Current Letter : t
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!

The continue Statement:


The continue statement in Python returns the control to the beginning of the while loop. 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.

The continue statement can be used in both while and for loops.
Example:
#!/usr/bin/python

for letter in 'Python': # First Example


if letter == 'h':
continue
print 'Current Letter :', letter

var = 10 # Second Example


while var > 0:
var = var -1
if var == 5:
continue
print 'Current variable value :', var
print "Good bye!"

This will produce following result:

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Good bye!
6. Program using Functions.

def test_prime(n):
if (n==1):
return False
elif (n==2):
return True;
else:
for x in range(2,n):
if(n % x==0):
print("not a prime")
print("Given no is prime")

n = int(input(“Give the number to check prime or not :”))


test_prime(n)

7.Program using Recursion.

def fact(n):
if n == 0 or n ==1 :
return 1
else:
return (n * fact(n-1))

n = input("enter any number from 0-100 to find factorial")


print(fact(n))

You might also like