Python Interview Questions

32 Questions
Python

Python

Web DevelopmentFrontendBackendData Science

Question 2

Explain the difference between Python 2 and Python 3

Answer:

Python 2 and Python 3 are two major versions of the Python programming language, each with distinct differences. Python 3, released in December 2008, introduced several changes and improvements over Python 2, which was first released in October 2000. Understanding these differences is crucial for developers, especially when maintaining or upgrading legacy code. Here are the key differences:

  1. Print Statement vs. Print Function:

    • Python 2: The print statement is used without parentheses.
      print "Hello, World!"
    • Python 3: The print function requires parentheses.
      print("Hello, World!")
  2. Integer Division:

    • Python 2: Dividing two integers performs floor division, resulting in an integer.
      print 5 / 2  # Output: 2
    • Python 3: Dividing two integers results in a float. Use // for floor division.
      print(5 / 2)  # Output: 2.5
      print(5 // 2)  # Output: 2
  3. Unicode and Strings:

    • Python 2: Strings are ASCII by default, and Unicode needs to be explicitly specified using the u prefix.
      my_string = "hello"
      my_unicode_string = u"hello"
    • Python 3: Strings are Unicode by default, and byte strings use the b prefix.
      my_string = "hello"
      my_byte_string = b"hello"
  4. xrange() vs range():

    • Python 2: range() returns a list, and xrange() returns an iterator.
      for i in xrange(5):
          print i
    • Python 3: range() returns an iterator, and xrange() is removed.
      for i in range(5):
          print(i)
  5. Error Handling:

    • Python 2: Exceptions are handled using different syntax.
      try:
          raise ValueError("An error occurred")
      except ValueError, e:
          print e
    • Python 3: Exceptions require as keyword.
      try:
          raise ValueError("An error occurred")
      except ValueError as e:
          print(e)
  6. Input Function:

    • Python 2: input() evaluates the input as code, and raw_input() returns the input as a string.
      user_input = raw_input("Enter something: ")
    • Python 3: input() returns the input as a string, and raw_input() is removed.
      user_input = input("Enter something: ")
  7. Iteritems() vs items():

    • Python 2: Dictionary methods like iteritems(), iterkeys(), and itervalues() return iterators.
      my_dict = {'a': 1, 'b': 2}
      for key, value in my_dict.iteritems():
          print key, value
    • Python 3: These methods are removed, and items(), keys(), and values() return iterators.
      my_dict = {'a': 1, 'b': 2}
      for key, value in my_dict.items():
          print(key, value)
  8. Libraries and Dependencies:

    • Python 2: Some older libraries may only be compatible with Python 2.
    • Python 3: Most modern libraries and frameworks support Python 3, and many new features are introduced in Python 3 only.
  9. End of Life:

    • Python 2: Officially reached end of life on January 1, 2020. No further updates or security fixes are provided.
    • Python 3: Actively maintained with regular updates and improvements.

Summary

Python 3 introduces significant changes aimed at improving the language’s consistency and efficiency. While Python 2 was widely used for many years, the transition to Python 3 is encouraged due to its long-term support and modern features. The key differences include changes in syntax, such as the print function, integer division, and Unicode handling, as well as the removal and modification of certain functions and methods.

Recent job openings