Repeated Action Syntax
In this post, you will learn about how Python uses loops. We'll also look at their use cases along with some examples that you can test on this page!
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. Indentation is important for any type of loop in Python!
Let's look at an example while loop:
Note that the for loop does not require an indexing variable to set beforehand, as it's named when you declare it (i.e. when saying "for x in loop", you just named the iterator variable 'x'; you can instead say, for example, "for things in loop" and the iterator variable will be named 'things'.)
Let's look at some examples:
Types of Loops
"while" Loop
A while loop repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. While loops repeat as long as the certain condition is met. When the condition becomes false, the line immediately after the loop in program is executed. In its simplest form, the syntax looks like this: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. Indentation is important for any type of loop in Python!
Let's look at an example while loop:
"for" 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 similar to the "for each" loop in other languages. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc. Here's what the syntax looks like:Note that the for loop does not require an indexing variable to set beforehand, as it's named when you declare it (i.e. when saying "for x in loop", you just named the iterator variable 'x'; you can instead say, for example, "for things in loop" and the iterator variable will be named 'things'.)
Let's look at some examples:
Loop Control Statements
In Python, loops give you a way to execute a block of code repeatedly. However, sometimes you may want to exit a loop or skip a specific part of a loop when it meets a specific condition. This can be done using the loop control mechanism. Loop control statements change execution from its normal sequence. They can be used to alter or control the flow of loop execution based on specific conditions. Python supports the following control statements.
Break Statement
The break statement allows you to terminate the execution of the loop containing it and transfers execution to the statement immediately following the loop. Here's an example:
Continue Statement
The continue statement allows you to skip over the current iteration of any loop. In other words, it causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. Here's an example:
Pass Statement
The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. It is similar to a comment in the sense that nothing actually happens when the statement is executed. The difference is that while the Python interpreter executes the pass statement like a valid python statement, it ignores the comment statement completely. Here's an example:
Your turn!
If you want to try any of the code for yourself, all you have to do is copy it and paste it into the compiler below, and then hit run!
I like that Python implements the use of break, continue, and pass... I feel like that gives the programmer much more control over the loops they are working with. Again Python is kind of amazing me at it's simplicity up to this point. It is similar to other languages we have worked with but seems to even go a step further in making it that much simpler for the end user.
ReplyDeleteI agree! Python's developers created the language with the intent of making it easy to use, but still powerful. It's impressive that effectiveness and was not compromised for efficiency. This is likely another reason why Python is so popular compared to other languages that may not have the same balance.
Delete