Size: 611
Comment: creation
|
Size: 1306
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 5: | Line 5: |
In version 7.2, SageMath is using the Python 2 syntax. It may soon switch to the Python 3 syntax, where print is a function. | In version 7.2, SageMath is using the Python 2 syntax for print. Later (no schedule yet) it will switch to the Python 3 syntax, where print is a function. |
Line 7: | Line 7: |
To convert from python2 to python3, you basically need to add parentheses, and write print("x") instead of print "x". | It is therefore a good idea to try to use print in a way that is compatible with both Python 2 and Python 3. |
Line 9: | Line 9: |
Here is a conversion table to help you adapt your code: | The easiest way to achieve this is to always use '''print(...)''' with a single string inside the parentheses. |
Line 11: | Line 11: |
|| Python 2 || Python 3 || | For example, '''print("He has eaten {} bananas".format(10))''' will work in both Python 2 and Python 3 and give the same result. Here below are some instructions for going from Python 2-only print to Python 3-only print. The Python 3 syntax can be used right now if you add the line {{{ from __future__ import print_function }}} as the first code line of your python files. ||<#FFFF66>To convert '''print''' from Python 2 to Python 3, you basically need to add parentheses, and write '''print("x")''' instead of '''print "x"'''.|| Here is a conversion table to help you adapt your code in more complex cases: ||<#98FF98> Python 2 ||<#98FF98> Python 3 || || print a || print(a) || |
Line 13: | Line 27: |
||print "%03d" % 7 || print("{:03d}".format(7)) || |
Behaviour of print
The behaviour of print differs in Python 2 and in Python 3.
In version 7.2, SageMath is using the Python 2 syntax for print. Later (no schedule yet) it will switch to the Python 3 syntax, where print is a function.
It is therefore a good idea to try to use print in a way that is compatible with both Python 2 and Python 3.
The easiest way to achieve this is to always use print(...) with a single string inside the parentheses.
For example, print("He has eaten {} bananas".format(10)) will work in both Python 2 and Python 3 and give the same result.
Here below are some instructions for going from Python 2-only print to Python 3-only print. The Python 3 syntax can be used right now if you add the line
from __future__ import print_function
as the first code line of your python files.
To convert print from Python 2 to Python 3, you basically need to add parentheses, and write print("x") instead of print "x". |
Here is a conversion table to help you adapt your code in more complex cases:
Python 2 |
Python 3 |
print a |
print(a) |
print a, b, c |
print(a, b, c) |
print x, |
print(x, end=" ") |
print >>sys.stderr, x |
print(x, file=sys.stderr) |