3 types of python loops
3 types of python loops
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.
• 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