Python函数第一节
来源:互联网 发布:python 有抽象类吗? 编辑:程序博客网 时间:2024/05/29 14:57
# coding=utf-8# 1.定义一个方法 func,该func可以引入任意多的整型参数,结果返回其中最大与最小的值。import globdef func1(*nums): for i in nums: if not isinstance(i, int): return "error" return max(nums), min(nums)# isinstance的用法:判断对象类型a = 10isinstance(a, int) # Trueisinstance(a, (int, float)) # True# isinstance和type的区别:(强烈建议用isinstance而不用type)class A(): passclass B(A): passisinstance(A(), A) # Truetype(A()) == A # Falseisinstance(B(), A) # Truetype(B()) == A # False# 2.定义一个方法func,该func可以引入任意多的字符串参数,结果返回(长度)最长的字符串。def func2(*strl): max_str = '' for i in strl: if not isinstance(i, str): return "error" else: max_str = i if len(i) > len(max_str) else max_str return max_str# 3.定义一个方法get_doc(module),module参数为该脚本中导入或定义的模块对象,该函数返回module的帮助文档。def get_doc(module): return help(module)# 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')
0 0
- Python函数第一节
- python第一节
- Python 第一节
- python 第一节
- 函数第一节
- 函数第一节
- Python学习,第一节
- 第一节:安装Python
- 第一节 Python基础
- python 初探 第一节
- python基础第一节
- Python入门第一节
- 第一节 Python 简历 (蟒蛇)
- 第五章 第一节:字符函数
- python 第一节 python基本运算符
- 第一节
- 第一节
- 第一节
- mysql 5.7 安装手册(for linux)
- extjs6 基础组件扩展和自定义组件封装--04静态js方法的设计思想
- HDU1698(线段树)
- 配置Windows 防火墙,允许SQL Server的远程连接
- Zookeeper作用
- Python函数第一节
- Js代码用<script>调用时,放在<head>与<body>的区别
- Oracle集成云服务Integration Cloud Service
- 欢迎使用CSDN-markdown编辑器
- hdu5540 Secrete Master Plan--矩阵旋转
- Android Studio Monitor no debuggable applications
- Android原生应用集成ReactNative坑总结
- 关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码
- Android监听软键盘打开收起事件(软键盘自带收起按钮)