Loop conditions and Statements Loops to use in Python properly
Sections repeat in the program code, also uses Python to loop. Use While and For loops. The contribution shows, what options are there, grinding to properly use it.
Company to the topicIn outstaffing programming and data processing loops are essential.
A couple of short definitions for the beginning of The grinding head determines the condition of the loop. The program code that is repeated in a loop, is also known as the “loop body”. As soon as the loop body was executed, the condition is a jump to the beginning of the loop, and the loop body is executed from the front.
Simple loops are dissolved in the programming mostly with the “while”Statement, this is also true for Python. Here, the loop will run, as long as (engl: while) is a condition is true. A little more complex, the repetition works in a “for”loop, which is also referred to as a counting loop.
As soon as one arrives in a While loop, the value of the condition will execute the condition for the loop to False. This means that the loop body is not executed, and the end of the loop is.
Mostly worked in a loop with a variable that changes during the end of the loop. The value of a variable with the specified condition in a While loop, the program ends the loop. In the case of the variables, it can also be a counter, which determines how long the loop is executed to be.
While loop code execution to Meet the conditions
While used in a loop in Python, if sections are to be executed in a program until a condition is met. In the loop header is specified with while the condition. If the condition of the While loop returns the value “true”, executes Python the loop body if the condition is true, is False, the value of the execution of the loop.
In this case, the program jumps to the end of the loop body and proceeds with the execution of the program. The clarity of the code of the loop body is indented by one level. The Code after the loop body moves back one step to the left. Loops can also be nested.
In most cases, is here worked with a counter or a variable whose value is changed in the loop body until a certain condition is met. Before the While loop, the loop body is performed again, the program checks whether the condition of the variables to arrive already.
Loops with While use
In the case of a counting loop, the initial value of a variable to a specified value. In the loop body, the value is changed for so long, until the condition for the execution of the loop is False, for example:
x = 0
while x < 5:
print( x )
x += 1
Here the loop is executed as long, as long as x grows from 0 to 4. The value must be < 5, that is, when X=5, the loop will not execute the loop body once again. From the value 5 to x the condition of the loop to False, and the loop body is skipped. In this example, the program is 0,1,2,3,4 and terminates the loop.
In the While line, a colon at the end of the condition. When you Define the condition of a loop can be defined with the “and” a further condition for a loop.
If the condition of a While loop is never set to False accepts, running the infinite loop. That means that the loop body will never leave. Often are endless programmed loops with intention, but there is an error in the programming is, here is a lack of increment:
x = 0
while x < 5:
print( x )
Grinding cancel and multiple conditions use
With the “break”command from a point of the loop body, the possibility of loops cancel. This termination condition is often linked to an If-condition. With “else”, you can then define what should happen if the condition in the “if” is received.
A For loop is to use the Python
In addition to the While loops in Python often a For loop is used. For loops in Python are used to iterate object sequences. The for Loop in a For loop is, therefore, first of all, the key word “for”. Then an identifier, then the key word is “in” and finally, the render iterable object:
For <Variable> in <Objekt>:
Also in the case of For-indented loop, the loop body is a unit. On the Element used, iterierbaren object can be accessed in the loop body. Example:
orte = ["Berlin", "Stuttgart", "Frankfurt", "Hamburg"]
for x in orte:
print( x )
The output of the loop is:
With For-loops can be, therefore, lists, worked through, and for each Element from a list of specific actions to be carried out. In the above example, the For there are the places from loop sequentially, which were defined before the loop. Another example is:
for x in „Thomas“:
Here is the For loop the String “Thomas works” and displays the letters. The output in this example is:
For-can be used to grind for different types of data. In addition, For-may be canceled loop with break. Also, the use of “If….else“ with For loops.
Loops in Python: function range use
With For-can be used to loop the function “range”. This function creates a iterierbares object in the For loop to use. Here are all the Numbers in a range, for example, are usable. With “range()” lists are generated, which is basically arithmetic enumerations correspond to An example of the Sum of the Numbers from 1 to 10:
n = 10
x = 0
for i in range(1, n+1):
x = x + i
In this example, Python evaluates the function “range(1, n+1)”. A list is generated Numbers. These Figures, uses the loop to Calculate and use the variables that were created before the loop.
“Range” can also be a fixed area is defined, as well as the steps to be used to Generate the elements of the Objects: “, range(1, 100, 2)”. Here is a list of all the Numbers between 1 and 100 in the 2 series is created steps. This can also be used in reverse perform “range (100, 1, -2).
For-can be nested loops loops and While-with each other. It is easily possible in a for loop, a For-to use a loop, or a combination of the types of Loops to each other.