New Features In Python 3.x
I have updated the python's version from 2.7.x to 3.6 for pursing the effective work. But the python 3.x is not backwards imcompatible. So I decide the figure out what are the new fetures brought by python 3.x.
In [1]:
print('The answer is ', 2*2)
In [2]:
for x in range(5):
print(x, end=' ')
In [3]:
import sys
print('fatal error', file=sys.stderr)
In [1]:
x, y = 1,2
print(x,y)
In [2]:
print((x,y))
customize the separator between items
In [4]:
print('There are ', 2**32, ' possibilities!', sep='~')
In [7]:
dic = dict({'name':'gaofeng', 'age': 25, 'university':'cumt'})
print(type(dic.keys()))
print(type(dic.items()))
print(type(dic.values()))
In [8]:
for key in dic.keys():
print(key, dic[key])
In [14]:
k = sorted(dic)
print(type(k))
dict.iterkeys(), ditc.iteritems() and dict.itervalues() methods are no longer supported.
remove the dict.has_key() method. Using in
instead.
In [62]:
print('name' in dic)
In [18]:
x = range(10)
type(map(lambda value:value**2, x))
Out[18]:
In [19]:
type(filter(lambda value:value%2==0, x))
Out[19]:
In [22]:
type(range(10))
Out[22]:
In [23]:
if 1 < '':
pass
Builtin.sorted() and list.sort() no longer accept cmp argument and __cmp()__ method does not support. Use __lt__() for sorting and __eq__() with __hash__() for other rich comparsion if you need.
In [31]:
class Person(object):
def __init__(self, age):
self._age = age
@property
def age(self):
return self._age
def __lt__(self, person):
return self._age < person.age
def __gt__(self, person):
return self._age > person.age
def __eq__(self, person):
return self._age == person.age
persons = [Person(8), Person(4), Person(6)]
persons.sort()
for person in persons:
print(person.age)
In [34]:
print(1/2)
In [35]:
print(1//2)
Before 1980s, almost all personal computer were 8-bit which means that one byte(8 bit) could represent value ranging from 0 to 255. And ASCII code only uses up to 127. As digits technology spreads all over the world, the short of characters emerged. One bytes could not map the whole the characters in the world. The Unicode standard describe how characters are represents by code points. Each code point is an integer value which are denoted in base 16(strared by '0X'). For example, we say 'This is character U+12CA'. U+12CA is a code point, which represents some particular character. You can go Unicode Query website to find its representive.
Unicode string is a sequence of code points, which are number of from 0 to 0X10FFFF. But we want to map those sequence from code points to a set of bytes(value from 0 through 255) in memory. And the processing of translating a unicode string into a sequence of bytes are called an encoding.
But if ours translating is straightforward, which brings many weakness, such as importable, space-wasteful and imcompatible with some functions. Generally, we choose UTF-8 rule and the rules are listed below:
Since python 3.x, the str
type contains contains unicode characters, and the default encoding for Python source code is UTF-8. You can also put a specially-formatted comment as the first line of the source code if you want to change another encoding.
In [37]:
# -*- coding:<encoding name> -*-
You can also use Unicode character in identifiers
In [44]:
# -*- coding:utf8 -*-
高峰 = 'gaofeng'
for name in 高峰:
print(name, end=',')
You can use the character name and code point of Unicode character if you editor could not enter a particular character.
In [45]:
"\N{GREEK CAPITAL LETTER DELTA}"
Out[45]:
In [46]:
"\u0394"
Out[46]:
In [47]:
"\U00000394"
Out[47]:
You can create a string using the decode() method of bytes. This method method take an encoding argument, and optionally an error argument.
In [52]:
b"\x80abc".decode('utf-8','ignore')
Out[52]:
str.encode() is opposite method of bytes.decode(), which returns a bytes represent of unicode string.
In [59]:
print(type('a'.encode('utf-8')))
for char in 'a'.encode('utf-8'):
print(char)
In [61]:
bytes_value='高峰'.encode('utf-8')
bytes_value.decode('utf8')
Out[61]:
In [63]:
guess = int(input('Enter an integer: '))
In [64]:
bin(255)
Out[64]:
In [65]:
class C(object):
def __init__(self, a):
print('C', a)
class D(C):
def __init__(self, a):
super().__init__(a)
D(10)
Out[65]:
In [66]:
def foo(cls):
def _wrapper(self):
print('decorator')
cls.print=_wrapper
return cls
@foo
class Bar():
pass
Bar().print()
In [67]:
import collections
dir(collections)
Out[67]:
Itertor mode replaces next() method of __next__ method