Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (4.1k points)
What is the difference between Python 2.x and Python 3.x?

1 Answer

0 votes
by (14.4k points)

Key Differences Between Python 2 and Python 3

  1. Print: In Python 2, “print” is treated as a statement and Python 3 explicitly treats “print” as a function, which means you have to pass the items you need to print to the function in parentheses in the standard way, or you will get a syntax error.a statement rather than a function.
  2. Integer Division: Python 2 treats numbers that you type without any digits after the decimal point as integers, which can lead to some unexpected results during division. For example, if you type the expression 3 / 2 in Python 2 code, the result of the evaluation will be 1, not 1.5 as you might expect.
  3. List Comprehension Loop Variables: In previous versions of Python, giving the variable that is iterated over in a list comprehension the same name as a global variable could lead to the value of the global variable being changed — something you usually don’t want. This irritating bug has been fixed in Python 3, so you can use a variable name you already used for the control variable in your list.
  4. Unicode Strings: Python 3 stores strings as Unicode by default, whereas Python 2 requires you to mark a string with a “u” if you want to store it as Unicode. Unicode strings are more versatile than ASCII strings, which are the Python 2 default, as they can store letters from foreign languages as well as emoji and the standard Roman letters and numerals.
  5. Raising Exceptions: Python 3 requires different syntax for raising exceptions.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 23, 2019 in Python by Karan Singh (4.1k points)
0 votes
4 answers
asked Apr 18, 2021 in Python by laddulakshana (16.4k points)

Browse Categories

...