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.