Chinaunix首页 | 论坛 | 博客
  • 博客访问: 754169
  • 博文数量: 26
  • 博客积分: 8255
  • 博客等级: 中将
  • 技术积分: 934
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-06 01:52
文章分类

全部博文(26)

文章存档

2012年(1)

2011年(8)

2010年(10)

2008年(7)

分类: Python/Ruby

2011-03-13 19:22:04

py 是Python 源码文件扩展名
pyw 是图形用户接口源码文件扩展名  可以用pythonw.exe 解析执行
pyc 是源文件编译后生成的文件,可以在多个平台运行
pyo 是优化过的源码文件,不能再用文本编辑器编辑了
例如:
  1. python -O -m py_compile hello.py # -m表示把导入的py_compile模块作为脚本运行
使用源文件
#!/usr/bin/python  它被称作 组织行 ——源文件的头两个字符是#!,后面跟着一个程序。这行告诉你的Linux/Unix系统当你 执行 你的程序的时候,它应该运行哪个解释器。

数:
在Python中有4种类型的数——  整数、长整数、浮点数和复数。

字符串:
记住,单引号和双引号字符串是完全相同的——它们没有在任何方面有不同。
使用三引号('''或""")利用三引号,你可以指示一个多行的字符串。你可以在三引号中自由的使用单引号和双引号。例如:

  1. '''This is a multi-line string. This is the first line.
  2. This is the second line.
  3. "What's your name?," I asked.
  4. He said "Bond, James Bond.
转义符
这个字符串是What's your name?。你肯定不会用'What's your name?'来指示它,因为Python会弄不明白这个字符串从何处开始,何处结束。
你可以把字符串表示为'What\'s your name?'

Unicode字符串

Unicode是书写国际文本的标准方法。如果你想要用你的母语如北印度语或阿拉伯语写文本,那么你需要有一个支持Unicode的编辑器。类似地,Python允许你处理Unicode文本——你只需要在字符串前加上前缀u或U。例如,u"This is a Unicode string."。

记住,在你处理文本文件的时候使用Unicode字符串,特别是当你知道这个文件含有用非英语的语言写的文本。


缩进:
空白在Python中是重要的。事实上行首的空白是重要的。它称为缩进。在逻辑行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而用来决定语句的分组。
这意味着同一层次的语句必须有相同的缩进。每一组这样的语句称为一个块。

if语句实例:

  1. number = 23
  2. guess = int(raw_input('Enter an integer : '))

  3. if guess == number:
  4.     print 'Congratulations, you guessed it.' # New block starts here
  5.     print "(but you do not win any prizes!)" # New block ends here
  6. elif guess < number:
  7.     print 'No, it is a little higher than that' # Another block
  8.     # You can do whatever you want in a block ...
  9. else:
  10.     print 'No, it is a little lower than that'
  11.     # you must have guess > number to reach here

  12. print 'Done'
  13. # This last statement is always executed, after the if statement is executed
while语句实例:
  1. number = 23
  2. running = True

  3. while running:
  4.     guess = int(raw_input('Enter an integer : '))

  5.     if guess == number:
  6.         print 'Congratulations, you guessed it.'
  7.         running = False # this causes the while loop to stop
  8.     elif guess < number:
  9.         print 'No, it is a little higher than that'
  10.     else:
  11.         print 'No, it is a little lower than that'
  12. else:
  13.     print 'The while loop is over.'
  14.     # Do anything else you want to do here

  15. print 'Done'
for循环
for..in是另外一个循环语句,它在一序列的对象上 递归 即逐一使用队列中的每个项目。
for循环在这个范围内递归——for i in range(1,5)等价于for i in [1, 2, 3, 4]
  1. for i in range(1, 5):
  2.     print i
  3. else:
  4.     print 'The for loop is over'
====输出====
1
2
3
4
The for loop is over

else部分是可选的。如果包含else,它总是在for循环结束后执行一次,除非遇到break语句。
Python的for循环从根本上不同于C/C++的for循环。C#程序员会注意到Python的for循环与C#中的foreach循环十分类似。Java程序员会注意到它与Java 1.5中的for (int i : IntArray)相似。

break语句
break语句是用来 终止 循环语句的,即哪怕循环条件没有称为False或序列还没有被完全递归,也停止执行循环语句。
  1. while True:
  2.     s = raw_input('Enter something : ')
  3.     if s == 'quit':
  4.         break
  5.     print 'Length of the string is', len(s)
  6. print 'Done'
===输出===
Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something :       use Python!
Length of the string is 12
Enter something : quit
Done

定义函数:

函数通过def关键字定义。def关键字后跟一个函数的 标识符 名称,然后跟一对圆括号。圆括号之中可以包括一些变量名,该行以冒号结尾。接下来是一块语句,它们是函数体。

  1. #!/usr/bin/python
  2. # Filename: function1.py

  3. def sayHello():
  4.     print 'Hello World!' # block belonging to the function

  5. sayHello() # call the function
形参和实参

函数中的参数名称为 形参 而你提供给函数调用的值称为 实参 。
  1. #!/usr/bin/python
  2. # Filename: func_param.py

  3. def printMax(a, b):
  4.     if a > b:
  5.         print a, 'is maximum'
  6.     else:
  7.         print b, 'is maximum'

  8. printMax(3, 4) # directly give literal values

  9. x = 5
  10. y = 7

  11. printMax(x, y) # give variables as arguments
输出:
$ python func_param.py
4 is maximum
7 is maximum

局部变量

当你在函数定义内声明变量的时候,它们与函数外具有相同名称的其他变量没有任何关系,即变量名称对于函数来说是 局部 的。这称为变量的 作用域 。所有变量的作用域是它们被定义的块,从它们的名称被定义的那点开始。
  1. #!/usr/bin/python
  2. # Filename: func_local.py

  3. def func(x):
  4.     print 'x is', x
  5.     x = 2
  6.     print 'Changed local x to', x

  7. x = 50
  8. func(x)
  9. print 'x is still', x
输出
$ python func_local.py
x is 50
Changed local x to 2
x is still 50

使用global语句(不鼓励这样做)
如果你想要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,而是 全局 的。我们使用global语句完成这一功能。没有global语句,是不可能为定义在函数外的变量赋值的。

  1. #!/usr/bin/python
  2. # Filename: func_global.py

  3. def func():
  4.     global x

  5.     print 'x is', x
  6.     x = 2
  7.     print 'Changed local x to', x

  8. x = 50
  9. func()
  10. print 'Value of x is', x
输出

$ python func_global.py
x is 50
Changed global x to 2
Value of x is 2

默认参数值

对于一些函数,你可能希望它的一些参数是 可选 的,如果用户不想要为这些参数提供值的话,这些参数就使用默认值。这个功能借助于默认参数值完成。你可以在函数定义的形参名后加上赋值运算符(=)和默认值,从而给形参指定默认参数值。默认参数值应该是不可变的
  1. #!/usr/bin/python
  2. # Filename: func_default.py

  3. def say(message, times = 1):
  4.     print message * times

  5. say('Hello')
  6. say('World', 5)
输出
$ python func_default.py
Hello
WorldWorldWorldWorldWorld

关键参数(其实是一种参数的使用方式)

  1. #!/usr/bin/python
  2. # Filename: func_key.py

  3. def func(a, b=5, c=10):
  4.     print 'a is', a, 'and b is', b, 'and c is', c

  5. func(3, 7)
  6. func(25, c=24)
  7. func(c=50, a=100)
输出
$ python func_key.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50

return语句
  1. #!/usr/bin/python
  2. # Filename: func_return.py

  3. def maximum(x, y):
  4.     if x > y:
  5.         return x
  6.     else:
  7.         return y

  8. print maximum(2, 3)
输出

$ python func_return.py
3

DocStrings
Python有一个很奇妙的特性,称为 文档字符串 ,它通常被简称为 docstrings 。DocStrings是一个重要的工具,由于它帮助你的程序文档更加简单易懂,你应该尽量使用它。你甚至可以在程序运行的时候,从函数恢复文档字符串!文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。 强烈建议 你在你的函数中使用文档字符串时遵循这个惯例。
  1. #!/usr/bin/python
  2. # Filename: func_doc.py

  3. def printMax(x, y):
  4.     '''Prints the maximum of two numbers.

  5.     The two values must be integers.'''
  6.     x = int(x) # convert to integers, if possible
  7.     y = int(y)

  8.     if x > y:
  9.         print x, 'is maximum'
  10.     else:
  11.         print y, 'is maximum'

  12. printMax(3, 5)
  13. print printMax.__doc__
输出
$ python func_doc.py
5 is maximum
Prints the maximum of two numbers.

        The two values must be integers.

使用模块的__name__
每个模块都有一个名称,在模块中可以通过语句来找出模块的名称。这在一个场合特别有用——就如前面所提到的,当一个模块被第一次输入的时候,这个模块的主块将被运行。假如我们只想在程序本身被使用的时候运行主块,而在它被别的模块输入的时候不运行主块,我们该怎么做呢?这可以通过模块的__name__属性完成。
  1. #!/usr/bin/python
  2. # Filename: using_name.py

  3. if __name__ == '__main__':
  4.     print 'This program is being run by itself'
  5. else:
  6.     print 'I am being imported from another module'
输出

$ python using_name.py
This program is being run by itself

$ python
>>> import using_name
I am being imported from another module
>>>

创建你自己的模块
  1. #!/usr/bin/python
  2. # Filename: mymodule.py

  3. def sayhi():
  4.     print 'Hi, this is mymodule speaking.'

  5. version = '0.1'

  6. # End of mymodule.py
记住这个模块应该被放置在我们输入它的程序的同一个目录中,或者在sys.path所列目录之一。

调用模块
  1. #!/usr/bin/python
  2. # Filename: mymodule_demo.py

  3. import mymodule

  4. mymodule.sayhi()
  5. print 'Version', mymodule.version
输出

$ python mymodule_demo.py
Hi, this is mymodule speaking.
Version 0.1

下面是一个使用from..import语法的版本

  1. #!/usr/bin/python
  2. # Filename: mymodule_demo2.py

  3. from mymodule import sayhi, version
  4. # Alternative:
  5. # from mymodule import *

  6. sayhi()
  7. print 'Version', version


阅读(6280) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~