免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2196 | 回复: 0
打印 上一主题 下一主题

python起步2 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-25 18:20 |只看该作者 |倒序浏览

第7章 函数
1、函数定义:通过def关键字定义,后跟 一个函数的标识符名称,然后一对圆括号,后再跟冒号。
2、函数形参
参数在函数定义的圆括号对内指定,用逗号分割。
术语——函数中的参数名称为 形参 而你提供给函数调用的值称为 实参 。
3、局部变量
def func():
    global x
    print 'x is', x
    x = 2
    print 'Changed local x to', x
x = 50
func()
print 'Value of x is', x
global语句:被用来声明x是全局的——因此,当我们在函数内把值赋给x的时候,这个变化也反映在我们在主块中使用x的值的时候。
4、默认参数值
在函数定义的形参名后加上赋值运算符(=)和默认值,从而给形参指定默认参数值。
def say(message,times = 1):
    print message * times
注:say的函数用来打印一个字符串任意所需的次数重要:只有在形参表末尾的那些参数可以有默认参数值
5、关键参数
使用名字(关键字)而不是位置来给函数指定实参。
def func(a, b=5, c=10):
    print 'a is', a, 'and b is', b, 'and c is', c
func(3, 7)
func(25, c=24)
func(c=50, a=100)
6、return语句
没有返回值的return语句等价于return None。None是Python中表示没有任何东西的特殊类型。
def someFunction():
    pass
pass语句在Python中表示一个空的语句块。
7、DocStrings:文档字符串
def printMax(x, y):
    '''Prints the maximum of two numbers.
    The two values must be integers.'''
    x = int(x) # convert to integers, if possible
    y = int(y)
    if x > y:
        print x, 'is maximum'
    else:
        print y, 'is maximum'
printMax(3, 5)
print printMax.__doc__
help(printMax)
输出为:
E:\02_study\21-python\prac_code>python func_doc.py
5 is maximum
Prints the maximum of two numbers.
    The two values must be integers.
Help on function printMax in module __main__:
printMax(x, y)
    Prints the maximum of two numbers.
    The two values must be integers.
注:
在函数的第一个逻辑行的字符串是这个函数的 文档字符串
使用__doc__(注意双下划线)调用printMax函数的文档字符串属性(属于函数的名称)。
10:43 2008-9-25
第8章 模块
模块基本上就是一个包含了所有你定义的函数和变量的文件。
为了在其他程序中重用模块,模块的文件名必须以.py为扩展名。
1、使用标准库模块。
import sys
print 'The command line arguments are:'
for i in sys.argv:
    print i
print '\n\nThe PYTHONPATH is', sys.path, '\n'
运行后输出:
E:\02_study\21-python\prac_code>python using_sys.py we are arguments
The command line arguments are:
using_sys.py
we
are
arguments
The PYTHONPATH IS  ['E:\\02_study\\21-python\\prac_code',
'C:\\WINDOWS\\system32
[url=file://\\python23.zip']\\python23.zip'[/url]
, 'E:\\02_study\\21-python\\prac_code', 'D:\\Python23
[url=file://\\DLLs']\\DLLs'[/url]
, 'D:
[url=file://\\Python23\\lib']\\Python23\\lib'[/url]
, 'D:\\Python23\\lib\\plat-win', 'D:\\Python23
[url=file://\\lib\\lib-tk']\\lib\\lib-tk'[/url]
, 'D
:\\Python23', 'D:\\Python23\\lib\\site-packages']
注:
(1)、利用import语句 输入 sys模块。
(2)sys模块包含了与Python解释器和它的环境有关的函数。
(3)、sys.argv包含了 命令行参数 的列表,即使用命令行传递给你的程序的参数。
脚本的名称总是sys.argv列表的第一个参数,即'using_sys.py'是sys.argv[0],'we'是sys.argv[1]。。。
(4)、sys.path包含输入模块的目录名列表。
2、字节编译的.pyc文件
字节编译的文件与Python变换程序的中间状态有关,是与平台无关的,使输入模块更加快一些的
3、from..import语句
如果你想要直接输入argv变量到你的程序中(避免在每次使用它时打sys.),那么你可以使用from sys import argv语句。
如果你想要输入所有sys模块使用的名字,那么你可以使用from sys import *语句。
应尽量避免
4、模块的__name__
if __name__ == '__main__':
    print 'This program is being run by itself'
else:
    print 'I am being imported from another module'
注:每个Python模块都有它的__name__
假如我们只想在程序本身被使用的时候运行主块,而在它被别的模块输入的时候不运行主块,可用上面的代码来分开。
5、创建自己的模块
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
注:个人感觉像类,还是不用实例化的静态类。
用from...import的例子
from mymodule import sayhi,version
sayhi()
print 'vresion',version
6、dir函数
(1)、可以使用内建的dir函数来列出模块定义的标识符。标识符有函数、类和变量。
>>> import mymodule
>>> dir(mymodule)
['__builtins__', '__doc__', '__file__', '__name__', 'sayhi',
'version']
>>>
(2)、如果不传参数,返回当前模块的属性列表,输入的模块同样是列表的一部分。
>>> dir()
['__builtins__', '__doc__', '__name__', 'mymodule', 'sys']
>>>
(3)、往列表中增、减
>>> a = 5
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'mymodule', 'sys']
>>> del a
>>> dir()
['__builtins__', '__doc__', '__name__', 'mymodule', 'sys']
>>>
注:于del的一点注释——这个语句在运行后被用来 删除 一个变量/名称。
del a,你将无法再使用变量a——它就好像从来没有存在过一样。
12:38 2008-9-25
第9章 数据结构
在Python中有三种内建的数据结构——列表、元组和字典。
17:18 2008-9-25
1、列表
列表是 可变的 数据类型
是处理一组有序项目的数据结构。
(1)、初始化
shoplist = ['apple','mango','carrot','banana']
注:在列表中添加 任何种类的对象 包括数甚至其他列表。
(2)、逐个显示
print 'these items are:',
for item in shoplist:
    print item,
注:在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符。
(3)、使用append方法在列表中添加了一个项目,并打印列表的内容
shoplist.append('rice')
print 'my shopping list is now',shoplist
(4)、使用列表的sort方法来对列表排序,这个方法影响列表本身,而不是返回一个修改后的列表
shoplist.sort()
(5)、使用del语句来删除
print 'The first item I will buy is',shoplist[0]
olditem = shoplist[0]
del shoplist[0]
(6)、可以通过help(list)获得完整的知识。
2、元组
元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组。
(1)、元组通过圆括号中用逗号分割的项目。
zoo = ('wolf', 'elephant', 'penguin')
(2)、访问
通过一对方括号来指明位置,这被称作 索引 运算符。我们使用new_zoo[2]来访问new_zoo中的第三个项目。我们使用new_zoo[2][2]来访问new_zoo元组的第三个项目的第三个项目。
(3)、含有0个或1个项目的元组。
一个空的元组由一对空的圆括号组成,如myempty = ()。
含有单个元素的元组:必须在第一个(唯一一个)项目后跟一个逗号,这样Python才能区分元组和表达式中一个带圆括号的对象。
(4)、元组与打印语句
age = 22
name = 'Swaroop'
print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name
注:print语句可以使用跟着%符号的项目元组的字符串。定制让输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。元组必须按照相同的顺序来对应这些定制。
%符号后的单个项目——没有圆括号,这只在字符串中只有一个定制的时候有效。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/28123/showart_1225421.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP