Python 2 vs. Python 3 — Important Differences Every Hacker Should Know « Null Byte :: WonderHowTo

Python is commonly touted as one of the best programming languages for beginners to learn, and its straightforward syntax and functionality makes that hard to argue with. But a lot of tutorials still use Python 2, which is outdated now. Python 3 introduces many new features, and it's important to be aware of them going forward, as well as the key differences between Python 3 and its predecessor.Python 2was first released in 2000. It improved upon earlier versions of the language and introduced features common to other programming languages such as garbage collection, list comprehension, and Unicode support. Version 2.1 saw a few minor upgrades, and 2.2 was released at the tail end of 2001. Version 2.2 featured type unification, which merged types and classes into a single hierarchy, transforming Python into an object-oriented language.Versions progressed until Python 2.7, which was to be the last major release until Python 3. Python 2 was officially retired on the first day of 2020. Python 3, first started in 2008, is now the most current option, with version 3.8.3 being the latest update.Recommended on Amazon:Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to ProgrammingPrint FunctionThe first and possibly the most noticeable difference inPython 3is theprintfunction. In Python 2, print could be used with parentheses with either single or double quotation marks:print('Hello, Null Byte.')
Hello, Null Byte.

or

print("Hello, Null Byte.")
Hello, Null Byte.Or without, using just a space between it and what needs printed:print 'Hello, Null Byte.'
Hello, Null Byte.

or

print "Hello, Null Byte."
Hello, Null Byte.In Python 3, parentheses must be used:print('Hello, Null Byte.')
Hello, Null Byte.

or

print("Hello, Null Byte.")
Hello, Null Byte.So Python 2 is more versatile in this case, but Python 3 makes the code more concise with fewer ways to do the same thing, and that can actually be easier to learn and use.Integer DivisionPython 3 also handles the division of integers differently. In version 2, dividing two integers would yield an integer as a result:print 5 / 3
1

or

print 5/3
1

or

print(5 / 3)
1

or

print(5/3)
1To get the full float value with decimals, you would need to use decimals for the print function, like so:print 5.0 / 3.0
1.6666666666666667

or

print 5.0/3.0
1.6666666666666667

or

print(5.0 / 3.0)
1.6666666666666667

or

print(5.0/3.0)
1.6666666666666667But in Python 3, a float value is returned no matter if you use decimals or just integers (though, you do need to use parentheses):print(5 / 3)
1.6666666666666667

or

print(5/3)
1.6666666666666667

or

print(5.0 / 3.0)
1.6666666666666667

or

print(5.0/3.0)
1.6666666666666667Python 3 is more how a calculator would work, which is a lot easier to understand and remember for people just getting into Python.Range FunctionIn Python 2, thexrangefunction is used for iteration, in for loops, as well as iterating through a list or dictionary:for x in xrange(1, 10):
    print(x)In version 3, xrange is replaced by therangefunction:for x in range(1, 10):
    print(x)The advantage of xrange is that it is faster when iterating over a list one time. However, it doesn't support splicing and other list-oriented methods, so using range is now preferred.UnicodePython 2 implicitly stores strings asASCIIvalues. To store them using Unicode, it needs to be specified withulike so:print(u'This is Unicode')
This is Unicode

or

print(u"This is Unicode")
This is Unicode

vs.

print('This is NOT Unicode but ASCII')
This is NOT Unicode but ASCII

or

print("This is NOT Unicode but ASCII")
This is NOT Unicode but ASCIIIn Python 3, strings are now treated as Unicode by default, so there is no need to specify:print(u'This is Unicode')
This is Unicode

or

print(u"This is Unicode")
This is Unicode

vs.

print('This is ALSO Unicode')
This is ALSO Unicode

or

print("This is ALSO Unicode")
This is ALSO UnicodeExceptionsThe way exceptions and errors are handled has changed in Python 3. In version 2, exceptions look something like this:try:
    checking_error
except NameError, err:
    print err, ‘Error found’In version 3, we need to add theaskeyword:try:
    checking_error
except NameError as err:
    print(err, ‘Error found’)Python 3 also handles the way exceptions are raised. In version 2:raise IOError, ‘error’In version 3, parentheses are required:raise IOError(‘error’)Variable LeakageIn Python 2, variables in a for loop will leak into the global namespace. This means global variables can change while inside the loop. In Python 3, it was tweaked, and the value of the variables never changes.Future ModulePython 3 added certain keywords and features that aren't compatible with Python 2. Luckily, there is an easy way to use the functionality of version 3 in code running Python 2 — the _future_ module. To help migrate to Python 3, simply use future imports:from _future_ import divisionNow there is Python 3 support in Python 2 code.Advantages of Python 3Now that we've gone over the changes in Python 3, it's easy to see some of the advantages it has over the retired version. Things like the print function actually behaving like a function are nice, as well as the way division is handled. Unicode support is another advantage 3 has over 2 — no more having to worry about specifying it. Error handling is also more robust in Python 3 and allows for easier tracking anddebugging.One of the main goals of Python 3 was to remove some of the redundancy that was present in version 2. Since this language is often recommended to and used bybeginners, the developers wanted to make it clear there was only one way to perform a certain action. Lastly, the biggest advantage of Python 3 is that it is being actively developed, allowing the codebase to become more efficient and functional as time goes on.Python 2 is officially retired, so join the party and migrate to Python 3 as soon as possible. And those of you with original Python apps on GitHub, make sure to update to Python 3 so your users won't run into any hiccups.Don't Miss:How to Execute Hidden Python Commands in a One-Line StagerWant to start making money as a white hat hacker?Jump-start your hacking career with our2020 Premium Ethical Hacking Certification Training Bundlefrom the newNull Byte Shopand get over 60 hours of training from cybersecurity professionals.Buy Now (90% off) >Other worthwhile deals to check out:97% off The Ultimate 2021 White Hat Hacker Certification Bundle99% off The 2021 All-in-One Data Scientist Mega Bundle98% off The 2021 Premium Learn To Code Certification Bundle62% off MindMaster Mind Mapping Software: Perpetual LicenseCover image byCg__Prodigy/PixabayRelatedHack Like a Pro:Python Scripting for the Aspiring Hacker, Part 1How To:This Extensive Python Training Is Under $40 TodayHow To:The Novice Guide to Teaching Yourself How to Program (Learning Resources Included)How To:Break into the Lucrative World of Ethical Hacking with This Reasonably Priced Course BundleHow To:Here's Why You Need to Add Python to Your Hacking & Programming ArsenalHack Like a Pro:Python Scripting for the Aspiring Hacker, Part 2News:And the Winner of the White Hat Award for Technical Excellence Is...Hack Like a Pro:Python Scripting for the Aspiring Hacker, Part 3 (Building an FTP Password Cracker)How to Train Your Python:Part 1, IntroductionNews:Learning Python 3.x as I go (Last Updated 6/72012)How To:Make a Gmail Notifier in PythonHow To:Make a Change-of-IP Notifier in PythonHow To:Generate Word-Lists with Python for Dictionary AttacksNews:Ball PythonsHow To:Enable Code Syntax Highlighting for Python in the Nano Text EditorHow To:Send SMS Messages with PythonNews:Monty Python's World Cup - In case you haven't seen itCommunity Byte:Coding a Web-Based Password Cracker in PythonPygame:All You Need to Start Making Games in PythonHow To:Shorten URLs from the Command Line with PythonPet Portrait Challenge:FranklinCommunity Byte:HackThisSite Walkthrough, Part 5 - Legal Hacker TrainingNews:Learn to Code in Python, Part One: Variables, Input and OutputCommunity Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingHow To:Code Your Own Twitter Client in Python Using OAuthCommunity Byte:HackThisSite, Realistic 3 - Real Hacking SimulationsCommunity Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingWeekend Homework:How to Become a Null Byte Contributor (3/9/2012)Weekend Homework:How to Become a Null Byte Contributor (1/29/2012)Goodnight Byte:Coding a Web-Based Password Cracker in PythonCommunity Byte:HackThisSite Walkthrough, Part 9 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 8 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 7 - Legal Hacker TrainingCommunity Byte:HackThisSite, Realistic 5 - Real Hacking SimulationsCommunity Byte:HackThisSite, Realistic 4 - Real Hacking SimulationsWeekend Homework:How to Become a Null Byte Contributor