Você está na página 1de 28

Python Loops

(Week6)

https://www.tutorialspoint.com/python3/python_basic_operators.htm
Overview

 While loop
 For loop
 Break statement
 Continue statement
Loops
A loop statement allows to execute a statement or
group of statements multiple times
While loop
 Repeats a statement or group of statements while
a given condition is TRUE.
 It tests the condition before executing the loop
body.

 Syntax

 statement(s) may be a single statement or a block


of statements with uniform indent.
While loop
 A while loop statement in Python programming
language repeatedly executes a target statement
as long as a given condition is true.
While loop
 Same number of character spaces after a
programming construct are considered to be part
of a single block

 Python uses indentation as its method of


grouping statements

 While loop is that the loop might not ever run.


When the condition is tested and the result is
false
While loop (example)
Infinite loop
 A loop becomes infinite loop if a condition never
becomes FALSE

 This results in a loop that never ends

 Might be useful in client/server programming


where the server needs to run continuously
Infinite loop (example)
Single Statement
 If loop body consist of only one statement then it
can be used as follow
else Statement with while loop
 Python supports having an else statement
associated with a loop statement
 the else statement is executed when the
condition becomes false.
For statement
 For statement in Python has the ability to iterate
over the items of any sequence,
 such as a list or a string

 Syntax
range() function
 built-in function range() is used to iterate over a
sequence

 It generates an iterator of arithmetic


progressions.
For statement (example)
Iterating by Sequence Index
 An alternative way of iterating through each item
is by index offset into the sequence itself
else Statement with for loop
 else block is executed only if for loops terminates
normally
 But it is ignored on encountering break statement
Loop Control Statements
 Loop control statements change the execution
from its normal sequence

 break statement
 continue statement
 pass statement
break statement
 Terminates the loop statement and transfers
execution to the statement immediately following
the loop
 most common use of break is when some
external condition is triggered requiring a hasty
exit
break (example)
break (example)
continue statement
 Causes the loop to skip the remainder of its body
and immediately retest its condition prior to
reiterating
 continue statement in Python returns the control
to the beginning of the current loop
continue (example)
Sample 1
 Write a Python program that calculates the sum
of the numbers from 1 to 100
Sample 1
 Write a Python program that calculates the sum
of the numbers from 1 to 100
Sample 2
 Use an if else statement in combination with a
while loop to calculate the sum of natural
numbers upto num
Sample 2
 used an if else statement in combination with a
while loop to calculate the sum of natural
numbers upto num
Sample 3
 Write a program to find a factorial of a number
Sample 3
 Write a program to find a factorial of a number

Você também pode gostar