Python Basics — Python 2 vs Python 3
Python 2 vs Python 3
If you’ve browsed Python tutorials or Stack Overflow answers, you’ve probably seen code written for both Python 2 and Python 3. This can be confusing for beginners.
Here’s the short answer: Always use Python 3. Python 2 is dead.
Python 2 reached its end of life on January 1, 2020. It no longer receives security patches, bug fixes, or updates of any kind.
Key Differences
| Feature | Python 2 | Python 3 |
|---|---|---|
print "Hello" (statement) |
print("Hello") (function) |
|
| Division | 5 / 2 = 2 (integer division) |
5 / 2 = 2.5 (true division) |
| Strings | ASCII by default | Unicode by default |
| range() | Returns a list | Returns an iterator (memory efficient) |
| input() | raw_input() for strings |
input() always returns string |
| Unicode | u"text" for Unicode |
All strings are Unicode |
| Syntax | except Exception, e: |
except Exception as e: |
| Status | End of life (Jan 2020) | Actively developed |
| Support | No updates | Regular updates |
The print Statement vs Function
This is the most visible difference you’ll encounter in old code:
Python 2:
print "Hello, World!"
print "Name:", name
Python 3:
print("Hello, World!")
print("Name:", name)
In Python 3, print() is a function — it uses parentheses. This is more consistent with the rest of the language.
Integer Division
This one catches people off guard:
Python 2:
>>> 5 / 2
2 # Truncates the decimal!
>>> 5.0 / 2
2.5 # Only works with floats
Python 3:
>>> 5 / 2
2.5 # True division by default
>>> 5 // 2
2 # Use // for integer division
Python 3 does what you’d expect — 5 / 2 gives you 2.5. If you want integer division, you explicitly use //.
Unicode Strings
Python 2:
>>> type("hello")
<type 'str'> # bytes, not Unicode
>>> type(u"hello")
<type 'unicode'> # need u prefix for Unicode
Python 3:
>>> type("hello")
<class 'str'> # Unicode by default
Python 3 handles international text (emojis, Chinese characters, Arabic script) out of the box.
Why Python 2 Was Retired
- Python 2 had fundamental design limitations
- Maintaining two parallel versions split community effort
- The Python 2 to 3 migration was announced over a decade in advance
- January 1, 2020 was the final cutoff date
- No security patches means using Python 2 is a security risk
What If You See Python 2 Code?
You’ll occasionally encounter Python 2 code in:
- Old Stack Overflow answers (especially pre-2020)
- Legacy projects that haven’t been migrated
- Older textbooks and tutorials
How to spot Python 2 code:
printwithout parentheses → Python 2raw_input()→ Python 2except Exception, e:→ Python 2xrange()→ Python 2
What to do: Mentally convert it to Python 3 syntax, or search for a newer answer.
The Only Choice: Python 3
For this entire tutorial — and for anything new you build — use Python 3. Specifically, Python 3.10 or newer, which gives you access to modern features like:
- Structural pattern matching (
match/case) - Better error messages
- Performance improvements
- Type hints and annotations
Check your Python version:
python --version
# or
python3 --version
Expected output:
Python 3.12.1
If you see Python 2.x, you need to install Python 3. We’ll cover that in the next section.
Summary
- Python 2 reached end of life on January 1, 2020
- Python 3 is the only actively maintained version
- Key differences:
print()function, true division, Unicode by default - Always use Python 3.10+ for new projects
- If you see Python 2 code online, convert it to Python 3 syntax
Next up: Installation & Setup →