python 基础 学习 内置函数
内置函数
例:如果返回数字的绝对值 ,写函数是非常不方便的 [root@zabbix tools]# python fa.py 10 [root@zabbix tools]# cat fa.py #!/usr/bin/python def a(x):if x < 0 :return -x return x n = a(-10) print n
#帮助查看#
>>> help(len)
用法: help(函数名)
abs() 返回绝对值
>>> abs(-10) 10
max()返回最大值 , min()返回最小值
>>> l=[1,4,7,12,555,66,] >>> min(l) 1 >>> max(l) 555
len() 获取字符串长度
>>> s = 'konglingchao' >>> len(s) 12
divmod() 求商,求余 用法:divmod(除数,被除数)
>>> divmod(10,2) (5, 0)
pow() 次方运算 用法: pow(数1,数2,/(数3)) 数3可有可无
>>> pow(3,3) 27 >>> pow(3,3,2) 1
round()返回浮点数
>>> round(1112) 1112.0
callable()测试函数是否被定义
>>> callable(min) True #函数定义返回True >>> callable(f) Traceback (most recent call last):File "<stdin>", line 1, in <module> NameError: name 'f' is not defined #函数未被定义 >>> f=100 #f是一个变量 >>> callable(f) False #返回True
isinstance()
>>> print l [1, 4, 7, 12, 555, 66] >>> print f <function f at 0x1a1e320> >>> type(f) <type 'function'> >>> type(l) <type 'list'> >>> if type(l) ==type([]): ... print 'ok' ... ok #判断是否为列表 使用isinstance 判断某一个对象类型 >>> isinstance(l,int) False >>> isinstance(l,str) False >>> isinstance(l,list) True
cmp ()字符串比较
>>> cmp("7","7") 0 >>> cmp("7","m") -1 >>> cmp("7","6") 1
range()快速生成序列
>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
xrange() 可以 理解为对象,效率比range高
>>> x = xrange(10) >>> print xxrange(10)
数据转换类型的函数:
int() 整数
long() 长×××
float() 浮点型
complex() 复数型
str()
list()
tuple()
>>> L=[1,2,3,4] >>> tuple(l) (1, 4, 7, 12, 555, 66)
注意: 数据转换要有选择性的转换
>>> s='hello' >>> int(s) Traceback (most recent call last):File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'hello' >>> s='123' >>> type(s) <type 'str'> >>> int(s) 123
string 函数 :功能只限于对字符串的操作
#查看帮助要 加上 函数的类别:>>> help(str.replace)
str.capitalize() :首字母大写
>>> s='hello' >>> str.capitalize(s) 'Hello' >>> s 'hello word' >>> s.capitalize() #s是字符串里的对象,s调用capitalize函数 'Hello word'
str.replace() :替换,(可以指定替换次数)
>>> s.replace('hello','good') 'good word' >>> s 'hello word' #这里的good word 只是一个返回值,并没有将数据直接改变 >>> ss='12121212121212' >>> ss.replace('1','xxxxx') 'xxxxx2xxxxx2xxxxx2xxxxx2xxxxx2xxxxx2xxxxx2' >>> ss.replace('1','xxxxx',1) #指定替换的次数 'xxxxx2121212121212'
str.split() :切割 str.split(“参数1:切割服”,“参数2=切割次数”)
>>> ip="192.168.1.2"
>>> ip.split('.')
['192', '168', '1', '2']
>>> ip.split('.',2)
['192', '168', '1.2']
#######################
通过导入模块调用方法
>>> import string >>> help(string.replace) >>> s 'hello word' >>> string.replace(s,'hello','good') 'good word'
#####################################
序列处理函数
len()
max()
min()
#使用序列函数求数字的长度是无法使用的!!!!!
>>> len(123) Traceback (most recent call last):File "<stdin>", line 1, in <module> TypeError: object of type 'int' has no len() >>>
filter (参数1:函数,参数2:序列)
Help on built-in function filter in module __builtin__:
filter(...)
filter(function or None, sequence) -> list, tuple, or string
#函数 或者 #空 序列
Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.
范例:
>>> def f(x): ... if x>5: ... return True ... >>> f(10) True >>> f(3) >>> l=range(10) >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> filter(f,l) [6, 7, 8, 9] # f 函数作用于列表l
zip、map()列表遍历
>>> name=['milo','zou','tom'] >>> age=[20,30,40] >>> tel=['133','144','155'] >>> zip(name,age,tel) [('milo', 20, '133'), ('zou', 30, '144'), ('tom', 40, '155')] #并行遍历 >>> map(None,name,age,tel) [('milo', 20, '133'), ('zou', 30, '144'), ('tom', 40, '155')] >>> test=['a','b'] #如果其中一个列表的值少于其它列表,它会取最短的列表进行遍历; >>> zip(name,age,tel,test) [('milo', 20, '133', 'a'), ('zou', 30, '144', 'b')] 如果换为map遍历,它会将某一列表缺少的值用None填充。 >>> map(None,name,age,tel,test) [('milo', 20, '133', 'a'), ('zou', 30, '144', 'b'), ('tom', 40, '155', None)] #此处的None也可以变为函数,把后面的值进行运算。 >>> a=[1,4,6,] >>> b=[7,8,9] >>> map(None,a,b) [(1, 7), (4, 8), (6, 9)] >>> def mf(x,y): ... print x*y ... >>> map(mf,a,b) 7 32 54 [None, None, None]
reduce()递归 运算
>>> def f(x,y): ... return x+y ... >>> l=(range(1,101)) >>> reduce(f,l) 5050 或者使用匿名函数 >>> reduce(lambda x,y:x+y,l) 5050
转载于:https://blog.51cto.com/linuxboys/1657075
python 基础 学习 内置函数相关推荐
- 十五. Python基础(15)--内置函数-1
十五. Python基础(15)--内置函数-1 1 ● eval(), exec(), compile() 执行字符串数据类型的python代码 检测#import os 'import' in c ...
- 【Python基础】内置函数filter详解
filter,顾名思义,就是一个过滤器.其作用是从列表(或其他序列类型)中筛选出满足条件的子列表,filter是python的内置函数,无须import即可直接使用. 1 filter的基础用法 对于 ...
- python基础学习1-内置函数
#!/usr/bin/env python # -*- coding:utf-8 -*- 系统内置函数n =abs(-1) #绝对值 print(n)#bytes()函数 s="离开&quo ...
- Kotlin基础学习 --- 内置函数apply、let
apply内置函数 fun main(){val buffer = "i am buffer "//常规方式println("buffer的字符长度是:${buffer. ...
- python中的内置函数怎么学_python内部函数学习(九)
python提供了很多的内置函数,这些内置的函数在某些情况下,可以起到很大的作用,而不需要专门去 写函数实现XX功能,直接使用内置函数就可以实现,下面分别来学习内置函数的使用和案例代码. 1.abs( ...
- python之路——内置函数和匿名函数
楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们继续谈下一话题... 来你们在自己的环境里打印 ...
- python 两个内置函数——locals 和globals(名字空间)批量以自定义变量名创建对象
文章目录 locals 和globals(名字空间)简介 1.局部变量函数locals例子(locals 返回一个名字/值对的字典) 批量创建对象 示例1 示例2 函数内 类内 2.全局变量函数glo ...
- python提供的内置函数有哪些_python内置函数介绍
内置函数,一般都是因为使用频率比较频繁,所以通过内置函数的形式提供出来.对内置函数通过分类分析,基本的数据操作有数学运算.逻辑操作.集合操作.字符串操作等. 说起我正式了解内置函数之前,接触到的是la ...
- python中如何调用函数_如何调用python中的内置函数?(实例解析)
对于第一次接触到python这门编程语言的朋友来说,刚刚开始学习python编程的时候对于python函数调用这一方面的了解比较少,在这篇文章之中我们就来了解一下python怎么调用函数. Pytho ...
最新文章
- SVD理论以及Python实现
- 天津盈克斯机器人科技_柔性视觉选料 机器人摆盘 柔性振动盘
- Google Analytics Advanced Configuration - Google Analytics 高级配置
- python bokeh 示例_Python bokeh.plotting.figure.arc()用法及代码示例
- ThreadLocal原理机制
- finalize作用
- gateway网关_使用Sentinel实现gateway网关及服务接口限流
- 从 0 到 70%:Chrome 上位揭秘!
- 变量可以存储在堆中,栈中,方法区中。哪里都可以啊。对象只能存储在堆中...
- Linux下搭建Haproxy负载均衡
- unity3d工程Plugin文件夹笔记
- 图解Navicat连接、操作数据库
- 基于 Retina-GAN 的视网膜图像血管分割
- 在JavaScript中改变鼠标指针样式的方法
- “发烧请假”是面照妖镜,聊聊我以前遇到的奇葩领导
- 学习的 定义是什么?生物
- 电商大数据应用之用户画像
- 中国最大在线保健品供应商“健康中国”停业
- nvme协议阅读笔记
- Keras LSTM实现多维输入输出时序预测实践详解
热门文章
- TensorFlow Lite发布重大更新!支持移动GPU、推断速度提升4-6倍
- 手机App都在偷听我说话?窃听疑云全球密布,科技公司连连喊冤
- 阿里芯片大动作!NPU明年6月发布,“平头哥”研发量子芯片
- 从今天起,TensorFlow 1.9开始支持树莓派了
- Facebook又开两处AI实验室,在西雅图和匹兹堡招兵买马
- 业界分享 | 数据科学家工作融入及面试技巧
- python中列表操作
- IIS6.0服务器架站无法访问解决方案总结
- VC++动态链接库(DLL)编程(二)--非MFC DLL
- 1031 质量环(深层搜索演习)