0% found this document useful (0 votes)
9 views6 pages

3 types of python loops

The document explains three types of loops in Python: for loops, break statements, and continue statements. It details how for loops iterate over sequences like lists and strings, while break can exit a loop prematurely, and continue skips the current iteration. Examples are provided to illustrate the usage of each loop type.

Uploaded by

computerlaboow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

3 types of python loops

The document explains three types of loops in Python: for loops, break statements, and continue statements. It details how for loops iterate over sequences like lists and strings, while break can exit a loop prematurely, and continue skips the current iteration. Examples are provided to illustrate the usage of each loop type.

Uploaded by

computerlaboow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

3 types of python

loops
By Rithwick the chosen one
Python loop
• A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
• This is less like the for keyword in other programming languages, and works more
like an iterator method as found in other object-orientated programming languages.
• With the for loop we can execute a set of statements, once for each item in a list,
tuple, set etc.

• Example fruits = ["apple", "banana", "cherry"]


• for x in fruits:
• print(x)
Looping Through a String
• even strings are tolerable objects, they contain a sequence
of characters:
• Example
• Loop through the letters in the word "banana":
• for x in "banana":
• print(x)
The break Statement

• With the break statement we can stop the loop before it has looped
through all the items:
• Example
• Exit the loop when x is "banana":
• fruits = ["apple", "ban ana", "cherry"]
• for x in fruits: print(x)
• if x == "banana": break
The continue Statement

• With the continue statement we can stop the current iteration of the loop,
and continue with the next:
• Example
• Do not print banana:
• fruits = ["apple", "banana", "cherry"]
• for x in fruits:
• if x == "banana":
• continue
• print(x)
Thank you
• Good bye

You might also like