相比Java/C++, Python可以写更少的代码,相比Perl,Python也更简单、清晰、易于编写,写大型程序Perl会变得不那么实用), 如果是Linux/Unix下的Shell编程,也最好用Python(该事情Perl排第一,Python第二)[1,2]。
Python的主要用途:
脚本编写,Linux/Unix系统维护;
自动化测试脚本编写(许多自动化测试工具支持Python脚本);
抓取网页,生成报表;
…
Note:关于系统运维的书“Python forUnix and Linux System Administration”
学习Python的好网站:
Ÿ Google Python Class. http://code.google.com/edu/languages/google-python-class/
Ÿ Python官方网站:https://www.python.org/
Ÿ 简明Python教程:http://sebug.net/paper/python/ch01s03.html(学习Python的中文网站)
本文是一个Python学习笔记,内容主要来自Google Python Class.
1 Python安装
一些系统如Linux,Max已经默认安装了Python。测试Python是否安装:
在命令行中执行一个python程序测试能否执行,如Google Python Exercises中的hello.py程序:
python hello.py
Google Python Exercises 下载地址:
https://developers.google.com/edu/python/google-python-exercises.zip
如果没有安装Python
Python下载地址:http://python.org/download
这里安装的是Windowsx86 MSI Installer (2.7.8) (环境:Win8OS, X64)
2 Python基本介绍
Python汲取了Perl的很多优点,但在很多方面优于Perl。Python是一个动态的解释型语言。源代码不需要声明变量的类型,所以代码非常简短、灵活。Python会在运行时追踪所有值的类型。
Python基本语法:
· 以回车标识一条语句的结束,不需要分号。
· 单行的注释以”#”开头
· 后缀名为”.py”
2.1 代码示例
下面是一段Python代码hello.py
# import modules used here -- sys is a verystandard one
import sys
def repeat(s, exclaim):
"""Returns the string s repeated 3 times.
If exclaim is true, add exclamation marks.
"""
result = s + s + s # can also use "s * 3" which isfaster (Why?)
if exclaim:
result = result + '!!!'
return result
# Gather our code in a main() function
def main():
print repeat(sys.argv[1], False)
#Command line args are in sys.argv[1], sys.argv[2] ...
# sys.argv[0] is the script name itself and can be ignored
# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
main()
函数体的格式是每行的缩进,而不是像其他语言位于一个花括号内。函数需要在调用前通过def定义,一个典型的做法是在文件的最后定义main函数,它调用的函数位于main之前。
在一个标准的Python程序中,sys.argv包含了所有的命令行参数,sys.argv[0]是程序的名字,后续的是参数。
2.2 代码检查
Python在编译时只做很少的代码检查,主要是在运行时进行。所以很可能代码中含有错误,只要运行时没有跑这一行,就运行正常。
2.3 Python Module
一个Python文件就是一个module,可以单独执行,也可以被其他python文件引用。一个函数的完整限定名包含了文件名,如上面hello.py中的repeat函数的完整限定名是hello.repeat。
2.4 运行Python代码
Python是解释型语言,所以可以直接输入python代码执行。
在cmd中输入python就可以启动Python解释器,如输入
>>>print ‘Hello world!’
就可以得到运行结果: Hello world!
Cmd中直接输入
>python hello.py
可以运行hello.py程序
也可以运行IDLE,Python自带的集成开发环境。
3 Python的使用
Python的使用非常简单灵活,下面简单介绍了Python中String和List。
3.1 String
Python有一个built-in的字符串对象str。字符串用单双引号括起来都可。Python字符串是不可变的,意味着改变字符串时,会创建新的字符串。
除了普通的[]操作符外,Python有一个[start:end]格式的下标操作符:
0-*:
· s[1:4] is 'ell' -- charsstarting at index 1 and extending up to but not including index 4
· s[1:] is 'ello' -- omittingeither index defaults to the start or end of the string
· s[:] is 'Hello' -- omittingboth always gives us a copy of the whole thing (this is the pythonic way tocopy a sequence like a string or list)
· s[1:100] is 'ello' -- an indexthat is too big is truncated down to the string length
负标:
· [-1] is 'o' -- last char (1stfrom the end)
· s[-4] is 'e' -- 4th from theend
· s[:-3] is 'He' -- going up tobut not including the last 3 chars.
· s[-3:] is 'llo' -- startingwith the 3rd char from the end and extending to the end of the string.
3.2 List
Python有一个强大的内置list,list的值用”[]”括起来:
colors = ['red', 'blue', 'green']
通过”=”进行List对象的赋值时,只是指向同一个对象:
b = colors;
“+”可以作用于List对象,[1,2] +[3,4] 的结果会是[1,2,3,4]。
使用for-in遍历:
squares= [1, 4, 9, 16]
sum = 0
for numin squares:
sum +=num
printsum ## 30
测试一个元素是否在List中:
list = ['larry', 'curly', 'moe']
if'curly' in list:
print'yay'
range函数:range(n)产生0,1,2,…,n-1,range(a,b)产生a, a+1,…,b-1
4 参考
[1] 每个程序员都应该学习使用Python或Ruby.
http://www.oschina.net/news/20020/why-every-programmer-should-learn-python-or-ruby
[2] 简明Python教程. http://sebug.net/paper/python/ch01s03.html