python-day1-05-Cycle

Source: Internet
Author: User

  1. The cycle is that the computer repeatedly help us do one thing, people hate to do the repetition of dull things, like the grind, people feel boring, and then have a donkey, a circle of cycle, people a lap of a circle of push not exhausted, so the computer is the same, when there are repetitive work, and the same computer, when there are repetitive work, With the computer command, you repeat to do, then the Python used to command the syntax is only while and for two.
  2. While Loop
    1. As the name implies, when .... , that is, when the condition is met, I run while the following code, conversely, when not satisfied with the time, we do not run, see here is not the feeling and the above if the judgment is the same? Yes, that's the same thing, but the difference between the while is the loop, meaning that I run the while the following code, and then run to the while to start here, continue to judge, if the code for the real run the following, for the false then end, and then run out and return to the judging condition here, which you also found, As long as the condition has been true, then he will run nonstop, like the perpetual motive, this is called the cycle of death, so we use to judge this condition, can not be a constant, judging conditions need to change in real time, or is a dead loop, will never end, look at the pseudo-code:
    2. While I am a rookie:
      The hardest class, the lowest salary.
    3. If I do not study hard, enterprising, has been a rookie, I have been circulating this state, this is the cycle of death. So how to end this cycle of death, of course, is the effort to improve themselves, change the value of the judging condition, not the end of it, so we need this while condition is a change in the value of the line, otherwise it has been running, in Python, commonly used is a counter to count, When the count reaches the expected value, it terminates the loop and looks at the code.
    4. 1i = 0#define a variable that is used to determine2  whileI&LT;=10:#determine if the value of the variable i is <=10, meet the conditions and go down3I +=1#count, after each cycle I self-increment 14     Print(i)#print I, this will print 1-115 Else:6     Print(' Over')#This will be the end of the production, so after printing 1-11, it will print over

    5. Break and continue in the while loop:
      • Break that breaks the cycle, forcing the end, while the loop as long as the end of breaking, this time is like the donkey is sick, no longer cyclic pull mill, the direct end;
      • 1i =02  whilei<=10:3I+=14     ifi = = 5:#when the value of I equals 5 o'clock, the following break is performed5          Break    #while encountering a break, the entire loop is ended, so the execution of this code is 1-46     Print(i)7 Else:#when the upper while loop ends normally, run else8     Print(' Over')#This is not the normal end, so print 1-4, do not print over

      • Continue is to end this cycle, continue the next cycle, like grinding, to re-repair, this lap did not run out, and then back to the beginning of the start of the drawing, re-pull grinding. The code is as follows:
      • 1i =02  whileI <=10:3I +=14     ifi = = 5:#when the value of I equals 5 o'clock, execute the following continue statement5         Continue  #when the while encounters the continue, it ends the loop and continues the next loop, so the result of this code execution is 1-4,6-116     Print(i)7 Else:#when the upper while loop ends normally, run else8     Print(' Over')#This will end normally, so after printing the 1-4,6-11, it will print over

  3. For loop
    1. For the translation is "in order to", Python here is what the loop, for example, followed by a list, in order to list the elements of the loop, loop to take the elements of the table, like the back with their own children, certainly can not discriminate, all have to take care of.
    2. The For loop must be followed by an iterative object, what is an iterative object? You can think of it as a candied fruit, and then naturally understand that the object of this iteration is a string of candied fruit that can take one down at a time, which is an iteration. The For loop is to take one candied fruit at a time until it is finished, which means you have a few candied fruit to loop a few times.
    3. This understanding should be better understood, so what are the iterative objects in Python? There are only a few more of them:
      1. String: Takes one character at a time
      2. List: Take one element at a time
      3. Ganso: Take one element at a time
      4. Collection: Take one element at a time
      5. Dictionary: Each time you take a dictionary key ()
      6. Non-iterative: numbers, decimals
  4. With a For loop, it is more convenient to compare while, the basic will be used for the loop, because unlike while need to set up a calculator, for never worry about going into the dead loop, it iterates after the value of the iteration object, it ends, there is no dead loop, So it's better than the while at this point. The other is similar to the while syntax above and the code is as follows
    • Break statement
    • 1  forIinch['a','b','C']:2     ifi = ='b':#when the value of I equals ' B ', the following break is performed3          Break       #for when a break is encountered, it ends the entire loop, so the result of this code is ' a '4     Print(i)5 Else:#when the above for loop ends normally, run else6     Print(' Over')#This is not the normal end, after printing ' a ', do not print over

    • Continue statements
    • 1  forIinch['a','b','C']:2     ifi = ='b':#when the value of I equals ' B ', perform the following continue3         Continue    #when the For Loop encounters continue, the current loop ends and the next loop is executed, so the result of this code is ' a ' C '4     Print(i)5 Else:#when the above for loop ends normally, run else6     Print(' Over')#this ends normally, after printing ' a ' C ', print over

  5. For loop remember a few points and then say it in the back
    • The loop list is essentially based on the elements in the subscript loop.
    • When looping the list, remember not to delete the list element, or it will result in a value disorder, workaround, set a new list with the Copy method
    • The For loop can be used as long as the object can be evaluated by the subscript.
    • Looping a file object is essentially the line in the loop file
    • Cycle dictionary, the essence is the loop key
  6. After-school practice
    1. Combined with the above input, conditional judgment and cycle of knowledge points, to write a guess the number of small games, game rules: Generate a 1-100 random number, receive user input, judge input and random values, equal print guessed right, the game is over; input value is greater than random value, prompt guess big, continue The input value is less than the random value, the hint guess is small, continue. Generate random number, use module random, method: Num=random.randint (1,101), num is a random number.
    2. 1 ImportRandom2Random = Random.randint (1,101)3 Print(Random)4i =05  whileI <Random:6I+=17 8Input_num = input ('Please enter a number')9Input_num =float (input_num)Ten  One     ifInput_num = =Random: A         Print("guess right.") -          Break -     elifInput_num>Random: the         Print("big guess.") -     Else: -         Print("small guess.")

python-day1-05-Cycle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.