Loop in Python-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

While Loop in Python

In Python, a while loop is used to execute a block of statements repeatedly until a


given condition is satisfied. When the condition becomes false, the line immediately
after the loop in the program is executed.
While Loop Syntax:
while expression:
statement(s)
All the statements indented by the same number of character spaces after a
programming construct are considered to be part of a single block of code. Python
uses indentation as its method of grouping statements.
Example of Python While Loop
count = 0
while (count < 3):
count = count + 1
print("Hello ")

For Loop in Python


For loops are used for sequential traversal. For example: traversing
a list or string or array etc. In Python, there is “for in” loop which is similar
to foreach loop in other languages. Let us learn how to use for loop in Python for
sequential traversals with examples.
For Loop Syntax:
for iterator_var in sequence:
statements(s)
Example:
n=4
for i in range(0, n):
print(i)

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.

for letter in 'greetings from us':


if letter == 'e' or letter == 's':
break
print('Current Letter :', letter)
Output

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

start Optional. An integer number specifying at which position to start. Default is 0

stop Required. An integer number specifying at which position to stop (not included).

step Optional. An integer number specifying the incrementation. Default is 1

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy