http://blog.csdn.net/hitlion2008/article/details/9285785
重要说明
这不是给编程新手准备的教程,如果您入行编程不久,或者还没有使用过1到2门编程语言,请移步!这是有一定编程经验的人准备的.最好是熟知Java或C,懂得命令行,Shell等.总之,这是面向老鸟的,让老鸟快速上手Python教程.
为什么总结这样的一个教程
我虽不是老鸟,但已熟悉Java,C/C++, Shell和Perl,且对常见的数据结构和算法等都了解.最近因项目需要,要做一个小工具,评估后感觉用Python实现最为方便,于是就有了对Python的学习.这时就需要一门快速上手Python的教程:因为编程语言的基本知识,以及如何实现程序对我来说不是难事,关键的就是如何具体使用Python语句来体现程序的逻辑!Python的书籍对我来说内容太多了,没有时间去看,查找也不是很容易!网上的资料又太零散,这就需要一个快速入门Python的教程.
这里重点是以对比的方式来说明Python与其语言的不同之处,和一些Python特有的特性,以能以最快速度能用Python写程序.
输入exit()可以退出命令提示符.
- [alex@alexon:~]$python
- Python 2.7.3 (default, Apr 10 2013, 06:20:15)
- [GCC 4.6.3] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> print 'hello, world'
- hello, world
- >>>
- #!/usr/bin/python
- print 'hello, world'
正数索引 0 1 2 3实例:
数组元素 [1] [3] [5] [7]
负数索引 -4 -3 -2 -1
List是一个对象,它有一此内置的方法,如:
- >>> a = [1, 3, 5, 7];
- >>> a[0]
- 1
- >>> a[3]
- 7
- >>> a[-1]
- 7
- >>> a[-2]
- 5
- >>> a[0:3]
- [1, 3, 5]
- >>> a[1:3:2]
- [3]
- >>> a[0:3:2]
- [1, 5]
- >>> a[0:-1:2]
- [1, 5]
- >>>
- >>> 3 in a
- True
- >>> 8 in a
- False
- >>> 8 not in a
- True
- >>>
- >>> a + [9, 11]
- [1, 3, 5, 7, 9, 11]
- >>> a * 2
- [1, 3, 5, 7, 1, 3, 5, 7]
- >>>
- >>> str = 'hello, world'
- >>> str[0:3]
- 'hel'
- >>> str[0:3:2]
- 'hl'
- >>> str[-1]
- 'd'
- >>> str * 2
- 'hello, worldhello, world'
- >>> '3' in str
- False
- >>> 'le' in str
- False
- >>> 'el' in str
- True
- >>> 'ell' not in str
- False
- >>>
formats % (var1, var2,....)它返回的是一个String.
- >>> "Int %d, Float %d, String '%s'" % (5, 2.3, 'hello')
- "Int 5, Float 2, String 'hello'"
- >>>
- >>> box = {'fruits': ['apple','orange'], 'money': 1993, 'name': 'obama'}
- >>> box['fruits']
- ['apple', 'orange']
- >>> box['money']
- 1993
- >>> box['money'] = 29393
- >>> box['money']
- 29393
- >>> box['nation'] = 'USA'
- >>> box
- {'money': 29393, 'nation': 'USA', 'name': 'obama', 'fruits': ['apple', 'orange']}
- >>> box.keys()
- ['money', 'nation', 'name', 'fruits']
- >>> box.values()
- [29393, 'USA', 'obama', ['apple', 'orange']]
- >>>
其中逻辑表达式可以加上括号(),也可以不加.但如果表达式里面比较混乱,还是要加上括号,以提高清晰.但整体的逻辑表达式是可以不加的:
- if expression:
- blocks;
- elif expression2:
- blocks;
- else:
- blocks;
- >>> a = 3; b = 4; c = 5;
- >>> if a == b and a != c:
- ... print "Are you sure"
- ... elif (a == c and b == c):
- ... print "All equal"
- ... else:
- ... print "I am not sure"
- ...
- I am not sure
- >>>
while expression:
blocks
- >>> i = 0;
- >>> while i < 3:
- ... print "I am repeating";
- ... i += 1;
- ...
- I am repeating
- I am repeating
- I am repeating
- >>>
for var in list:
blocks;
- >>> msg = "Hello";
- >>> for c in msg:
- ... print c;
- ...
- H
- e
- l
- l
- o
- >>>
list = [expression for var in list condition]它相当于这样的逻辑:
list = [];一句话,相当于这么多逻辑,可见数组推导是一个十分强大的功能:
for var in list:
if condition:
execute expression;
add result of expression to list
return list;
遍历列表a,对其是偶数的项,乘方.
- >>> a = range(4);
- >>> a
- [0, 1, 2, 3]
- >>> [x*x for x in a if x % 2 == 0]
- [0, 4]
- >>>
def function_name(args):调用函数的方式function_name(formal_args):
function_body;
Python中函数也是一个对象,可以赋值,可以拷贝,可以像普通变量那样使用.其实可以理解为C语言中的指针:
- >>> def power(x):
- ... return x*x;
- ...
- >>> power(4)
- 16
- >>>
另外就是匿名函数,或者叫做lambda函数,它没有名字,只有参数和表达式:
- <pre name="code" class="python">>>> d = power;
- >>> d(2)
- 4</pre>
- <pre></pre>
lambda args: expression
lambda最大的用处是用作实参:
- >>> d = lambda x: x*x;
- >>> d(2)
- 4
- >>> def iter(func, list):
- ... ret = [];
- ... for var in list:
- ... ret.append(func(var));
- ... return ret;
- ...
- >>> iter(lambda x: x*x, a)
- [0, 1, 4, 9]
- >>>
print var1, var2, var3
- >>> a
- [0, 1, 2, 3]
- >>> d
- <function <lambda> at 0x7f668c015140>
- >>> print a, d
- [0, 1, 2, 3] <function <lambda> at 0x7f668c015140>
- >>>
print与%结合更为强大:
print formats % (var1, var2, ...):
- >>> print "today is %d, welcome %s" % (2013, 'alex');
- today is 2013, welcome alex
- >>>
其实这没什么神秘的,前面提到过%格式化返回是一个字串,所以print仅是输出字串而已,格式化工作是由%来做的.
- >>> range(4)
- [0, 1, 2, 3]
- >>> range(1,4)
- [1, 2, 3]
- >>> range(1,4,2)
- [1, 3]
- >>>
- >>> check_call("ls -l .", shell=True);
- total 380
- -rw-r--r-- 1 alex alex 303137 Jun 28 23:25 00005.vcf
- drwxrwxr-x 3 alex alex 4096 Jun 28 23:57 3730996syntheticseismogram
- -rw-rw-r-- 1 alex alex 1127 Jun 28 23:45 contacts.txt
- -rw-rw-r-- 1 alex alex 3349 Jun 29 00:19 contacts_vcard.vcf
- drwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Desktop
- drwxr-xr-x 3 alex alex 4096 Jun 22 08:59 Documents
- drwxr-xr-x 9 alex alex 4096 Jul 3 20:34 Downloads
- -rw-r--r-- 1 alex alex 8445 Jun 15 18:17 examples.desktop
- drwxrwxr-x 5 alex alex 4096 Jun 19 23:01 gitting
- -rw-rw-r-- 1 alex alex 0 Jun 19 20:21 libpeerconnection.log
- drwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Music
- -rw-rw-r-- 1 alex alex 148 Jul 4 22:46 persons.txt
- drwxr-xr-x 3 alex alex 4096 Jul 4 23:08 Pictures
- drwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Public
- -rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py
- -rw-rw-r-- 1 alex alex 271 Jul 4 21:28 speech.txt
- -rw-rw-r-- 1 alex alex 93 Jul 3 23:02 speech.txt.bak
- drwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Templates
- drwxrwxr-x 2 alex alex 4096 Jun 22 19:01 Ubuntu One
- drwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Videos
- 0
- >>>
check_call是相当于在Shell上执行一个语句,所以可以发挥想像力,组合Shell命令:
- >>> check_call("ls -l . | grep 'py'", shell=True);
- -rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py
- 0
- >>>
所以,这是相当强大的工具,可以像写Shell脚本那样,结合管道干一些大事!
- >>> a = check_output("ls -l .", shell=True);
- >>> a
- 'total 380\n-rw-r--r-- 1 alex alex 303137 Jun 28 23:25 00005.vcf\ndrwxrwxr-x 3 alex alex 4096 Jun 28 23:57 3730996syntheticseismogram\n-rw-rw-r-- 1 alex alex 1127 Jun 28 23:45 contacts.txt\n-rw-rw-r-- 1 alex alex 3349 Jun 29 00:19 contacts_vcard.vcf\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Desktop\ndrwxr-xr-x 3 alex alex 4096 Jun 22 08:59 Documents\ndrwxr-xr-x 9 alex alex 4096 Jul 3 20:34 Downloads\n-rw-r--r-- 1 alex alex 8445 Jun 15 18:17 examples.desktop\ndrwxrwxr-x 5 alex alex 4096 Jun 19 23:01 gitting\n-rw-rw-r-- 1 alex alex 0 Jun 19 20:21 libpeerconnection.log\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Music\n-rw-rw-r-- 1 alex alex 148 Jul 4 22:46 persons.txt\ndrwxr-xr-x 3 alex alex 4096 Jul 4 23:08 Pictures\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Public\n-rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py\n-rw-rw-r-- 1 alex alex 271 Jul 4 21:28 speech.txt\n-rw-rw-r-- 1 alex alex 93 Jul 3 23:02 speech.txt.bak\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Templates\ndrwxrwxr-x 2 alex alex 4096 Jun 22 19:01 Ubuntu One\ndrwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Videos\n'
- >>> b = check_output("ls -l . | grep 'py'", shell=True);
- >>> b
- '-rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py\n'
- >>>
不用我说你就知道它的强大之处了!唯一需要注意的就是换行符也在里面,处理的时候需要注意!
如:
- import re;
- p = re.compile(expression);
- m = p.search(target);
- if m != None:
- # got match
- else:
- # no match
这些就是Python的入门基础知识, 了解了这些内容,我相信就可以使用Python来完成你的需要,比如写一些小工具之类的.对于大部分的日常工作,或者一些小工具来说,这些东西就足够了!
- >>> message = 'Welcome to the year of 2013';
- >>> import re;
- >>> p = re.compile('(\d+)');
- >>> m = p.search(message);
- >>> m
- <_sre.SRE_Match object at 0x7f668c015300>
- >>> print m.group(1)
- 2013
- >>>