Loop in Python-1
Loop in Python-1
Loop in Python-1
Output
0
1
2
3
Loop Control Statements
Loop control statements change execution from their normal sequence. When
execution leaves a scope, all automatic objects that were created in that scope are
destroyed. Python supports the following control statements.
Continue Statement
The continue statement in Python returns the control to the beginning of the loop.
Example
for letter in 'greetings from us':
if letter == 'e' or letter == 's':
continue
print('Current Letter :', letter)
Output
Current Letter : g
Current Letter : r
Current Letter : t
Current Letter : i
Current Letter : n
Current Letter : g
Current Letter :
Current Letter : f
Current Letter : r
Current Letter : o
Current Letter : m
Current Letter :
Current Letter : u
Break Statement
The break statement in Python brings control out of the loop.
Current Letter : g
Current Letter : r
range() function
Definition and Usage
The range() function returns a sequence of numbers, starting from 0 by default, and increments by
1 (by default), and stops before a specified number.
Syntax
range(start, stop, step)
Parameter Values
Parameter Description
stop Required. An integer number specifying at which position to stop (not included).
Example 1
x = range(3, 6)
for n in x:
print(n)
Output
3
4
5
Example 2
x = range(3, 20, 2)
for n in x:
print(n)
Output
3
5
7
9
11
13
15
17
19
Example 3
x = range(20)
for n in x:
print(n)
0Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
exit() function
The exit() function in Python is used to exit or terminate the current running
script or program. You can use it to stop the execution of the program at
any point. When the exit() function is called, the program will immediately
stop running and exit.
The syntax of the exit() function is:
exit([status])
Here, status is an optional argument that represents the exit status of
the program. The exit status is an integer value that indicates the
reason for program termination. By convention, a status of 0
indicates successful execution, and any non-zero status indicates an
error or abnormal termination.
If the status argument is omitted or not provided, the default value of
0 is used.
Here's an example usage of the exit() function:
print("Before exit")
exit(1)
print("After exit")
Output
Before exit