Python Function 函数
来源:互联网 发布:淘宝banner在线生成 编辑:程序博客网 时间:2024/05/23 14:00
自定义函数
def name ( parameter1 , parameter2 ) : # 别忘了冒号 参数可以有任意个(貌似可以没有) res = parameter1 * parameter2 #pyhon 用空格缩进 return res # 可以没有返回值
Python 模块 里的函数
print sqrt(25)import math #generic importprint math.sqrt(25)
这样每次调用要用math.sqrt()很麻烦
所以有以下的方法
from math import sqrtprint sqrt(25)
然后还有一次性导入 模块 里全部函数的代码
from math import *print sqrt(25)
查看 模块 里所有的
import math # Imports the math moduleeverything = dir(math) # Sets everything to a list of things from mathprint everything # Prints 'em all!
不用从模块导入就可以使用的函数?
a.upper() #将a全转化为大写字母a.lower() #将a全转化为小写字母str(a) #返回a的字符串形式len(a) #返回a的长度max(a,b,c,d) #返回最大的(参数数量>1),可以包含int和floatmin(a,b,c,d) #返回最小的(参数数量>1),可以包含int和floatabs(a) #返回a的绝对值
# type(a) 返回a的数据类型print type(42) #返回 intprint type(4.2)print type('spam')# Python will output:<type 'int'><type 'float'><type 'str'>
附一段随意的代码
def distance_from_zero (a) : if type (a) == int or type(a) == float : return abs(a); else : return "Nope"
骚操作?
def biggest_number(*args): print max(args) return max(args)def smallest_number(*args): print min(args) return min(args)def distance_from_zero(arg): print abs(arg) return abs(arg)biggest_number(-10, -5, 5, 10)smallest_number(-10, -5, 5, 10)distance_from_zero(-10)
阅读全文
1 0
- Python function 函数定义
- Python Function 函数
- Python - 函数(function) 的 代码 及 详解
- python内建函数(build-in function)
- Python内置函数(built-in function)
- Python标准库:内置函数classmethod(function)
- Python标准库:内置函数staticmethod(function)
- Python中Function(函数)和methon(方法)
- Python基础-偏函数 Partial function
- Python教程学习简记12--Python Partial function 偏函数
- function Function函数
- Python-function
- 函数 function
- Function函数
- function函数
- 函数Function
- 函数function
- 函数function
- 汽车SMT保险丝不会爆炸---凯利讯半导体
- rewrite方法-1
- 插入排序
- ionic3目录结构
- Python中PILImage和Python转化
- Python Function 函数
- 解决Jetbrains旗下产品的插件下载失败问题(IntelliJ IDEA、RubyMine、WebStorm、PhpStorm、PyCharm、AppCode、Android Studio等)
- 学习笔记:信息检索(0) 导论
- java类自定义实现类排序实现(Comparable或者额外类实现Comparator)
- HTML基本内容总结
- rewrite方法--2
- WebService之JAX-WS注解描述
- 欢迎使用CSDN-markdown编辑器
- java实现的顺序表栈