Python是一門腳本語言,我也久聞大名,但正真系統的接觸學習是在去年(2013)年末到今年(2014)年初的時候。不得不說的是Python的官方文檔至關齊全,若是你是在Windows上學習Python,安裝包自帶的「Python Manuals」就是一份很好的學習資料(基本上不用去找其餘資料了);尤爲是其中的Tutorial,很是適合初學者。本文一方面總結了python語言的核心——數據類型和控制結構;另外一方面,經過與其餘語言的對比表達了我對Python的一些拙見。python
數據類型算法
>>> type(123) <type 'int'> >>> type(-234) <type 'int'> >>> type(123456123456) <type 'long'> >>> type(-123456123456) <type 'long'> >>> type(123.456) <type 'float'> >>> type('abc') <type 'str'> >>> type("hello, world") <type 'str'>
>>> type(123456) <type 'int'> >>> type(123456789) <type 'int'> >>> type(1234567890) <type 'int'> >>> type(12345678901) <type 'long'>能夠看到1234567890仍是int,12345678901就是long了,說明int是有範圍的。記得C/C++的int長度(4B)的同窗都知道,C/C++裏int的取值範圍是:[-2^31, 2^31-1]也就是[-2147483648, 2147483647]。據此,咱們能夠看看Python的int範圍:
>>> type(2147483647) <type 'int'> >>> type(2147483648) <type 'long'> >>> type(-2147483648) <type 'int'> >>> type(-2147483649) <type 'long'>此次試驗說明,Python的int範圍和C/C++同樣。(事實上和long同樣,這裏只是由於運行的是32位的python解釋器,若是是64位python解釋器,int是8字節)
>>> type(1L) <type 'long'> >>> type(2l) <type 'long'>
>>> type(123.456) <type 'float'> >>> type(123456123456.123456123456123456123456) <type 'float'>
>>> type(3+4j) <type 'complex'> >>> type(3+4J) <type 'complex'> >>> type(4j) <type 'complex'> >>> type(j) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'j' is not defined >>> type(1j) <type 'complex'>可是1j不容許直接寫成j,j會被當作name查找,若是沒找到就會報錯。
>>> type([1, 2, 3]) <type 'list'> >>> type({2, 3, 4}) <type 'set'> >>> type((3, 4, 5)) <type 'tuple'> >>> type({'key1': 'value1', 'key2': 'value2'}) <type 'dict'>能夠看到 (), [], {}和它括起來的一系列元素,分別是表示:元組、列表、集合。而dict則是{key1: value1, [key2: value2, ...]}的形式。
>>> (1, 'two', 3.0) (1, 'two', 3.0) >>> [(1, 'two', 3.0), '4', 5] [(1, 'two', 3.0), '4', 5] >>> {1, 2L, 3.0, 4j} set([1, 2L, 3.0, 4j]) >>> {1: 'one', 'one': 1} {1: 'one', 'one': 1}
控制結構小程序
>>> print "hello, world" hello, world而且Python程序沒有所謂的「入口」,這和多數腳本語言相似。
>>> a = 123 >>> b = "asdf" >>> c = [3, 4, 5] >>> a 123 >>> b 'asdf' >>> c [3, 4, 5] >>> a = b >>> b 'asdf'
def sayHello(name): print 'Hello, ' + name + '!' sayHello('Jack')這段代碼的運行結果爲:Hello, Jack!
class Man: def __init__(self, name): self.name = name def hello(self): print 'Hello, ' + self.name + '!' m = Man('Jack') m.hello()這段代碼也會輸出:Hello, Jack!
>>> type(sayHello) <type 'function'> >>> type(Man) <type 'classobj'> >>> type(m) <type 'instance'> >>> type(m.hello) <type 'instancemethod'> >>> type(Man.hello) <type 'instancemethod'>能夠想象,Python世界裏的東西都是」灰色「的,解釋器對它們」一視同仁「,歷來不以貌取人,只看他們如今身上的標籤是什麼~
Python的選擇結構以if開始。函數
>>> type(1==1) <type 'bool'> >>> type(True) <type 'bool'> >>> type(False) <type 'bool'>
>>> if 1: ... print "true" ... true >>> if 0: ... print "true" ... else: ... print "false" ... false >>> if 0.0: ... print "0.0 is true" ... >>> if 0j: ... print "0j is true" ...提示:Python是以代碼縮進區分代碼塊的
>>> if '': ... print 'null string is true' ... >>> if (): ... print 'null tuple is true' ... >>> if []: ... print 'null list is true' ... >>> if {}: ... print 'null set is true' ...
>>> x = int(raw_input("Please enter an integer: ")) Please enter an integer: 42 >>> if x < 0: ... x = 0 ... print 'Negative changed to zero' ... elif x == 0: ... print 'Zero' ... elif x == 1: ... print 'Single' ... else: ... print 'More' ... More
>>> a = [1, 'two', 3.0] >>> for i in a: ... print i ... 1 two 3.0這種for迭代集合很方便。
>>> for i in range(1, 6): ... print i ... 1 2 3 4 5 >>> for i in range(10, 65, 10): ... print i ... 10 20 30 40 50 60這裏展現了range的兩種調用形式,一種是range(a, b),它將返回一個從a(包含a)到b(不包含)的整數列表(list),另外一種range(a, b, s),將返回一個a~b,以s爲步長的list:
>>> range(1, 6) [1, 2, 3, 4, 5] >>> range(10, 65, 10) [10, 20, 30, 40, 50, 60]
>>> i = 1 >>> >>> while i < 5: ... i = i+1 ... print i ... 2 3 4 5
>>> i 5 >>> i += 1 >>> i 6 >>> i++ File "<stdin>", line 1 i++ ^ SyntaxError: invalid syntax >>> ++i 6 >>> i 6各位可能會疑惑,爲何++i能夠?由於pyhon支持前置的+(正負號)運算,++被當作兩次正運算了;同理,+++i,++++i都是同樣的;咱們能夠順便測一下負號運算:
>>> i 6 >>> +++i 6 >>> ++++i 6 >>> -i -6 >>> --i 6 >>> ---i -6和想象的結果一致,Great!
輸入輸出(IO)oop
>>> varA = raw_input('please input:') please input:Life is too short, you need Python! >>> varA 'Life is too short, you need Python!' >>> type(raw_input('input something:')) input something:asdf <type 'str'> >>> type(raw_input('input something:')) input something:123 <type 'str'>A:你看到了,raw_input不論你輸入什麼都會返回str類型,這也是爲何叫作raw_input的緣由。
>>> type(input('input sth:')) input sth:123 <type 'int'> >>> type(input('input sth:')) input sth:asdf Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'asdf' is not defined >>> type(input('input sth:')) input sth:varA <type 'str'> >>> input('sth:') sth:varA 'Life is too short, you need Python!' >>> input('try some input like your code:') try some input like your code:[1, 'two', 3.0] [1, 'two', 3.0] >>> input('try again:') try again:'Oh!!!' 'Oh!!!'