What's the main difference between py2 vs py3?
-
From a beginner point of view what is the main changes in our Drawbot code when using Python 2 and Python 3?
Tx
-
For beginners there is not such a big difference:
Strings
All strings are unicode string, so no converting back and forth, but this is actually the best thing!!
print
print('hello world')
requires brackets(
and)
divisions
Divisions are always floats so no more rounding, this is also a good thing:
1/3
->0.333333
instead of0
You keep the old behaviour with
1//3
If you dive deeper:
dictionaries
dict.keys()
,dict.values()
,dict.items()
are not a lists anymoreLooping (a
for
loop or awhile
loop) over a dictionary and changing content of that dictionary is tricky:for key in list(myDict.keys()): myDict[key] = "changed" + myDict[key]
There is of course more
- Python2 or Python3
- What's New
- google it
-
Thanks! I'll look more into it.
-
I noticed today iterate a dict works also different.
While translating scripts from py2 to py3 this was a thing...If the keys are integers in py2 they will be sorted.
In py3 the will stay in order as appended to dict.print {1:"A",0:"B"} > {0: 'B', 1: 'A'}
vs
print({1:"A",0:"B"}) > {1: 'A', 0: 'B'}
(also it seems
print()
is not coloured in snippet)
-
@thom It's actually not generally true that keys get sorted in Py2. The order of a dict is in implementation detail that you should not rely on. If you need sorted keys: get sorted keys!
d = {1:"A",0:"B"} print(d) > {1: 'A', 0: 'B'} print(d.keys()) > dict_keys([1, 0]) print(sorted(d)) > [0, 1]
(Indeed print() not getting colorized may be a bug in the syntax colorizer. Or it's not yet fully Py 3 compatible.)
-
@justvanrossum improved... and requested here https://github.com/isagalaev/highlight.js/issues/1468