Python2.7 -- 如何使用函数
2018-01-09 10:44
423 查看
函数使用和创建入门
# coding:utf-8 def my_def(): return '你调用我想干啥!!!!!' def my_pass(): pass def my_return_more(x, y, z): return x, y, z print '官网的函数参考:http://docs.python.org/2/library/functions.html#abs' print '函数的用法:%s ' % abs(-20) print '数据类型转换:%s' % int('123') print '调用我的时候,得先声明函数,要注意顺序,调用my_def:%s' % my_def() print '什么都不想干 :%s' % my_pass() x1, y1, z1 = my_return_more(1, 2, 3) print '返回多个值:%s %s %s' % (x1, y1, z1)
千奇百怪的参数
# coding:utf-8 def my(x=2): return x; def my_more(a, x=2): return a, x def my_list(l=[]): return l; def my_charge(number): result = 0 for i in number: result += i return result def my_charge1(*number): result = 0 for i in number: result += i return result def person(name, age, **kw): print 'name:', name, 'age:', age, 'other:', kw print '函数默认的参数: %s' % my() a1, x1 = my_more(1) print '函数多个参数的: %s %s' % (a1, x1) l = [1, 2, 3] print '参数List,返回:%s' % my_list(l) print '可变参数, 返回:%s' % my_charge([1, 1, 1, 1, 2]) print '可变参数, 返回:%s' % my_charge1(1, 1, 1, 1, 2) print '**标识DICT参数类型:%s' % person('Bob', 35, city='Beijing')
其他特性
# coding:utf-8 L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] print '切片操作:', L[0:3] print '切片操作:', L[:3] print '切片操作:', L[1:3] d = {'a': 1, 'b': 2, 'c': 3} def diedai(): for value in d.itervalues(): print value from collections import Iterable def isCollections(): print 'd是否可以进行迭代', isinstance(d, Iterable) print '迭代对象:', diedai() isCollections() list = range(1, 11) print '生成List', list
函数作为返回值
# coding:utf-8 def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sum s = lazy_sum(1, 2, 3, 4, 5) print '函数作为返回值,执行函数:', s()
相关文章推荐
- Python下如何使用第三方函数库
- mac python自带版本2.6与2.7 如何在Python Django 项目里使用新版Python3.x.x
- python当中的yield函数具体如何使用
- 【python】如何批量读取文件夹的所有文件数据,os模块与open函数结合使用实例
- python如何使用生成器函数实现可迭代对象
- Python的enumerate 枚举函数如何使用?
- python 8-1 如何使用多线程,Thread创建线程,执行函数赋值给target//类+函数放在run方法中执行
- 如何在C++中使用类似python的字符串split()函数
- Python中如何避免使用多个分支语句(多个if和else)——函数字典(Function Map)简介
- 如何在系统默认使用的python2.7情况下使用python3环境
- Python中callable函数如何使用?
- 如何使用Python api 函数写股票策略
- python-7-如何使用生成器函数实现可迭代对象?
- python 9-1 如何使用函数装饰器,定义函数装饰器,用来生成一个原函数基础添加新功能的函数,替代原函数
- 学python(03)—— 如何使用函数实现一个随机字符串里的大小写字符互换
- python 9-2 如何为被装饰的函数保存元数据,使用标准库functools中的装饰器wraps 装饰内部包裹函数
- python2.7系列下的print函数使用注意事项
- 如何使用不同参数组合生成独立的TestCase函数(Python)
- 如何使用SetWindowRgn函数创建异形窗口
- 如何使用SendMessage自定义消息函数(VC实例)