Page 1 of 1

(Problem solved) Printout results differ from examples

Posted: Mon Feb 17, 2014 7:16 pm
by Delzun
I been trying examples from this site: http://docs.python.org/3/tutorial/controlflow.html but results I get are often different from examples given.

This is what site shows:
>>> 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')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
I used the same code, but got different looking output:
(2, 'is a prime number')
(3, 'is a prime number')
(4, 'equals', 2, '*', 2)
(5, 'is a prime number')
(6, 'equals', 2, '*', 3)
(7, 'is a prime number')
(8, 'equals', 2, '*', 4)
(9, 'equals', 3, '*', 3)
All printed text has annoying '-marks around them. This seems happen always when there is text and variables printed out together.

Some other thing shown in examples have also given to me different result or refused to work at all, giving some errors instead.
What could be causing this and how could I make outputs tidier?
This was done with latest version of Raspbian.

Re: Printout results differ from examples

Posted: Mon Feb 17, 2014 8:06 pm
by Douglas6
The examples from that link are using the latest version of Python, Python 3. Raspian by default uses Python 2.7, and there are some differences. I'm not sure how to 'upgrade' Raspian to Python3, (I'm a Luddite), but it may be as simple as specifying

Code: Select all

python3 example.py
There's probably an environment variable or other setting to default things to 3.

Re: Printout results differ from examples

Posted: Mon Feb 17, 2014 8:21 pm
by Delzun
Thanks, with version 2.7 examples I got everything to go as expected.

Should I change to Python 3 or could I just learn from older examples. Is there big diffence between the two?

Re: Printout results differ from examples

Posted: Mon Feb 17, 2014 8:53 pm
by Douglas6
That's a can of worms. I don't believe the differences are big for learning purposes. About half the replies will say "Yes, meet the future, get used to 3", and the other half will say "Stick with what works, let someone else bleed on the edge for awhile". The problem is that some 'standard' modules/libraries have not been updated for 3, and some code will only work with 3. Me, I'm sticking with 2.7. Ya pays your money and ya takes your chance. You CAN (already do?) have both installed, and switch between them, so again, no great consequences either way.

Re: Printout results differ from examples

Posted: Mon Feb 17, 2014 9:04 pm
by davef21370
Delzun wrote:Should I change to Python 3 or could I just learn from older examples. Is there big diffence between the two?
Personally (and I stand prepared for flak) I'd stick with Python 2.x while you get to grips with the language in general. There are far more examples and tutorials to look at than there are for Python 3 which, obviously, makes learning easier.
Once you're comfortable with the basics make the choice (or just do the right thing and stick with 2.x).

Dave.

ps. If they'd have just made the whole thing backward compatible things would have been a whole lot easier.

Re: Printout results differ from examples

Posted: Mon Feb 17, 2014 9:09 pm
by DougieLawson
Dave just stated my opinion in the grand python flame wars better than I could.

I've yet to fathom the reasons for the incompatibility.

Re: Printout results differ from examples

Posted: Mon Feb 17, 2014 9:31 pm
by croston
The major version number changed from 2 to 3 so some long term backwards incompatible improvements to the Python language could be made. If full compatibility was maintained, the 2.x series would have continued. Python 3 cleans up a few language inconsistencies:
[*] The print statement became a function so it could be overloaded
[*] The raw_input / input function was fixed for security reasons
[*] Integer division now behaves as expected - dividing integers produces a float. e.g 1/2 now returns 0.5 instead of 0 (a truncated integer)
[*] There are more changes but these are the ones that you will encounter as a beginner

These changes make Python 3 more beginner friendly. Python 2 is the past.

Re: Printout results differ from examples

Posted: Mon Feb 17, 2014 10:01 pm
by DougieLawson
croston wrote: These changes make Python 3 more beginner friendly. Python 2 is the past.
Are you able to explain why Raspbian still defaults to python 2.7 and haven't made python 3 the default interpreter. They aren't encouraging any of the novices to make the switch.

Re: Printout results differ from examples

Posted: Mon Feb 17, 2014 10:03 pm
by mahjongg
probably because most of the libraries are written for 2.x.

Re: Printout results differ from examples

Posted: Mon Feb 17, 2014 10:24 pm
by croston
mahjongg wrote:probably because most of the libraries are written for 2.x.
Most libraries are now written for both. The ones that aren't don't seem to be actively maintained. The maintainers have had since 2008 to update their code! This does not include the time in alpha or beta either.