WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Basic · question 2 of 100

Can you explain the differences between Python 2 and Python 3?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Python 2 and Python 3 are two major versions of the Python programming language. Python 3 was released in 2008 and is the current version of the language, while Python 2 is an older version that is no longer being actively developed. There are several key differences between the two versions, including:

Print Statement: One of the most significant differences between Python 2 and Python 3 is the way print statements are handled. In Python 2, the print statement is a statement, whereas in Python 3, it is a function. This means that in Python 3, you need to use parentheses when you print something, whereas in Python 2, you don’t. Here’s an example:

Python 2:

    print "Hello, World!"

Python 3:

    print("Hello, World!")

Division Operator: In Python 2, the division operator (/) works differently than in Python 3. In Python 2, when you divide two integers, the result is an integer, and the decimal part is truncated. In Python 3, the division operator always returns a float, even when the result is a whole number. Here’s an example:

Python 2:

    print 7/2 # Output: 3

Python 3:

    print(7/2) # Output: 3.5

Unicode Support: Python 2 and Python 3 also handle Unicode differently. In Python 2, you need to use a special unicode type to work with Unicode strings, whereas in Python 3, all strings are Unicode by default. Here’s an example:

Python 2:

    # Declare a Unicode string
    unicode_string = u"Hello, World!"

Python 3:

    # All strings are Unicode by default
    unicode_string = "Hello, World!"

Range Function: In Python 2, the range() function returns a list, whereas in Python 3, it returns an iterator. This means that in Python 3, you need to use the list() function if you want to convert the range object to a list. Here’s an example:

Python 2:

    # Returns a list of numbers from 0 to 4
    print range(5) # Output: [0, 1, 2, 3, 4]

Python 3:

    # Returns an iterator that generates numbers from 0 to 4
    print(range(5)) # Output: range(0, 5)
    
    # Convert the range object to a list
    print(list(range(5))) # Output: [0, 1, 2, 3, 4]

These are just a few of the key differences between Python 2 and Python 3. It’s important to note that Python 2 is no longer being actively developed and will not receive any more updates, whereas Python 3 is the current version of the language and will continue to receive updates and improvements. If you’re starting a new project, it’s recommended that you use Python 3. If you’re working with legacy code that was written in Python 2, you may need to make changes to the code to ensure that it works correctly in Python 3.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics