![if !IE]> <![endif]>
The keyword while followed by a test expression (which can be any valid expression), and a colon. Following the header is an indented body. The test expression is evaluated. If it evaluates to True, then the body of the loop is executed. After executing the body, the test expression is evaluated again. While test expression evaluates to True, the body of the loop is executed. When the test expression evaluates to False, the loop is terminated and execution continues with the statement following the body.
while n != 1:
if n%2 == 0: # n is even
n = n/2
else: # n is odd
n = n*3+1
The condition for this loop is n != 1, so the loop will continue until n is 1, which makes the condition false.
Each time through the loop, the program outputs the value of n and then checks whether it is even or odd. If it is even, n is divided by 2. If it is odd, the value of n is replaced with n*3+1. For example, if the argument passed to sequence is 3, the resulting sequence is 3,10, 5, 16, 8, 4, 2, 1.
Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.
for val in sequence:
Body of for
# Program to find the sum of all numbers stored in a list
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
Output: The sum is 48
print("The sum is", sum)
If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. It generates arithmetic progressions:
# Prints out the numbers 0,1,2,3,4
# for x in range(5):
This function does not store all the values in memory, it would be inefficient. So it remembers the start, stop, step size and generates the next number on the go.
The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop.
The working of break statement in for loop and while loop is shown below.
# Prints out 0,1,2,3,4
count = 0
count += 1 if
count >= 5:
The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.
The working of continue statement in for and while loop is shown below.
# Prints out only odd numbers - 1,3,5,7,9
# for x in range(10):
Check if x is even
if x % 2 == 0:
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
>>> while True:
.. pass # Busy-wait for keyboard interrupt (Ctrl+C)
This is commonly used for creating minimal classes:
Another place pass can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored:
... pass # Remember to implement this!
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.