Brief analysis of the difference between py3.x and py2.x

I started learning Python this week because the books I've been reading are based on Python 2.x, but I installed Python 3.1, which caused many of the examples in the book to not work properly. To understand the differences between Python 3.x and 2.x, I did a lot of research online. I decided to write this down in my own space so that I can refer back to it later, and also share it with friends who are interested in learning Python. **Performance** Python 3.0 was about 30% slower than Python 2.5 in the pystone benchmark. Guido van Rossum believed there was still a lot of room for optimization, especially in string and list operations. In Python 3.1, performance improved slightly, but it was still 15% slower than Python 2.5, indicating that there was still a lot of potential for improvement. **Coding** In Python 3.x, the default encoding for source files is UTF-8, which makes it possible to use Unicode characters directly in variable names. For example: ```python China = 'china' print(China) ``` This code would output `China` without any issues. **Grammar Changes** 1. The `<>` operator has been removed; now `!=` is used instead. 2. Backticks (`` ` ``) have been removed; `repr()` is used instead. 3. New keywords like `as`, `True`, `False`, and `None` were added. 4. Integer division now returns a float by default. Use `//` for integer division. 5. The `nonlocal` statement was introduced to allow assignment to variables in outer scopes. 6. The `print` statement was replaced with the `print()` function. Similarly, `exec` became a function. - Example: - Python 2.x: `print "The answer is", 2*2` - Python 3.x: `print("The answer is", 2*2)` - Python 2.x: `print x,` (ends with a space) - Python 3.x: `print(x, end=" ")` - Python 2.x: `print` (prints a new line) - Python 3.x: `print()` (same result) - Python 2.x: `print >> sys.stderr, "fatal error"` - Python 3.x: `print("fatal error", file=sys.stderr)` - Python 2.x: `print (x, y)` (prints the tuple) - Python 3.x: `print((x, y))` (different from `print(x, y)`) 7. The behavior of sequence operators changed. 8. The `input()` function replaced `raw_input()`. - Python 2.x: `guess = int(raw_input('Enter an integer: '))` - Python 3.x: `guess = int(input('Enter an integer: '))` 9. Tuple parameter unpacking in function definitions was removed. You can no longer define functions like `def(a, (b, c)): pass`. 10. Octal literals were changed. In Python 3.x, you must use `0o666` instead of `0666`. - Python 2.x: `0666` → `438`, `oct(438)` → `'0666'` - Python 3.x: `0o666` → `438`, `oct(438)` → `'0o666'` 11. Binary literals and the `bin()` function were added. - `bin(438)` → `'0b110110110'` 12. Extended iterable unpacking allows patterns like `a, b, *rest = seq` or `*rest, a = seq`. 13. The `super()` function no longer accepts arguments. ```python class C(object): def __init__(self, a): print('C', a) class D(C): def __init__(self, a): super().__init__(a) ``` 14. A new metaclass syntax was introduced: `class Foo(*bases, **kwds): pass` 15. Class decorators were added, similar to function decorators. ```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() ``` **Strings and Bytes** In Python 3.x, strings are of type `str`, which is similar to Python 2.x's `unicode`. There is also a separate `bytes` type for handling binary data. This separation helps avoid confusion between text and binary data.

Pure Tin Wire

High purity Tin wire is a special target material, Vacuum coating target technology was used in Automobile glass.

Pure Tin Wire,Tin Wire,Tin Solder Wire,Tin Plated Copper Wire

Shaoxing Tianlong Tin Materials Co.,Ltd. , https://www.tianlongspray.com

This entry was posted in on