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. Learning by doing

    Learning by doing 绪论:读了娄老师的公众号中--<做中学(Learning By Doing)>这篇文章后,深有感触,我想到很多自己之前的事情,很多都是每每想的很好,总是 ...

  2. 安卓解析json,使用BaseAdapter添加至ListView中,中间存储用JavaBean来实现

    这是一个小练习,要求解析一个提供的json文件.并将其中的id,title值获取,以ListView形式展示出来.(开发工具是android studio) 下面开始: 首先我想到的是先把json文件 ...

  3. 腾讯云>>云通信>>TLS后台API在mac上JAVA DEMO搭建

    1.相关文档地址 2.相关demo代码 代码部分作了修改,使用了commons-io中的IOUtils.toString简化了io操作 public class Demo { public stati ...

  4. Lucene 简单API使用

    本demo 简单模拟实现一个图书搜索功能. 模拟向数据库添加数据的时候,添加书籍索引. 提供搜索接口,支持按照书名,作者,内容进行搜索. 按默认规则排序返回搜索结果. Jar依赖: <prope ...

  5. 此数据库文件与当前sql server实例不兼容

    在vs2015导入mdf数据库文件时提示:此数据库文件与当前sql server实例不兼容. mdf文件的版本是SQL SERVER 2005的,而VS2015自带的数据库是LocalDB,直接导入该 ...

  6. language model —— basic model 语言模型之基础模型

    一.发展 起源:统计语言模型起源于 Ponte 和 Croft 在 1998年的 SIGIR上发表的论文 应用:语言模型的应用很多: corsslingual retrieval distribute ...

  7. [地图SkyLine二次开发]框架(5)完结篇

    上节讲到,将菜单悬浮到地图上面,而且任何操作都不会让地图把菜单盖住. 这节带大家,具体开发一个简单的功能,来了进一步了解,这个框架. 1.想菜单中添加按钮 -上节定义的mainLayout.js文件里 ...

  8. 01 初识python

    python.exe -v / python3 -v安装python3时, 会得到一个 IDLE(提示符>>>), 简单, 有用, 包含语法编辑器(颜色可变), 调试工具, pyth ...

  9. Python_Day11_同步IO和异步IO

    同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network IO. ...

  10. mybatis原理

    http://blog.csdn.net/column/details/mybatis-principle.html?page=1