Changes in Basic Commands:
A Comparison of Python 2.7 and Python 3.x.
Python is a powerful programming language that has evolved significantly over the years. Two of the most commonly used versions of Python are 2.7 and 3.x. While these versions are similar in many ways, there are some significant differences that developers should be aware of. In this article, we'll explore some of the changes that have been made to Python 3.x compared to Python 2.7, including changes to basic commands.
• Print Statement
In Python 2.7, the print statement is used as print "Hello, World!". However, in Python 3.x, print is a function and should be used as print("Hello,World!").
This change was made to make the language more consistent and easier to understand. In addition, it allows developers to use the print function with additional arguments, such as print("Hello, World!", end=""), which would print "Hello, World!" without a newline character.
Ex.
print "Hello, World!"
To
print("Hello, World!")
• Division
In Python 2.7, the division operator / returns an integer value if both operands are integers. However, in Python 3.x, the division operator / always returns a float value. If you want to perform integer division in Python 3.x, use // operator. This change was made to improve the accuracy of calculations, as well as to make the language more consistent.
Ex.
print 5/2 # output: 2
To
print(5/2) # output: 2.5
To perform integer division in Python 3.x, use // operator:
Ex.
print(5//2) # output: 2
• Unicode
In Python 3.x, all strings are Unicode by default. This means that you can use characters from any language in your code without having to encode or decode them. This change was made to make Python more international and to reduce the complexity of working with different character sets.
Ex.
text = u"hello"
To
text = "hello"
• Range and Xrange
In Python 2.7, you have the range() function and the xrange() function. In Python 3.x, the xrange() function is no longer available, and the range() function behaves like the old xrange() function. This change was made to simplify the language and reduce confusion for developers.
Ex.
for i in xrange(5):
print i
To
for i in range(5):
print(i)
• Iteration
In Python 3.x, the dictionary items() method returns an iterable view instead of a list. Similarly, the zip() function returns an iterator instead of a list. This change was made to improve the performance of code that iterates over large datasets.
Ex.
d = {'a': 1, 'b': 2, 'c': 3}
for k, v in d.items():
print k, v
To
d = {'a': 1, 'b': 2, 'c': 3}
for k, v in d.items():
print(k, v)
• Exception Handling
In Python 2.7, you can catch exceptions using the syntax except ExceptionType, e:. However, in Python 3.x, this syntax is not supported, and you should use except ExceptionType as e: instead. This change was made to make the language more consistent and easier to understand.
Ex.
try:
do_something()
except ExceptionType, e:
handle_exception(e)
To
try:
do_something()
except ExceptionType as e:
handle_exception(e)
• Relative Imports
In Python 2.7, you can use relative imports like from .module import function. However, in Python 3.x, this syntax is not supported, and you should use absolute imports like from package.module import function. This change was made to improve the readability and maintainability of code.
Ex.
from .module import function
To
from package.module import function
• Super Function
In Python 2.7, you can use the super() function without any arguments. However, in Python 3.x, you should always pass the current class and self as arguments to the super function, like super().__init__(args). This change was made to improve the consistency of the language and to reduce the likelihood of errors.
Ex.
class Child(Parent):
def __init__(self, args):
super(Child, self).__init__(args)
To
class Child(Parent):
def __init__(self, args):
super().__init__(args)
In conclusion, Python 3.x has made several changes compared to Python 2.7. These changes have been made to improve the performance, consistency, and readability of the language. Developers who are used to working with Python 2.7 should be aware of these changes to ensure that their code works correctly with Python 3.x.
No comments:
Post a Comment