Transformer Service,Transformer Energy Monitoring,Transformer Monitoring System,Transformer Monitoring Hangzhou Qiantang River Electric Group Co., Ltd.(QRE) , https://www.qretransformer.com
I started learning Python this week because the books I read were based on Python 2.x, but I installed Python 3.1, which caused many examples in the book to not work properly. I did some research online about the differences between Python 3.x and 2.x, and I decided to write this summary for my own reference, in case I need it later. It might also be helpful for friends who are starting to learn Python.
Performance
Python 3.0 ran the pystone benchmark 30% slower than Python 2.5. Guido van Rossum believed that there was still a lot of room for optimization in Python 3.0, especially in string and list operations. In Python 3.1, performance was about 15% slower than Python 2.5, but there was still significant potential for improvement.
Coding
In Python 3.x, the default encoding for source files is UTF-8. This makes code like the following valid:
>>> China = 'china'
>>> print(China)
China
Grammar
1) The `<>` operator has been removed; all comparisons now use `!=`.
2) Backticks (``) have been removed; use `repr()` instead.
3) New keywords such as `as`, `True`, `False`, and `None` have been added.
4) Integer division (`/`) now returns a float. To get an integer result, use `//`.
5) The `nonlocal` statement was introduced, allowing direct assignment to variables in outer scopes (but not global).
6) The `print` statement was replaced with the `print()` function. Similarly, the `exec` statement became the `exec()` function.
Example:
2.x: `print "The answer is", 2*2`
3.x: `print("The answer is", 2*2)`
2.x: `print x,` # ends with a space
3.x: `print(x, end=" ")` # replaces the newline with a space
7) The `input()` function replaced `raw_input()`.
Example:
2.x: `guess = int(raw_input('Enter an integer: '))`
3.x: `guess = int(input('Enter an integer: '))`
8) Tuple parameter unpacking in function definitions was removed. You can't define functions like `def(a, (b, c)): pass`.
9) Octal literals were changed. In Python 3.x, you must use `0o666` instead of `0666`.
Example:
2.x: `0666` → 438, `oct(438)` → `'0666'`
3.x: `0o666` → 438, `oct(438)` → `'0o666'`
10) Binary literals and the `bin()` function were added.
Example: `bin(438)` → `'0b110110110'`
11) Extended iterable unpacking allows syntax like `a, b, *rest = seq` or `*rest, a = seq`.
12) The `super()` function no longer accepts arguments.
Example:
```python
class C(object):
def __init__(self, a):
print('C', a)
class D(C):
def __init__(self, a):
super().__init__(a)
D(8)
# Output: C 8
```
13) A new metaclass syntax was introduced: `class Foo(*bases, **kwds): pass`.
14) Class decorators were added, similar to function decorators.
Example:
```python
def foo(cls_a):
def print_func(self):
print('Hello, world!')
cls_a.print = print_func
return cls_a
@foo
class C(object):
pass
C().print()
# Output: Hello, world!
```
String and Byte Strings
Now, the `str` type represents Unicode strings, similar to Python 2.x's `unicode`. Bytes are represented by the `bytes` type. This change helps avoid confusion between text and binary data.