Python (vi) Basic learning because there are so many deficiencies in the front, there's less here, but I'm still going to send it.

Source: Internet
Author: User

python3.5 use

Day Sixth:

Now let's talk about the use of the For loop

Write a piece of code to explain it first:

1  for  in range:2     print(i)

The first paragraph means that the loop I (variable) starts from 0 printing can only be printed 10 times

So when we're going to print I will print 10 times from 0 to 9

The last time you guessed the number, you could use for to optimize it.

1Number=232  forIinchRange (3):3Guess_age=int (Input ("Number :"))4     ifguess_age==Number :5         Print("Yes,you got it.")6          Break7     elifGuess_age>Number :8         Print("think smaller ...")9     Else:Ten         Print("Think bigger!") One Else: A     Print("You have tricd too many times.")

Careful comrade must have found that else did not appear in pairs with if, last time I said else means "other"

If it's not, it's the same, it's the other way around in the loop.

Let's continue to see the for statement

1  for  in range (0,10,2):2     print(i)

is not printed 0,2,4,6,8 these numbers ah, in the code that the 0 represents starting from 0, 10 represents the end of 10, the following 2 represents a jump 2 number.

What's the point, I'm going to tell you, for example, I'm asking 0 to 8648 in the middle is 1243 of the full number of multiples is not immediately get ah, programming charm is this.

For in-depth usage:

When the for encounters if:

Write a program to understand it

 for  in range    :print(i    )if i<5:        Print ('Huang')

Print out 10 numbers 5 "Hung" is not found only 0 to 4 followed by "Huang" because he executed the judgment he is a number a number of the loop when the loop to 5 when the if is not executed so there is no

  

Python (vi) Basic learning because there are so many deficiencies in the front, there's less here, but I'm still going to send it.

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.