Main Content
A reminder of the key issues to consider in moving from Python 2 to Python 3 Archive - Originally posted on "The Horse's Mouth" - 2016-10-30 10:50:20 - Graham Ellis
1. New style classes take on the old style class format
But does it matter if you explicity inherit in Python 3??
2. print moves from being a keyword to a function
Single value tuple as parameter will work in both cases
You may also do a sys.stdout.write
3. integer division using "/" returns a float
Use // if you need the whole number back
4. raw_input becomes input
Thank goodness ;-)
5. sort's callback changes to a "key =" type using a single parametered lambda
But remember you have things like sorted backported to 2.7
6. Text strings défaut to unicode in Python 3
7. You need all your modules and dependencies sorted out for Python 3 before you switch to that
8. You can't use new Python 3 stuff in Python 2 unless it's been backportd
In most cases, it's practical to write code that works with both Python2 and Python 3. However, if you must write something a bit different, you can use:
if sys.version_info < (3,0):
and you do need to remember that even if you do this, the syntax must be compatible with both the Python 2 and Python 3 compilers!
Some other articles
Y102 - Python - Fundamentals This article Mutable v Immuatble objects in Python, and the implication Learning to program - variables and constants BODMAS - the order a computer evaluates arithmetic expressions Formatting output - why we need to, and first Python example Some terms used in programming (Biased towards Python) Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. Beware - a=a+b and a+=b are different - Python Python - fresh examples from recent courses Learning to program in Python 2 ... and / or in Python 3 Variable storage - Perl, Tcl and Python compared Python - fresh examples of all the fundamentals Pascals Triangle in Python and Java Python - input v raw input Question on division (Java) - Also Perl, PHP, Python ... Integer v float - Python Python security - trouble with input Getting rid of variables after you have finished with them Copying a reference, or cloning Making programs easy for any user to start Y050 - Python - General This article Identifying the first and last records in a sequence Well House Consultants - Python courses / what's special. Public training courses - upcoming dates Additional Python courses added to our schedule A longer Python ... training course Using Python to analyse last years forum logs. Good coding practise discussion. Whether you have programmed before or not, we can teach you Python How well do you know Perl and / or Python? Python Programming class for delegates who have already self-taught the basics Shell - Grep - Sed - Awk - Perl - Python - which to use when? Want to escape the Olympics? Learn to program in the countryside! When you should use Object Orientation even in a short program - Python example Python - current versions and implementations (CPython, Jython, IronPython etc) Python courses and Private courses - gently updating our product to keep it ahead of the game Busy weekend of contrasts. Python through the Snow Python training courses for use with ESRI ArcMap software Learning to program in Python 2 ... and / or in Python 3 Learning to program in ... Two days of demonstration scripts in Python Learning to program - how to jump the first hurdles Great new diagrams for our notes ... Python releases Learning PHP, Ruby, Lua and Python - upcoming courses Learning Python - many new example programs Python - a truly dynamic language Python training Y199 - Python - Fast Start Equality (in Python) This article Some gems from Intermediate Python Some gems from an introduction to Python Python - current versions and implementations (CPython, Jython, IronPython etc) Python - a truly dynamic language Y300 - Python 3 - What is new, what's changed and why This article Convering from Python 2 to Python 3 - an update, and the 2to3 utility Why populate object with values as you construct them? Object and Static methods - what is the difference; example in Python 3 Progress on moving from Python 2 to Python 3 - training for both versions Testing in Python 3 - unittest, doctest and __name__ == __main__ too. Sorting in Python 3 - and how it differs from Python 2 sorting Moving from Python 2.6 to Python 3 Learning to program in Python 2 ... and / or in Python 3 Moving the product forward - ours, and MySQL, Perl, PHP and Python too Great new diagrams for our notes ... Python releases Python classes / courses - what version do we train on? The road ahead - Python 3 Python 2 to Python 3 / Python 3000 / Py3k Python 3000 - the next generation