Python learning questions

Source: Internet
Author: User

I'm tired of learning python3. Many resources are based on 2, and 3 has been around for so many years. Why are there so many thon3 resources! For beginners, each test example requires Google to differentiate between 2 and 3, and then silently modify the code and perform tests, which affects learning efficiency.

Let's talk about the official documents I have read. I opened Python tutorial, 3.3.2, and a local Python version. I opened a page and tested an example, but found various differences.

Who can tell me why ???

The official document is written as follows:

In the section 4.4 break and continue, there is an example:

>>> for n in range(2, 10):...     for x in range(2, n):...         if n % x == 0:...             print(n, 'equals', x, '*', n//x)...             break...     else:...         # loop fell through without finding a factor...         print(n, 'is a prime number')

The output in the Guide is as follows:

2 is a prime number3 is a prime number4 equals 2 * 25 is a prime number6 equals 2 * 37 is a prime number8 equals 2 * 49 equals 3 * 3

It looks quite normal. Right?

I wrote the same code in the Local idle:

for n in range(2,10):for x in range(2,n):if n%x == 0:print(n, 'equals', x, '*', n//x)breakelse:print(n, 'is a prime number')

The running result is:

3 is a prime number
4 equals 2*2
5 is a prime number
5 is a prime number
5 is a prime number
6 equals 2*3
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
8 equals 2*4
9 is a prime number
9 equals 3*3

One problem is that 2 is missing. I can understand this, because when n = 2, range (2, n) should be empty, because range is left closed and right open, I don't know how 2 appears in the Guide.

Another problem is that break seems useless.

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.