函数第一节
来源:互联网 发布:matlab 2016b mac安装 编辑:程序博客网 时间:2024/06/06 00:50
1.定义一个方法 func,该func可以引入任意多的整型参数,结果返回其中最大与最小的值。
>>> def fun1(*num):
... for i in num:
... if not isinstance(i,int):
... return "error"
... return max(num),min(num)
注意这里的num是一个元组
>>> fun1(1,2,3,4,5)
(5, 1)
2.定义一个方法func,该func可以引入任意多的字符串参数,结果返回(长度)最长的字符串。
>>> def fun1(*num):
... max_str=''
... for i in num:
... if not isinstance(i,str):
... return"error"
... else:
... max_str=i if len(i)>len(max_str) else max_str
... return max_str
...
>>> fun1('adsdsda','ad','sdada1')
'adsdsda'
3.定义一个方法get_doc(module),module参数为该脚本中导入或定义的模块对象,该函数返回module的帮助文档。
>>> def get_doc(module):
... return help(module)
然后就可以查文档如下。。。。
>>> get_doc('def')
Function definitions
********************
4.定义一个方法get_text(f),f参数为任意一个文件的磁盘路径,该函数返回f文件的内容。
>>> def get_text(f):
... return open(f).read()
...
5定义一个方法get_dir(folder),folder参数为任意一个文件夹,该函数返回folder文件夹的文件列表。提示(可以了解python的glob模块)
glob模块很简单,用到三个通配符*?[], *匹配0个或多个字符,?匹配单个字符,[]匹配指定范围内的字符
字符串前面加r防止转义字符的作用,路径中就要加
def get_dir(folder):
return glob.glob(folder+'/*')
print get_dir('D:/pycharm/python2.7.12')
- 函数第一节
- 函数第一节
- Python函数第一节
- 第五章 第一节:字符函数
- 第一节
- 第一节
- 第一节
- 第一节
- 第一节
- C++第一节:类、对象、构造函数、setget
- 第三章第一节 apply族函数
- 《Javascript with Mootools》------函数(第二节),第一节略过
- c++第一节之函数重载与缺省参数
- 辛星解读之php中的重点函数第一节之数组函数
- 第一节 IContextMenu
- 登场!第一节
- bsci第一节
- XML第一节
- 设计模式---状态模式Java示例实现
- dubbo本地缓存提供者信息配置问题
- 4-1 单链表逆转 (20分)
- jsp之简单用户登录(1)
- 数组作为函数参数
- 函数第一节
- Ubuntu 14.04搭建Android5.1开发环境和编译
- 'NSURL' is not implicitly convertible to 'URL'; did you mean to use 'as' to explicitly convert?异常
- 百度地图api 实例 整个自动提示 并计算两地的行驶距离
- Codevs1531 山峰 单调栈
- 使用Activator.CreateInstance完善简单工厂
- linux下ping域名的时候出现ping:unknown host xxx.xxx
- 在安卓工程使用JDK8
- JavaScript中表示空的undefined与null的区别及用法