Python入门教程

老鸟的Python入门教程

分类: Python 20399人阅读 评论(8) 收藏 举报
重要说明
这不是给编程新手准备的教程,如果您入行编程不久,或者还没有使用过1到2门编程语言,请移步!这是有一定编程经验的人准备的.最好是熟知Java或C,懂得命令行,Shell等.总之,这是面向老鸟的,让老鸟快速上手Python教程.
为什么总结这样的一个教程
我虽不是老鸟,但已熟悉Java,C/C++, Shell和Perl,且对常见的数据结构和算法等都了解.最近因项目需要,要做一个小工具,评估后感觉用Python实现最为方便,于是就有了对Python的学习.这时就需要一门快速上手Python的教程:因为编程语言的基本知识,以及如何实现程序对我来说不是难事,关键的就是如何具体使用Python语句来体现程序的逻辑!Python的书籍对我来说内容太多了,没有时间去看,查找也不是很容易!网上的资料又太零散,这就需要一个快速入门Python的教程.

这里重点是以对比的方式来说明Python与其语言的不同之处,和一些Python特有的特性,以能以最快速度能用Python写程序.


Python是一门动态语言

与Java,C等相对,Python不用编译,像脚本一样直接运行.这就导致了,所有错误都是运行时的!即使有语法错误,或者异常,如果程序逻辑没有执行到,就不会有错误.比如一个if分支中有语法错误,使用了未定义的函数,但如果未执行到此分支,就可以正常运行.
动态的另外一层意思就是它的类型是动态的,也就是说无需指定变量的类型,在运行时,根据它的内容来决定的类型.

如何运行Python

通常来讲有二种方式,一种方式是交互式的,就像Shell命令行提示符那样,交互式的,输入,就有输出;
在终端输入python命令,就进入了Python的命令提示符中:>>>输入Python语句,解释器就会执行,并输出结果,如:
  1. [alex@alexon:~]$python
  2. Python 2.7.3 (default, Apr10 2013, 06:20:15)
  3. [GCC 4.6.3] on linux2
  4. Type "help", "copyright","credits" or "license" for more information.
  5. >>> print 'hello, world'
  6. hello, world
  7. >>>
[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
>>>
输入exit()可以退出命令提示符.
另外一种方式就是脚本,就像Shell的脚本的一样,把一组命令集合到一起执行,这就能发挥更大的作用.
  1. #!/usr/bin/python
  2. print 'hello, world'
#!/usr/bin/python
print 'hello, world'

Python以缩进来区分语句块

不像Java,C/C++以花括号{}来区分语句块.Python是以缩进来表示语句块,同一缩进级别为同一级别的语句块.
一个脚本文件中的0级缩进是文件加载的时候就会被执行的语句,如上面的print.开启一个新的缩进需要使用:(冒号),代表下一级别的语句块,如条件,循环或者函数定义.
缩进最好使用四个空格.而且要注意缩进要一致,使用空格就全都用空格,使用Tab就都使用Tab,混用就可能得到缩进错误:
IndentationError: unindent does not match any outer indentation level

操作符

与Java和C中十分类似, +(加), -(减), *(乘), /(除), %(求余), **(指数运算), = (赋值).以及减便运算,如 +=, -=, *=和/= 等.
赋值运算与其他语言一致.
逻辑操作
> < <= >= != ==与其他语言一样.
不一样的有not逻辑非,and逻辑与和or逻辑或.

注释与文档

一行当中,从#开始地方就是注释.不会影响下一行.
""引号放在文件的开头,函数的开头或者一个类的开头,就是文档注释,与Java中的/** ... */作用和目的是一样的.

折行

如果一行太长了,写不下了,就需要在下一行接着写,这时可以使用\来告诉Python,下一行继续.

一行写多个语句

Python是一个语句放在一行,行尾可以选择性的加上;但如果想在一行放多个语句,就需要用;来分隔语句:
a = 1; b = 2; c = 3;
虽然这在语法上可行,但不是一个好习惯,绝大多数的编程规范都是要一行写一个语句.

基本数据类型

  • int
  • long
  • bool
  • float
与Java中非常接近.可以近似认为一致.bool的值是True和False,或者0(False),非0就是True.

List和Tuple

这就是Java或C中的数组.它是一个容器,能用来顺序的,以整数索引方式检索, 存储一组对象.List用[]来表示,如[1, 2, 3]就是一个List;而Tuple用()来表示,如(3, 4, 5)就是一个Tuple.它们的区别在于List是可变的;而Tuple是不可变的.也就是说不可以增,删和改.
索引方式除了与Java一样的以一个整数下标方式外,还可以指定开始,结束和步长,和使用负索引来分割List:
通用语法格式是:list[start:end:step]
  • list[index] --- 返回第(index+1)个元素,受C语言影响,下标亦是从0开始
  • list[start:end] --- 返回从start开始,到end-1,也就是list[start], list[start+1].....list[end-1]
  • list[start:end:step] --- 与上面类似,只不过每隔step取一个
  • list[:end] ---- 缺省的开端是0
  • list[start:] ---- 缺省的结尾是len(list),或者-1
负数索引更是方便,它与正数的对应关系为:
正数索引 0 1 2 3
数组元素 [1] [3] [5] [7]
负数索引 -4 -3 -2 -1
实例:
  1. >>> a = [1, 3,5, 7];
  2. >>> a[0]
  3. 1
  4. >>> a[3]
  5. 7
  6. >>> a[-1]
  7. 7
  8. >>> a[-2]
  9. 5
  10. >>> a[0:3]
  11. [1, 3, 5]
  12. >>> a[1:3:2]
  13. [3]
  14. >>> a[0:3:2]
  15. [1, 5]
  16. >>> a[0:-1:2]
  17. [1, 5]
  18. >>>
>>> 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]
>>>
List是一个对象,它有一此内置的方法,如:
  • 包含关系: in, not in
  1. >>> 3 in a
  2. True
  3. >>> 8 in a
  4. False
  5. >>> 8 notin a
  6. True
  7. >>>
>>> 3 in a
True
>>> 8 in a
False
>>> 8 not in a
True
>>>
  • 连接符: +
  1. >>> a + [9, 11]
  2. [1, 3, 5, 7, 9, 11]
>>> a + [9, 11]
[1, 3, 5, 7, 9, 11]
  • 重复: *
  1. >>> a * 2
  2. [1, 3, 5, 7, 1, 3, 5, 7]
  3. >>>
>>> a * 2
[1, 3, 5, 7, 1, 3, 5, 7]
>>>

字符串String

字符串就是一个字符的数组,List的操作都可以对String直接使用.
  1. >>> str = 'hello, world'
  2. >>> str[0:3]
  3. 'hel'
  4. >>> str[0:3:2]
  5. 'hl'
  6. >>> str[-1]
  7. 'd'
  8. >>> str * 2
  9. 'hello, worldhello, world'
  10. >>> '3' in str
  11. False
  12. >>> 'le' in str
  13. False
  14. >>> 'el' in str
  15. True
  16. >>> 'ell' not in str
  17. False
  18. >>>
>>> 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
>>>

字串格式化符%

这是一个类似C语言printf和Java中的String.format()的操作符,它能格式化字串,整数,浮点等类型:语句是:
formats % (var1, var2,....)
它返回的是一个String.
  1. >>> "Int %d, Float %d, String '%s'" % (5,2.3, 'hello')
  2. "Int 5, Float 2, String 'hello'"
  3. >>>
>>> "Int %d, Float %d, String '%s'" % (5, 2.3, 'hello')
"Int 5, Float 2, String 'hello'"
>>>

Dictionary字典

相当于Java中的HashMap,用于以Key/Value方式存储的容器.创建方式为{key1: value1, key2: value2, ....}, 更改方式为dict[key] = new_value;索引方式为dict[key]. dict.keys()方法以List形式返回容器中所有的Key;dict.values()以List方式返回容器中的所有的Value:
  1. >>> box = {'fruits': ['apple','orange'],'money': 1993, 'name': 'obama'}
  2. >>> box['fruits']
  3. ['apple', 'orange']
  4. >>> box['money']
  5. 1993
  6. >>> box['money'] = 29393
  7. >>> box['money']
  8. 29393
  9. >>> box['nation'] = 'USA'
  10. >>> box
  11. {'money': 29393,'nation': 'USA', 'name': 'obama', 'fruits': ['apple','orange']}
  12. >>> box.keys()
  13. ['money', 'nation','name', 'fruits']
  14. >>> box.values()
  15. [29393, 'USA','obama', ['apple', 'orange']]
  16. >>>
>>> 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']]
>>>

分支语句

格式为:
  1. if expression:
  2. blocks;
  3. elif expression2:
  4. blocks;
  5. else:
  6. blocks;
if expression:
      blocks;
elif expression2:
      blocks;
else:
      blocks;
其中逻辑表达式可以加上括号(),也可以不加.但如果表达式里面比较混乱,还是要加上括号,以提高清晰.但整体的逻辑表达式是可以不加的:
  1. >>> a = 3; b = 4; c =5;
  2. >>> if a == b and a != c:
  3. ... print "Are you sure"
  4. ... elif (a == c and b == c):
  5. ... print "All equal"
  6. ... else:
  7. ... print "I am not sure"
  8. ...
  9. I am not sure
  10. >>>
>>> 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循环

与Java中类似:
while expression:
blocks
  1. >>> i = 0;
  2. >>> while i < 3:
  3. ... print "I am repeating";
  4. ... i += 1;
  5. ...
  6. I am repeating
  7. I am repeating
  8. I am repeating
  9. >>>
>>> i = 0; 
>>> while i < 3:
... print "I am repeating";
... i += 1;
... 
I am repeating
I am repeating
I am repeating
>>>

for语句

与Java中的foreach语法一样, 遍历List:
for var in list:
blocks;
  1. >>> msg = "Hello";
  2. >>> for c in msg:
  3. ... print c;
  4. ...
  5. H
  6. e
  7. l
  8. l
  9. o
  10. >>>
>>> msg = "Hello";
>>> for c in msg:
... print c;
... 
H
e
l
l
o
>>>

数组推导

这是Python最强大,也是最性感的功能:
list = [expression for var in list condition]
它相当于这样的逻辑:
list = [];
for var in list:
if condition:
execute expression;
add result of expression to list
return list;
一句话,相当于这么多逻辑,可见数组推导是一个十分强大的功能:
  1. >>> a = range(4);
  2. >>> a
  3. [0, 1, 2, 3]
  4. >>> [x*x for x in aif x % 2 == 0]
  5. [0, 4]
  6. >>>
>>> a = range(4);
>>> a
[0, 1, 2, 3]
>>> [x*x for x in a if x % 2 == 0]
[0, 4]
>>>
遍历列表a,对其是偶数的项,乘方.

函数

如何定义函数
def function_name(args):
function_body;
调用函数的方式function_name(formal_args):
  1. >>> def power(x):
  2. ... return x*x;
  3. ...
  4. >>> power(4)
  5. 16
  6. >>>
>>> def power(x):
... return x*x;
... 
>>> power(4)
16
>>>
Python中函数也是一个对象,可以赋值,可以拷贝,可以像普通变量那样使用.其实可以理解为C语言中的指针:
  1. <PRE class=python name="code">>>> d = power;
  2. >>> d(2)
  3. 4</PRE>
  4. <PRE></PRE>
<div class="dp-highlighter bg_python" sizcache="29" sizset="145"><div class="bar" sizcache="29" sizset="145"><div class="tools" sizcache="29" sizset="145"><strong>[python]</strong> <a target=_blank class="ViewSource" title="view plain" href="http://blog.csdn.net/hitlion2008/article/details/9285785#">view plain</a><a target=_blank class="CopyToClipboard" title="copy" href="http://blog.csdn.net/hitlion2008/article/details/9285785#">copy</a><a target=_blank class="PrintSource" title="print" href="http://blog.csdn.net/hitlion2008/article/details/9285785#">print</a><a target=_blank class="About" title="?" href="http://blog.csdn.net/hitlion2008/article/details/9285785#">?</a></div></div><ol class="dp-py"><li class="alt">>>> d = power;  </li><li>>>> d(<span class="number">2</span>)  </li><li class="alt"><span class="number">4</span>  </li></ol></div><pre style="DISPLAY: none" class="python" name="code">>>> d = power;
>>> d(2)
4
 
  
 
 
另外就是匿名函数,或者叫做lambda函数,它没有名字,只有参数和表达式:
lambda args: expression
  1. >>> d = lambda x: x*x;
  2. >>> d(2)
  3. 4
>>> d = lambda x: x*x;
>>> d(2)
4
lambda最大的用处是用作实参:
  1. >>> def iter(func, list):
  2. ... ret = [];
  3. ... for var in list:
  4. ... ret.append(func(var));
  5. ... return ret;
  6. ...
  7. >>> iter(lambda x: x*x, a)
  8. [0, 1, 4, 9]
  9. >>>
>>> 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 --- 打印输出
print var1, var2, var3
  1. >>> a
  2. [0, 1, 2, 3]
  3. >>> d
  4. <function <lambda> at 0x7f668c015140>
  5. >>> print a, d
  6. [0, 1, 2, 3] <function <lambda> at0x7f668c015140>
  7. >>>
>>> 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, ...):
  1. >>> print "today is %d, welcome %s" % (2013,'alex');
  2. today is 2013, welcome alex
  3. >>>
>>> print "today is %d, welcome %s" % (2013, 'alex');
today is 2013, welcome alex
>>>

其实这没什么神秘的,前面提到过%格式化返回是一个字串,所以print仅是输出字串而已,格式化工作是由%来做的.

  • len()---返回列表,字串的长度
  • range([start], stop, [step]) --- 生成一个整数列表,从,start开始,到stop结束,以step为步长

  1. >>> range(4)
  2. [0, 1, 2, 3]
  3. >>> range(1,4)
  4. [1, 2, 3]
  5. >>> range(1,4,2)
  6. [1, 3]
  7. >>>
>>> range(4)
[0, 1, 2, 3]
>>> range(1,4)
[1, 2, 3]
>>> range(1,4,2)
[1, 3]
>>>

  • help(func)---获取某个函数的帮助文档.

执行系统命令行命令

import subprocess;
  • check_call(commands, shell=True)可以执行一个命令,并检查结果:
  1. >>> check_call("ls -l .", shell=True);
  2. total 380
  3. -rw-r--r-- 1 alex alex 303137 Jun 28 23:2500005.vcf
  4. drwxrwxr-x 3 alex alex 4096 Jun28 23:573730996syntheticseismogram
  5. -rw-rw-r-- 1 alex alex 1127 Jun 28 23:45 contacts.txt
  6. -rw-rw-r-- 1 alex alex 3349 Jun29 00:19 contacts_vcard.vcf
  7. drwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Desktop
  8. drwxr-xr-x 3 alex alex 4096 Jun22 08:59 Documents
  9. drwxr-xr-x 9 alex alex 4096 Jul 3 20:34 Downloads
  10. -rw-r--r-- 1 alex alex 8445 Jun15 18:17 examples.desktop
  11. drwxrwxr-x 5 alex alex 4096 Jun 19 23:01 gitting
  12. -rw-rw-r-- 1 alex alex 0 Jun19 20:21 libpeerconnection.log
  13. drwxr-xr-x 2 alex alex 4096 Jun 15 18:43 Music
  14. -rw-rw-r-- 1 alex alex 148 Jul4 22:46 persons.txt
  15. drwxr-xr-x 3 alex alex 4096 Jul 4 23:08 Pictures
  16. drwxr-xr-x 2 alex alex 4096 Jun15 18:43 Public
  17. -rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py
  18. -rw-rw-r-- 1 alex alex 271 Jul4 21:28 speech.txt
  19. -rw-rw-r-- 1 alex alex 93 Jul 3 23:02 speech.txt.bak
  20. drwxr-xr-x 2 alex alex 4096 Jun15 18:43 Templates
  21. drwxrwxr-x 2 alex alex 4096 Jun 22 19:01 Ubuntu One
  22. drwxr-xr-x 2 alex alex 4096 Jun15 18:43 Videos
  23. 0
  24. >>>
>>> 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命令:

  1. >>> check_call("ls -l . | grep 'py'", shell=True);
  2. -rw-rw-r-- 1 alex alex 65 Jul8 22:15 py.py
  3. 0
  4. >>>
>>> check_call("ls -l . | grep 'py'", shell=True);
-rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py
0
>>>

所以,这是相当强大的工具,可以像写Shell脚本那样,结合管道干一些大事!
  • check_output(cmds, shell=True)执行命令,并以字串形式返回结果:
  1. >>> a = check_output("ls -l .", shell=True);
  2. >>> a
  3. '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'
  4. >>> b = check_output("ls -l . | grep 'py'", shell=True);
  5. >>> b
  6. '-rw-rw-r-- 1 alex alex 65 Jul 8 22:15 py.py\n'
  7. >>>
>>> 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'
>>>

不用我说你就知道它的强大之处了!唯一需要注意的就是换行符也在里面,处理的时候需要注意!

正则表达式

Python也是支持正则表达式的,至于正则表达式,跟其他的语言如Java,C没什么差别,这里说说如何使用正则表达式来进行匹配:
  1. import re;
  2. p = re.compile(expression);
  3. m = p.search(target);
  4. if m != None:
  5. # got match
  6. else:
  7. # no match
import re;
p = re.compile(expression);
m = p.search(target);
if m != None:
    # got match
else:
    # no match
如:
  1. >>> message = 'Welcome to the year of 2013';
  2. >>> import re;
  3. >>> p = re.compile('(\d+)');
  4. >>> m = p.search(message);
  5. >>> m
  6. <_sre.SRE_Match object at 0x7f668c015300>
  7. >>> print m.group(1)
  8. 2013
  9. >>>
>>> 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
>>>

这些就是Python的入门基础知识, 了解了这些内容,我相信就可以使用Python来完成你的需要,比如写一些小工具之类的.对于大部分的日常工作,或者一些小工具来说,这些东西就足够了!
但是如果想学会Python,或者想学好Python,这才是一个小小的开端,最好读一读<Python核心编程>这本书,相当的不错.

推荐资料:

  • Learn Python the Hard Way这是一个相当好的网站.它的最大优点在于以实例为核心来讲解.缺点就是讲的不是很深入,非常适合入门汉.
  • <Core Python Programming>这本书相当的好,内容详细,且有练习题可以做
  • Coding Bat这上面有很多Python的练习题,很多都是关于String和List的,非常适合初学者练手
  • Python for fun这上面有很多小型的项目.可以用来练习.

文章来源:http://blog.csdn.net/hitlion2008/article/details/9285785
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值