SlideShare a Scribd company logo
Moving To Python 3 Why? When? How? Nick Efford (http://about.me/nickefford)
Backwards Compatibility An ‘unwritten law’ of software... ...and yet Python 3 breaks it – so badly that “Hello World!” no longer works! $ python3 hello.py File "hello.py", line 1 print 'Hello World!' ^ SyntaxError: invalid syntax Why?
Compatibility Isn’t Free... Java is bloated because obsolete features are never removed
Python’s Brave Decision "The language has two choices: either continue to bear the burden of what are now considered poor design decisions... or suck it up and let us try and fix some of these problems. It's like going to the dentist; it may hurt, but if that minor toothache goes untreated and develops into an abscess, you will wish you were dead." –  blog entry by Collin Winter
Desirable Language Attributes Minimal redundancy
Maximal separation of concerns
Minimal surprise
Conceptual consistency
Appropriate complexity
Redundancy: Iteration in Java for (int i = 0; i < message.length(); ++i) { System.out.println(message.charAt(i)); } for (int i = 0; i < messageChars.length; ++i) { System.out.println(messageChars[i]); } 2 different ‘classic’ syntaxes for iterating over strings & arrays...
Redundancy: Iteration in Java for (int i = 0; i < vec.size(); ++i) { System.out.println(vec.elementAt(i)); } for (int i = 0; i < vec.size(); ++i) { System.out.println(vec.get(i)); } Enumeration<Integer> enumerator = vec.elements(); while (enumerator.hasMoreElements()) { System.out.println(enumerator.nextElement()); } Iterator<Integer> iterator = vec.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } ...+ 4 for vectors...
Redundancy: Iteration in Java for (int number : vec) { System.out.println(number); } for (char character : messageChars) { System.out.println(character); } ...+ 2 newer approaches for vectors & arrays...
Redundancy: Iteration in Java // Before JDK 1.5 BufferedReader inputFile = new BufferedReader( new FileReader(&quot;foo.txt&quot;)); String line = inputFile.readLine(); while (line != null) { System.out.println(line); line = inputFile.readLine(); } // Since JDK 1.5 Scanner inputFile = new Scanner(new File(&quot;foo.txt&quot;)); while (inputFile.hasNextLine()) { System.out.println(inputFile.nextLine()); } ...+ 2 for text files  =  10 different iteration syntaxes!
Redundancy: Iteration in Python for character in string: print character for number in numbers: print number input_file = open('foo.txt') for line in input_file: print line, One syntax  works for strings, lists and text files!
Redundancy: Integer Representation Java: four primitive integer types, four corresponding wrapper classes,  BigInteger  class Python 2: two integer types –  int  &  long Python 3: one integer type –  int
Redundancy: Object Model Two types of class in Python 2: class Foo: ... class Bar(object): ... Only ‘new-style’ classes exist in Python 3 (either syntax can be used) ‘old-style’ class ‘new-style’ class
Redundancy: Console Input Python 2 has  two  functions providing console input: raw_input , yielding input as a string input , yielding the result of calling  eval  on input Python 3 has one function,  input , with the same behaviour as Python 2’s  raw_input (New programmers also find this less surprising...)
Conceptual Consistency: I/O In Python 2,  print  is a statement In Python 3, it is a  function Benefits: Consistency with console input (also a function)
Greater flexibility provided by keyword arguments print(text, file=output_file)   print(x, y, z, sep=':')
Minimal Surprise: Integer Division >>> 5 / 3 1.6666666666666667 >>> 5 // 3 1 Python 3 >>> 5 / 3 1 Python 2 (like C, C++, Java – but unlike proper arithmetic)

More Related Content

PDF
Python Workshop
PDF
Basic Concepts in Python
PDF
Python made easy
PPTX
Intro to Python Programming Language
DOCX
PYTHON NOTES
 
PPTX
Learn python – for beginners
PPTX
Python Basics
Python Workshop
Basic Concepts in Python
Python made easy
Intro to Python Programming Language
PYTHON NOTES
 
Learn python – for beginners
Python Basics

What's hot (20)

PPTX
Introduction to python
PPTX
Python basics
PDF
Python Tutorial
PPT
Python ppt
PPTX
Python basics
PPTX
Python Tutorial Part 1
PPTX
Python 3 Programming Language
PPTX
Python ppt
PPTX
Python 101: Python for Absolute Beginners (PyTexas 2014)
PPT
Python Programming Language
PDF
Python Loop
PPTX
Python Session - 2
PPTX
Fundamentals of Python Programming
PPTX
Learn Python The Hard Way Presentation
PPT
Python ppt
PPTX
Learning Python - Week 2
ODP
Python Presentation
PDF
Zero to Hero - Introduction to Python3
PPTX
Python Seminar PPT
Introduction to python
Python basics
Python Tutorial
Python ppt
Python basics
Python Tutorial Part 1
Python 3 Programming Language
Python ppt
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python Programming Language
Python Loop
Python Session - 2
Fundamentals of Python Programming
Learn Python The Hard Way Presentation
Python ppt
Learning Python - Week 2
Python Presentation
Zero to Hero - Introduction to Python3
Python Seminar PPT
Ad

Viewers also liked (7)

PPTX
Next generation web protocols
ODP
Technology of the future
PPSX
Future Technology
PPT
Future Technologies Presentation
PPT
Future Of Technology
PPTX
Future technology
PPT
Technology powerpoint presentations
Next generation web protocols
Technology of the future
Future Technology
Future Technologies Presentation
Future Of Technology
Future technology
Technology powerpoint presentations
Ad

Similar to Moving to Python 3 (20)

PPT
Os Vanrossum
PPT
Python 3000
PPTX
Python PPT by Sushil Sir.pptx
DOC
Python 2 vs Python 3 Key Differences.doc
PPT
Python Evolution
ODP
Programming Under Linux In Python
PPTX
A deep dive into python and it's position in the programming landscape.pptx
ODP
Introduction to Python
PPT
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
PPTX
Python-Yesterday Today Tomorrow(What's new?)
PDF
What is python
PPTX
What is Python?
PDF
Python Foundation – A programmer's introduction to Python concepts & style
PDF
Python tour
PPT
Python
PPT
1-ppt-python.ppt
PPTX
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
PPT
Python for students step by step guidance
PPT
Introduction to Python For Diploma Students
Os Vanrossum
Python 3000
Python PPT by Sushil Sir.pptx
Python 2 vs Python 3 Key Differences.doc
Python Evolution
Programming Under Linux In Python
A deep dive into python and it's position in the programming landscape.pptx
Introduction to Python
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
Python-Yesterday Today Tomorrow(What's new?)
What is python
What is Python?
Python Foundation – A programmer's introduction to Python concepts & style
Python tour
Python
1-ppt-python.ppt
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
Python for students step by step guidance
Introduction to Python For Diploma Students

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Electronic commerce courselecture one. Pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Machine learning based COVID-19 study performance prediction
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Empathic Computing: Creating Shared Understanding
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Electronic commerce courselecture one. Pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks
Per capita expenditure prediction using model stacking based on satellite ima...
Unlocking AI with Model Context Protocol (MCP)
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine learning based COVID-19 study performance prediction
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25 Week I
The Rise and Fall of 3GPP – Time for a Sabbatical?
Dropbox Q2 2025 Financial Results & Investor Presentation
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Network Security Unit 5.pdf for BCA BBA.
Empathic Computing: Creating Shared Understanding

Moving to Python 3

  • 1. Moving To Python 3 Why? When? How? Nick Efford (http://about.me/nickefford)
  • 2. Backwards Compatibility An ‘unwritten law’ of software... ...and yet Python 3 breaks it – so badly that “Hello World!” no longer works! $ python3 hello.py File &quot;hello.py&quot;, line 1 print 'Hello World!' ^ SyntaxError: invalid syntax Why?
  • 3. Compatibility Isn’t Free... Java is bloated because obsolete features are never removed
  • 4. Python’s Brave Decision &quot;The language has two choices: either continue to bear the burden of what are now considered poor design decisions... or suck it up and let us try and fix some of these problems. It's like going to the dentist; it may hurt, but if that minor toothache goes untreated and develops into an abscess, you will wish you were dead.&quot; – blog entry by Collin Winter
  • 5. Desirable Language Attributes Minimal redundancy
  • 10. Redundancy: Iteration in Java for (int i = 0; i < message.length(); ++i) { System.out.println(message.charAt(i)); } for (int i = 0; i < messageChars.length; ++i) { System.out.println(messageChars[i]); } 2 different ‘classic’ syntaxes for iterating over strings & arrays...
  • 11. Redundancy: Iteration in Java for (int i = 0; i < vec.size(); ++i) { System.out.println(vec.elementAt(i)); } for (int i = 0; i < vec.size(); ++i) { System.out.println(vec.get(i)); } Enumeration<Integer> enumerator = vec.elements(); while (enumerator.hasMoreElements()) { System.out.println(enumerator.nextElement()); } Iterator<Integer> iterator = vec.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } ...+ 4 for vectors...
  • 12. Redundancy: Iteration in Java for (int number : vec) { System.out.println(number); } for (char character : messageChars) { System.out.println(character); } ...+ 2 newer approaches for vectors & arrays...
  • 13. Redundancy: Iteration in Java // Before JDK 1.5 BufferedReader inputFile = new BufferedReader( new FileReader(&quot;foo.txt&quot;)); String line = inputFile.readLine(); while (line != null) { System.out.println(line); line = inputFile.readLine(); } // Since JDK 1.5 Scanner inputFile = new Scanner(new File(&quot;foo.txt&quot;)); while (inputFile.hasNextLine()) { System.out.println(inputFile.nextLine()); } ...+ 2 for text files = 10 different iteration syntaxes!
  • 14. Redundancy: Iteration in Python for character in string: print character for number in numbers: print number input_file = open('foo.txt') for line in input_file: print line, One syntax works for strings, lists and text files!
  • 15. Redundancy: Integer Representation Java: four primitive integer types, four corresponding wrapper classes, BigInteger class Python 2: two integer types – int & long Python 3: one integer type – int
  • 16. Redundancy: Object Model Two types of class in Python 2: class Foo: ... class Bar(object): ... Only ‘new-style’ classes exist in Python 3 (either syntax can be used) ‘old-style’ class ‘new-style’ class
  • 17. Redundancy: Console Input Python 2 has two functions providing console input: raw_input , yielding input as a string input , yielding the result of calling eval on input Python 3 has one function, input , with the same behaviour as Python 2’s raw_input (New programmers also find this less surprising...)
  • 18. Conceptual Consistency: I/O In Python 2, print is a statement In Python 3, it is a function Benefits: Consistency with console input (also a function)
  • 19. Greater flexibility provided by keyword arguments print(text, file=output_file) print(x, y, z, sep=':')
  • 20. Minimal Surprise: Integer Division >>> 5 / 3 1.6666666666666667 >>> 5 // 3 1 Python 3 >>> 5 / 3 1 Python 2 (like C, C++, Java – but unlike proper arithmetic)
  • 21. Separation of Concerns: Text vs Binary Data Python 2: str for ASCII text strings & byte strings unicode for Unicode strings Two representations of text, overlapping with one for binary data! Python 3: str for Unicode text strings bytes for strings of bytes Clean separation of text and binary representations, with encode & decode methods for conversion
  • 22. So Python 3... Is less redundant
  • 26. Time To Change? Python 3 has matured to the point where it is now ‘production-ready’
  • 27. Development of the Python 2 line has come to an end with Python 2.7 (apart from bug fixes)
  • 28. Focus for future innovation will be Python 3 (notwithstanding Jython, IronPython, PyPy, etc)
  • 29. A Helpful Tool: 2to3 Distributed with Python 3
  • 30. Converts Python 2 code to Python 3
  • 31. Integrates with D istutils and can run during installation, allowing you to support 2 & 3 from one codebase
  • 32. Cannot do a perfect job; you may need more unit tests and may have to refactor your code a bit
  • 33. Only handles Python code; C extensions need to be converted to Python 3 API by hand
  • 34. Obstacles Support from third-party libraries & frameworks – ‘chicken & egg’ situation
  • 35. Availability of books, online tutorials, etc
  • 36. Running multiple Python versions alongside each other is a pain ( virtualenv helps a lot)