Chris Castiglione Teacher at One Month. Faculty at Columbia University where I teach Digital Literacy. I write about coding, the internet, and social impact.

Python 2 vs Python 3: What’s the Difference?

4 min read

Instagram recently celebrated their move from Python 2 to Python 3Yet many famous companies are still using Python 2.

Why? One word: compatibility.

Python 3 is not “backwards-compatible” which means that if your company is using various Python 2 libraries they may no longer work after you upgrade.

Companies running code on Python 2 have to make a tough choice: they either have to find a replacement library that works with Python 3, or they are stuck with Python 2. Dropbox wrote a detailed report of the process that took them THREE YEARS to upgrade

Python 3 adoption has long been a subject of debate in the Python community. This is still somewhat true, though it’s now reached widespread support, with some very popular projects such as Django dropping Python 2 support entirely.

You read that right, it took Dropbox three years to completed upgrade from Python 2 to Python 3 — and for gods sakes, Guido Van Rossum (the inventor of Python) works at Dropbox!

Python 3 is more than 10 years old! Yet, many companies are still using Python 2. So which version of the language should you learn? In this post I’ll break down the differences between Python 2 and Python 3. If you don’t want to read the post my advice: 

Why is Python so Popular?

  • Intuitiveness. The syntax (or “grammar” of Python, so to speak) is easy to understand.
  • Maturity. Python is a proven language with powerful capabilities allowing you to code just about anything you can dream up.
  • In-demand. Python developers are regularly hired by a host of companies throughout the world.
  • Remote-friendly. Coding ninjas and coding students alike merely require an internet connection to achieve their goals.
  • Python is the new Excel. The reason Python is being taught in business school is because Python is like Excel on steroids. With Python financial analysts, CEOs, and data-driven marketers can leverage the power of Python to crunch big data.

Why are there two version of Python?

Change has been afoot for some time in the land of Python, however. The Python Software Foundation, an independent non-profit organization that holds the copyright on the software, released version 2.7 in 2010, while at the same time upgrading the codebase to various iterations of 3.x simultaneously. Over a number of years, companies and projects started migrating to Python 3 due to its various benefits.

Where does the name Python come from? Python (the coding language) comes from Montey Python, not the snake
Python is named after a famous 1970’s comedy troupe.Not the snake.

Why Choose Python 2?

One of the biggest reasons to stick with Python 2 is if you’re interested in working with a large codebase that was written in Python 2. Moving a large application written in an older version of one language, or re-rewriting it in another language altogether, can be a massive undertaking.

Another reason to stay in the land of Python 2 is if your code depends on a specific extension originally written for Python 2 that has not been updated. Most of them have been, but not all. It’s up to you to decide if it’s worthwhile to continue to work with older extensions (also called “packages”).

Why Choose Python 3?

To put it bluntly, Python 2 is legacy, Python 3 is the Future. If you are starting to learn to code — learn Python 3. Here are some additional reasons why you should learn Python 3:

  • Legacy code. After 2020, Python 2 will no longer be maintained. 
  • Learn best-practices. Python 3 includes upgrades not available in Python 2.
  • Write cleaner code. Python 3 has elegantly modified its structure to so that it takes fewer lines of code to perform an action.
  • Avoid syntax confusion. Some small grammatical differences exist between the two versions, which can frustrate beginners.

What are the Differences between Python 2 and Python 3?

One of the first lines of Python code you will write as a beginner will be to tell the computer to use the print function to return the phrase “Hello world!”

Python 2:

Print “Hello world!”

Python 3:

print(“Hello world!”)

Result: The computer returns the same thing, but you may find that code nested in parentheses is used much more often in Python 3. There are programmatic reasons for this as well: Python 3 is much easier to read.

Hello world!

If that sounds like a lot of parentheses to keep track of, never fear! At the beginning of your learning process you’ll install a text editor that is specifically designed to highlight missing parentheses and other syntax errors — and even autofill many common things for you.

Python Variables 

A second example involving a more extensive use of parentheses is replacing a returned line of prepared text with different variables. This is part of the operation that is happening when you sign up for an email newsletter, and they magically address it to you with your actual name.

Python 2:

User20987 = “Chris Castiglione”
print “Hi, %, hope you’re having a great day today!” % (User20987)

Python 3:

User20987 = “Chris Castiglione”
print(“Hi {0}, hope you’re having a great day today!”).format(User20987))

Python Functions 

Another major syntax difference is how the raw_input() function has changed. This is an extremely common function that takes information from various fields on a website (such as when you sign up for a new account and type in your email address).

Python 2: 

Field_1 = raw_input(“entered_value”)

Python 3:

Field_1 = input(“entered_value”)

This matters more than it appears, because it also has an effect on how the computer interprets the code. Python 2 will automatically “evaluate” the input, which can have unintended consequences if the user does not enter the kind of data the programmer is expecting.

Let’s assume that in the above example, the Python 2 programmer expected the user to enter a whole number, but instead the user made a typo and entered the mathematical operation “7-2”. That line of code would return the number 5, which would not be the desired outcome.

The Python 3 version will not evaluate the input by default. Instead of doing math, it will return a form of data called a “string”, which in this case would be a copy of what the user entered: “7-2”. This approach allows you to more precisely define what kind of data you receive from the user.

Elon Musk himself claims that Teslas can play any Monty Python skit on command. Huge Python fan.

Why I Recommend Learning Python 3: 

Python 2 vs 3 performance in terms of computing speed has been dramatically improved, but Python 3 also improves upon the already vast abilities of Python 2.

Here are some cool new things that you’ll learn as you start out:

  • Easier user input. A common early lesson is to take a user’s typed-in data (the input syntax example used above, for instance) and pass it off to a simple script to create a list of information. Python 3 improves upon this process.
  • Improved number management. Python 3 includes some math-calculation improvements that are critical for many applications.
  • Popular add-ons are supported. You’ll also learn the concept of extensions called “modules” and “packages” that are not included in the default Python 3 installation. As you grow, installing and using these will become part of your daily repertoire. Nearly all of the most popular packages have been Python 3-ready for years.
  • Unicode support. Did you know that emojis are Unicode characters? No emojis in Python 2! 😉 😎 😃
  • And there’s more! Here are six things to know before you learn Python.

How Do I Get Python 3 On My Computer?

Many brand-new computers still come with Python 2.7 pre-installed by default, but it’s worth it to learn how to install and use Python 3 on your own personal machine.

One Month’s Intro to Python class will guide you through installing Python 3 on your personal machine, as the process to do this varies between operating systems.

Chris Castiglione Teacher at One Month. Faculty at Columbia University where I teach Digital Literacy. I write about coding, the internet, and social impact.

Leave a Reply

Your email address will not be published. Required fields are marked *