abs 绝对值

n = abs(-1)
print(n) ========================= /usr/bin/python3.5 /home/liangml/pythonscript/test1.py
1 Process finished with exit code 0

abs 实例

all  所有为真,才为真

any 只要有真,就为真

n = all([1,2,3,4])
print(n)
n = all([0,2,3,4])
print(n)
n = any([[],1,"",None])
print(n)
n = any([[],0,"",None])
print(n) ============================================ /usr/bin/python3.5 /home/liangml/pythonscript/test1.py
True
False
True
False Process finished with exit code 0

all any 实例

bool 值(0,None,"",[]......)

print(bool())

===============================

/usr/bin/python3.5 /home/liangml/pythonscript/test1.py
False Process finished with exit code 0

bool值,实例

ascii 自动执行对象 _repr_ 方式

class Foo:
def __repr__(self):
return ""
n = ascii(Foo())
print(n) ======================================== /usr/bin/python3.5 /home/liangml/pythonscript/test1.py
444 Process finished with exit code 0

ascii 实例

bin() 将10进制转换成2进制

oct() 将10进制转换成8进制

hex() 将10进制转换成16进制

print(bin(5))
print(oct(9))
print(hex(15)) ======================================== /usr/bin/python3.5 /home/liangml/pythonscript/test1.py
0b101
0o11
0xf Process finished with exit code 0

bin oct hex 实例

callable 查看函数是否可以被调用

def f1():
pass
f2 = 123
print(callable(f1))
print(callable(f2)) ===================================== /usr/bin/python3.5 /home/liangml/pythonscript/test1.py
0b101
0o11
0xf Process finished with exit code 0

callable 实例

chr 将数字转换成字母

ord 将字母转换成数字

r = chr(65)
print(r)
n = ord('A')
print(n) ====================================== r = chr(65)
print(r)
n = ord('A')
print(n)

chr ord 实例

引入random 模块,制作随机验证码(字母随机验证码),每次切换都有不同的效果

import random
li = []
for i in range(6):
temp = random.randrange(65,91)
c = chr(temp)
li.append(c)
#li = ["c","b","a"]#cba
result = "".join(li)
print(result) ========================================== /usr/bin/python3.5 /home/liangml/pythonscript/test.py
MFRAID Process finished with exit code 0

随机验证码 1(字母)

import random
li = []
for i in range(6):
if i == 2 or i == 4:
num = random.randrange(0,10)
li.append(str(num))
else:
temp = random.randrange(65,91)
c = chr(temp)
li.append(c)
result = ''.join(li)
print(result) ========================================= /usr/bin/python3.5 /home/liangml/pythonscript/test.py
YY7X4V Process finished with exit code 0

随机验证码 2(有序插入数字)

import random
li = []
for i in range(6):
r = random.randrange(0,5)
if r == 2 or r == 4:
num = random.randrange(0,10)
li.append(str(num))
else:
temp = random.randrange(65,91)
c = chr(temp)
li.append(c)
result = ''.join(li)
print(result) ============================================== /usr/bin/python3.5 /home/liangml/pythonscript/test.py
VF95VR Process finished with exit code 0

随机验证码 3(所有字母及数字都无序)

compile() 将字符串编译成python代码

eval() 执行表达式,并返回值

exec() 执行python代码,接受代码或者是字符串(没有返回值)

s = 'print(123)'
#编译 single,eval,exec
r = compile(s,"<string>","exec")
#执行python代码
exec(r) ============================================= /usr/bin/python3.5 /home/liangml/pythonscript/test.py
123 Process finished with exit code 0

compile exec 配合使用

s = '8*8'
ret = eval(s)
print(ret) ================================================= /usr/bin/python3.5 /home/liangml/pythonscript/test.py
64 Process finished with exit code 0

eval 使用

dir 快速获取一个对象提供了什么功能

help 同dir功能一样,区别在于help可以列出使用详情

print(dir(list))

========================================

/usr/bin/python3.5 /home/liangml/pythonscript/test.py
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] Process finished with exit code 0

dir 实例

help(list)

=================================================

/usr/bin/python3.5 /home/liangml/pythonscript/test.py
Help on class list in module builtins: class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __gt__(self, value, /)
| Return self>value.
|
| __iadd__(self, value, /)
| Implement self+=value.
|
| __imul__(self, value, /)
| Implement self*=value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.n
|
| __ne__(self, value, /)
| Return self!=value.
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __repr__(self, /)
| Return repr(self).
|
| __reversed__(...)
| L.__reversed__() -- return a reverse iterator over the list
|
| __rmul__(self, value, /)
| Return self*value.
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(...)
| L.__sizeof__() -- size of L in memory, in bytes
|
| append(...)
| L.append(object) -> None -- append object to end
|
| clear(...)
| L.clear() -> None -- remove all items from L
|
| copy(...)
| L.copy() -> list -- a shallow copy of L
|
| count(...)
| L.count(value) -> integer -- return number of occurrences of value
|
| extend(...)
| L.extend(iterable) -> None -- extend list by appending elements from the iterable
|
| index(...)
| L.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
|
| insert(...)
| L.insert(index, object) -- insert object before index
|
| pop(...)
| L.pop([index]) -> item -- remove and return item at index (default last).
| Raises IndexError if list is empty or index is out of range.
|
| remove(...)
| L.remove(value) -> None -- remove first occurrence of value.
| Raises ValueError if the value is not present.
|
| reverse(...)
| L.reverse() -- reverse *IN PLACE*
|
| sort(...)
| L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None Process finished with exit code 0

help 实例

divmod 自动求出商  余数

r = divmod(97,10)
print(r)
n1,n2 = divmod(97,10) ============================================= /usr/bin/python3.5 /home/liangml/pythonscript/test.py
(9, 7) Process finished with exit code 0

divmod 实例

isinstance 判断对象是否是类的实例

s = 'liangml'
r = isinstance(s,str)
print(r) ======================================== /usr/bin/python3.5 /home/liangml/pythonscript/test.py
True
<class 'str'> {"k1":"v1"}
<class 'dict'> {'k1': 'v1'} Process finished with exit code 0

isinstance 实例

filter  map(函数可迭代对象)

两者区别
  filter:函数返回True,将元素添加到结果中
  map:将函数返回值添加到结果中
filter 函数讲解
def f1(args):
result = []
for item in args:
if item > 22:
result.append(item)
return result
li = [11,22,33,44,55]
ret = f1(li)
print(ret) =============================================================== /usr/bin/python3.5 /home/liangml/pythonscript/test.py
[33, 44, 55] Process finished with exit code 0
+++++++++++通过lambda表达式也可以实现相同的功能(自动return)+++++++++
f1 = lambda a: a >30
ret =f1(90)
print(ret) li = [11,22,33,44,55]
result = filter(lambda a: a > 33,li)
print(list(result)) =========================运行结果===========================
/usr/bin/python3.5 /home/liangml/pythonscript/test.py
True
[44, 55] Process finished with exit code 0 filter 函数运行原理 def f2(a):
if a>22:
return True
li = [11,22,33,44,55]
#result = []
#for item in 第二个参数(参数)
# r = 第一个参数(item)
# if r:
# result(item)
#return result
#fileter,循环第二个参数,让每个循环元素执行函数,如果函数返回值Ture,表示元素合法
ret = filter(f2,li)
print(list(ret)) map 函数讲解(函数可以使用for循环) ===================for循环实例==========================
li = [11,22,33,44,55] def f1(args):
result = []
for i in args:
result.append(100+i) return result
r = f1(li)
print(r)
====================运行结果=============================
/usr/bin/python3.5 /home/liangml/pythonscript/test.py
[111, 122, 133, 144, 155] Process finished with exit code 0 ====================设定函数运行=========================== def f2(a):
return a + 100
result = map(f2,li)
print(list(result))
++++++++++++++++++++运行结果+++++++++++++++++++++++++++++++ /usr/bin/python3.5 /home/liangml/pythonscript/test.py
[111, 122, 133, 144, 155] Process finished with exit code 0 ===================也可以使用lambda表达式来实现============== result = map(lambda a: a+200,li)
print(list(result)) ++++++++++++++++++++运行结果+++++++++++++++++++++++++++++
/usr/bin/python3.5 /home/liangml/pythonscript/test.py
[211, 222, 233, 244, 255] Process finished with exit code 0

filter map 实例

float 把一个数转换成浮点型

frozenset 集合set set基础上不可变的集合

globals 所有的全局变量/locals 所有的局部变量

NAME = "liangml"
def show():
a = 123
print(locals())
print(globals())
show()

globals locals 实例

hash 将某一个类型的数据转换成hash值

s = 'hhh123123123'
print(hash(s)) =============运行结果===================== /usr/bin/python3.5 /home/liangml/pythonscript/test.py
105733462617069300 Process finished with exit code 0

hash 实例

len 查看某个字符串的长度

s = '梁孟麟'
b = bytes(s,encoding='utf-8')
print(len(s))
print(len(b)) ======================================== /usr/bin/python3.5 /home/liangml/pythonscript/test.py
3
9 Process finished with exit code 0

len 实例

max() 求最大值/min() 求最小值/num() 求和/pow 求次方

q = max(2,10)
w = min(2,10)
e = sum([],10)
r = pow(2,10)
print(q)
print(w)
print(e)
print(r) ======================================== /usr/bin/python3.5 /home/liangml/pythonscript/test.py
10
2
10
1024 Process finished with exit code 0

max/min/sum/pow 实例

round 四舍五入

r = round(1,4)
print(r) ============================================ /usr/bin/python3.5 /home/liangml/pythonscript/test.py
1 Process finished with exit code 0

round 实例

slice() 切片功能

s = 'ssssssssssss'
print(s[0:2]) ================================ /usr/bin/python3.5 /home/liangml/pythonscript/test.py
ss Process finished with exit code 0

slice 实例

sorted 排序

li = [11,2,1,1]
r1 = li.sort()
r2 = sorted(li)
print(r1)
print(r2) =============================== /usr/bin/python3.5 /home/liangml/pythonscript/test.py
None
[1, 1, 2, 11] Process finished with exit code 0

sorted 实例

zip 压缩分类元素

l1 = ['liangml',11,22,33]
l2 = ['is',11,22,33]
l3 = ['nb',11,22,33]
r = zip(l1,l2,l3)
print(list(r)) ============================================
/usr/bin/python3.5 /home/liangml/pythonscript/test.py
[('liangml', 'is', 'nb'), (11, 11, 11), (22, 22, 22), (33, 33, 33)] Process finished with exit code 0
json将字符串转换成Python的基本数据类型,{},[],注意:字符串形式的字典{"k1":"v1"}内部的字符串必须是双引号
s = "[11,22,33,44,55]"
s = '{"k1":"v1"}'
print(type(s),s)
import json
n = json.loads(s)
print(type(n),n) ============================================= /usr/bin/python3.5 /home/liangml/pythonscript/test.py
<class 'str'> {"k1":"v1"}
<class 'dict'> {'k1': 'v1'} Process finished with exit code 0

json 实例

Pythonn 内置函数的更多相关文章

  1. Entity Framework 6 Recipes 2nd Edition(11-12)译 -> 定义内置函数

    11-12. 定义内置函数 问题 想要定义一个在eSQL 和LINQ 查询里使用的内置函数. 解决方案 我们要在数据库中使用IsNull 函数,但是EF没有为eSQL 或LINQ发布这个函数. 假设我 ...

  2. Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数

    dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...

  3. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  4. DAY5 python内置函数+验证码实例

    内置函数 用验证码作为实例 字符串和字节的转换 字符串到字节 字节到字符串

  5. python之常用内置函数

    python内置函数,可以通过python的帮助文档 Build-in Functions,在终端交互下可以通过命令查看 >>> dir("__builtins__&quo ...

  6. freemarker内置函数和用法

    原文链接:http://www.iteye.com/topic/908500 在我们应用Freemarker 过程中,经常会操作例如字符串,数字,集合等,却不清楚Freemrker 有没有类似于Jav ...

  7. set、def、lambda、内置函数、文件操作

    set : 无序,不重复,可以嵌套 .add (添加元素) .update(接收可迭代对象)---等于批量 添加 .diffrents()两个集合不同差 .sysmmetric difference( ...

  8. SQL Server 内置函数、临时对象、流程控制

    SQL Server 内置函数 日期时间函数 --返回当前系统日期时间 select getdate() as [datetime],sysdatetime() as [datetime2] getd ...

  9. Python-Day3知识点——深浅拷贝、函数基本定义、内置函数

    一.深浅拷贝 import copy #浅拷贝 n1={'k1':'wu','k2':123,'k3':['carl',852]} n2=n1 n3=copy.copy(n1) print(id(n1 ...

随机推荐

  1. NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象

    摘要: NSUserDefaults适合存储轻量级的本地数据,一些简单的数据(NSString类型的)例如密码,网址等,NSUserDefaults肯定是首选,但是如果我们自定义了一个对象,对象保存的 ...

  2. IOS苹果和百度地图的相关使用

    iOS中使用较多的3款地图,google地图.百度地图.苹果自带地图(高德).其中苹果自带地图在中国使用的是高德的数据.苹果在iOS 6之后放弃了使用谷歌地图,而改用自家的地图.在国内使用的较多的就是 ...

  3. python操作Excel--使用xlrd

    一.安装xlrd http://pypi.python.org/pypi/xlrd 二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open ...

  4. \r与\n的区别

    \r : return 到当前行的最左边. \n: newline 向下移动一行,并不移动左右. Linux中\n表示回车+换行: Windows中\r\n表示回车+换行. Mac中\r表示回车+换行 ...

  5. Contact项目梳理

    1. 共三张表:user用户表  group分组表 contact联系人表 entity  分模块,三个实体类,三个模块 2. 先注册再登录 DAO:UserDAOImpl public User g ...

  6. ubuntu服务管理

    uRedhat 提供了chkconfig这个命令来管理系统在不同运行级别下的服务开启/关闭: chkconfig ServiceName on/off 并可以用chkconfig --list(两个杠 ...

  7. 个人博客作业Week2

    一.是否需要有代码规范 这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 我反驳这个观点,这些规范是成千上万的程序员在开发程序中总结出来的代码规范,他有助于我们的开 ...

  8. 学习PHP 逛的几个网站。

    PHP 第一社区 http://www.php1.cn/ 51cto php开发 http://developer.51cto.com/col/1441/ phphub https://phphub. ...

  9. SQL Server Reporting Service(SSRS) 第二篇 SSRS数据分组Parent Group

    SQL Server Reporting Service(SSRS) 第一篇 我的第一个SSRS例子默认使用Table进行简单的数据显示,有时为了进行更加直观的数据显示,我们需要按照某个字段对列表进行 ...

  10. GO语言学习

    1. 语言特色 可直接编译成机器码,不依赖其他库,glibc的版本有一定要求,部署就是扔一个文件上去就完成了. 静态类型语言,但是有动态语言的感觉,静态类型的语言就是可以在编译的时候检查出来隐藏的大多 ...