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!!!'