Python Theory 3
Python Theory 3
Engineering College
Department of Computer Application
Assignment-3
Semester-1
Saikat Mazumder
Roll-25
Ans -
a) Break and Continue :
Break and continue are control statements that are used in
loop like for, while to alter the flow of execution.
Break Statement:
Usage : The break statement is used to terminate the loop it
is in.When a break statement is encountered inside a loop,
the loop iteration stops there, and control returns from the
loop immediately to the first statement after the loop.
Basically, break statements are used in situations when we
are not sure about the actual number of iterations for the
loop, or we want to terminate the loop based on some
condition.
When to use:
It is used when a certain condition is met, and you want to
exit the loop immediately, regardless of whether the loop's
iterations are complete.
Example:
Here you can see the loop is terminated after printing 0 to 5
because when i will be 6 the loop will encounter a break
statement and thereafter the loop is terminated.
Example:
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
Ans-
a) In Python, the range() function generates a sequence of
numbers. It's commonly used in loops to iterate a specific
number of times.
Basic Usage:
The range() function can be used in three different ways: