>>> print'hello,world' hello,world在3中
>>> print'hello,world' SyntaxError: invalid syntax >>> print('hello,world') hello,world
>>>1/2 #整除 0
>>> 1//2 #"//"表示整除,就算是浮点数也表示整除 0如果需要普通的除法,有两种方式:
>>>1.0/2.0 0.5 >>>1.0/2 0.5 >>>1/2.0 0.52、在程序前输入以下语句:from__future__import division ("__"是两个下划线)
>>> 1//2 0
>>> 1/2 0.5
>>> 10000000000000000000000000000000 #普通整数在-2147483648~2147483647之间。如果需要大数就会用到长整型数,书写方法与普通整数一样,但结尾有个L 10000000000000000000000000000000L在3中:
>>> 10000000000000000000000000000000 10000000000000000000000000000000
>>> price=raw_input('input the stock price of Apple:') input the stock price of Apple:109 >>> price '109' >>> price=input('input the stock price of Apple:') input the stock price of Apple:109 >>> price 109
>>> price=raw_input('input the stock price of Apple:') #3中raw_input()和input()整合成了input(),返回类型为str Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> price=raw_input('input the stock price of Apple:') NameError: name 'raw_input' is not defined >>> price=input('input the stock price of Apple:') input the stock price of Apple:109 >>> price '109'
在3中:
>>>from imp import reload >>> reload(kNN) <module 'kNN' from 'D:\\Python\\kNN.py'></span>
>>> reload(kNN) Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> reload(kNN) NameError: name 'reload' is not defined</span>
在2中:
range()和xrange()的语法一样,前者返回的是列表,后者返回的是生成器,但后者在内存上更有效。
>>>range(3,11,2) [3, 5, 7, 9] >>> xrange(3,11,2) xrange(3, 11, 2) >>>for i in xrange(3,11,2): ... print i ... 3 5 7 9
在3中:
没有xrange()。range()的功能就是2.x中xrange()的功能。
>>>for i in range(3,11,2): print(i) 3 5 7 9
如果要获得真实列表,需要显示调用:
>>> list(range(3,11,2)) [3, 5, 7, 9]
>>> import urllib >>> r = urllib.urlopen('http://z.cn/') >>> htlm = r.read()
>>> import urllib.request >>> r = urllib.request.urlopen('http://z.cn/') >>> htlm = r.read()