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 functionprint(). -
String types: In Python 2, strings are ASCII by default, with a separate
unicodetype. 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) andxrange()(generator). In Python 3,range()behaves likexrange(). -
Exception handling: Syntax changed:
except Exception, ein Python 2 is replaced withexcept Exception as ein Python 3. -
Modules and libraries: Some standard libraries have been renamed or changed.
Example:
# Python 2
print "Hello, world!"
# Python 3
print("Hello, world!")