eLearning

We are a professional site that will give you the strongest methods to get learn and we will teach you everything about programming and technology

PYTHON

Breaking News

PYTHON
PYTHON
جاري التحميل ...

Python loops Lesson 5

Python loops Lesson 5:



After we learned in the previous lesson how to deal with conditions, and how to use them in programs; We will continue our journey to learn this beautiful language. In this lesson, we will learn how to deal with iteration loops such as the for loop and the while loop.

Loops enable you to repeat a code a number of times, you can set the number of iterations as you wish.

While:

While loop it repeats some code when the condition is met and does not stop until when the condition is false, and to create a while loop, the number of times it is repeated and set it as a condition must be determined and then the value of a variable must be increased by one, so that it increases until it reaches the specified number in the condition and stops. You can also specify a condition of another type so that the loop is not related to executing the code a certain number of times, but rather to the fulfillment of a certain condition (if a variable value becomes contrary to the value we specify)

Let's say we want to type "Hello" a hundred times, of course you might say and repeat the following sentence a hundred times:

print ("Hello")

This method is correct, but it costs a lot of time, and the programmer always thinks in a smarter way (do as many tasks as possible in the least time possible). Therefore, we cannot adopt this method. Programming languages ​​offer us simply iteration with the least number of lines.

For example, we can print "Hello" ten times in the following way. Note that the sentences following the word while are offset by four white spaces (Tab Button)

i = 0

while I <10:

    print "Hello"

    i = i +1

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Explanation of the example above:

Line 1: We are setting an initial value for the variable i. The second line: We add the condition that the value of the variable is less than the number ten, and if it is equal to 10, the program will stop. Line three: We print the word Line four: we are increasing the value of the variable

The first time, the interpreter will read the value of the variable and find it equal to the number 0, then verify that the condition that the value is less than 10 is true, and since the condition is met the first time (of course because 0 is less than 10), the program will continue working, and it will print the “Hello” Then the interpreter increases the value of the variable i by one (so that the new value is 1) the interpreter checks the validity of the condition again and since it is validated, the sentence "Hello" is printed a second time, after which it increases the value of the variable by one again so that the new value of the variable i is the number 2, which in turn Less than 10, so the sentence is printed a third time. And so it goes on until the value of the variable reaches the number 10, and the condition becomes false (because the number 10 is not less than 10 but is equal to it), and thus the repetition stops.

Example: accessing the items of a list, group, or row

There are several practical examples of using the While loop, for example, this iteration loop can be used to spin a list and print its elements,

see the following example:

I = 0

children = [John', 'Rinaldo', 'Ronaldo', 'Marco', 'Mark']

while i <len (children):

    print children [i]

    i = i + 1

John

Rinaldo

Ronaldo

Marco

Mark


In the above example, we created a list called children that contains five elements, then we printed each item at each iteration, and we set the necessity for the value of the variable I to be smaller than the number of list items that we set with the help of the function len, so that the program stops when the value of the variable reaches Issue 5.

Note: This is just an example of how to use the While loop. The for loop is usually used for such applications (rotating on lists, groups and classes…) because it is more flexible, and in the end, the choice is up to you (and you can use the method that suits you).

'

Loop iteration For

A For loop is another programming statement that enables iteration, which is one of the methods used to rotate around list values ​​as well and is characterized by defining the count variable at the beginning of the loop, meaning that unlike the While loop you do not need to define the variable before starting to write the loop, and the for loop does not need A specific condition, but the number of repetitions is determined according to the number of the assigned list items. See the following example, note that the sentences are subordinate to the line

for counter in range (1, 6)

Indented by four white spaces.

 for counter in range (1, 6):

      print counter, 'Hello World!'

1 Hello World!

2 Hello World!

 3 Hello World!

 4 Hello World!

 5 Hello World!

In the above example, we created a loop that rotates on a list that we created using the range function, and we assigned the frequency values ​​to the counter variable. The difference here from the while loop is that the counter variable is part of a list and not a variable that carries a value in each iteration.

Note: The range function creates a list of numbers arranged from smallest to largest according to two factors. The first is the starting number and the second represents the end, noting that the final number in the series does not count (in the following example, 1 is the first number and the final number is 6).

range (1, 6) (1, 2, 3, 4, 5)

The for loop enables to rotate the elements of a group (menus, rows, strings ...) by passing the name of the variable after the word in in the code. Notice that the strings following the for letter in v are offset by four white spaces (see the previous lesson).

v = "Hello"

 for letter in v:

     print letter

H

e

l

l

o

In the example above, we assign the value "Hello" to the variable v and then use the for loop to print each letter of that word that we assign to the variable letter.

As we saw before, for can also be used to iterate a code a number of times, as we did with the while loop with the help of the range function:

for i in range (0,100): print i

We can also use the for loop to pass through the keys of a dictionary to get each key on Sharpness, see the following example:

a = {'x': 1, 'y': 2, 'z': 3}

for key in a:

    print key

x

y

z

Stop loop with the statement: break

A while loop can stop at a certain point in the break statement, and the breakpoint can be determined using the if statement, see the example:

i = 0

while i <30:

    if i> 9:

    print "Stop the loop"

    break

    print i

    i = i + 1

  0

 1

 2

 3

 4

 5

 6

 7

 8

 9

  Stop the loop

In the above example, the loop was supposed to complete execution until the variable I reached the value of 30, but it stopped as soon as the value of the variable reached 9 and this is because we set a condition to stop the iteration when the value of i becomes greater than 9 and since the condition became true, the program stopped because we ordered the bulk break.

It is also possible for a for loop to stop at a specific point in the break sentence, and the breakpoint can be determined using the if statement:

 list = [1,5,10,15,20,25]

 for i in list:

      if i> 15:

      print "Stop the loop"

      break

      print i

1

 5

 10

 15

 Stop the loop

In the example above, the list rotation stopped after the variable reached the value 15, which is the condition that we put in the if statement.

Ignore the code implementation in a loop with the statement: continue

It can stop an iterative loop of the for type at a certain point and then complete the iteration in the next step, that is, jump by step on iteration, by using the sentence continue. Condition, see the example:

 list = range (1, 9)

 for i in list:

     if i == 4:

     print "Continue the loop"

     continue

     print i

    1

    2

    3

    Continue the loop

    5

    6

    7

    8 

عن الكاتب

ADMIN

التعليقات


اتصل بنا

If you like the content of our blog, we hope to stay in constant contact, just enter your email to subscribe to the blog express mail so that you will receive the new blog first-hand, and you can send a message by clicking the next button ...

جميع الحقوق محفوظة

eLearning