Sobes.tech
Junior — Middle

What are the main differences between Python 2 and Python 3?

sobes.tech AI

Answer from AI

Main differences between Python 2 and Python 3:

  • print: In Python 2, it is a statement print, in Python 3 — a function print().

  • String types: In Python 2, strings are ASCII by default, with a separate unicode type. In Python 3, strings are Unicode by default.

  • Integer division: In Python 2, dividing integers returns an integer (e.g., 3/2 == 1), in Python 3 — a floating-point number (3/2 == 1.5). For integer division in Python 3, use //.

  • range() and xrange(): In Python 2, there is range() (returns a list) and xrange() (generator). In Python 3, range() behaves like xrange().

  • Exception handling: Syntax changed: except Exception, e in Python 2 is replaced with except Exception as e in Python 3.

  • Modules and libraries: Some standard libraries have been renamed or changed.

Example:

# Python 2
print "Hello, world!"

# Python 3
print("Hello, world!")
What are the main differences between Python 2 and… - sobes.tech