十五. Python基础(15)--内置函数-1
十五. Python基础(15)--内置函数-1
1 ● eval(), exec(), compile()
|
执行字符串数据类型的python代码 ① eval : 有返回值, 适用于执行计算语句, 例如eval("4+3"). ② exec : 没有返回值, 适用于执行流程控制语句, 例如exec(a = b if b>c else c) ③ complie: code1 = 'for i in range(0,3): print(i)' compile1 = compile(code1, '', 'exec') # 中间表示filename的参数即使没有也要有一个空字符串(其实也可以胡乱写一个字符串) print(compile1) # <code object <module> at 0x0000000002802270, file "", line 1> exec(compile1) exec(compile1) ''' 0 1 2 0 1 2 ''' |
|
compile(str ,filename ,kind ) compile()将一个字符串编译为字节代码, str是将要被编译的字符串, filename是定义该字符串变量的文件, kind参数指定了代码被编译的类型: eval, single, exec 'eval'指一个表达式. 'single'指单个语句, 'exec'指多个语句, 返回一个代码对象,该对象也可以被传递给eval()函数和exec语句来执行
预编译, 可以有效提高程序的执行效率 |
|
eval(), exec()和compile()不要随便使用, 若使用, 也要做好充分的测试. |
2 ● print()函数扩展
|
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) |
|
print("枯藤", "老树", "昏鸦", sep = ",", end = "←") # 枯藤,老树,昏鸦← # sep默认为一个空格" ", end默认为一个换行符 |
|
import time for i in range(0,101,10): time.sleep(1) # 便于观察 char_num = i//2 #打印多少个'*' #per_str = '\r%d%% : %s\n' % (i, '*' * char_num) if i == 100 else '\r%d%% : %s'%(i,'*'*char_num) # 等价于: #per_str = '\r%d%% : %s' % (i, '*' * char_num) if i != 100 else '\r%d%% : %s\n' % (i, '*' * char_num) # 或者是: per_str = '\r%d%% : %s' % (i, '*' * char_num) # 但不能是: print(per_str,end='', flush=True)
# 100% : ************************************************** # 每次循环都是从头开始打印在控制台上的 |
|
f = open('print_test', 'a', encoding = 'utf-8') print("This is a log!", file = f) # 用print写日志文件. |
3 ● 有关换行(line feed), 回车(carriage)
|
\r--CR(carriage return, 回车)--把光标移到所在行开头 \n--LF(line feed, 换行)--把光标移到下一行开头。 \r\n—CR+LF—回车+换行 |
4 ● 有关内置函数hash()
|
url = 'https://www.baidu.com/' from urllib.request import urlopen content = urlopen(url).read() dic = {hash(url):content} for key in dic: print(key) print(hash(url)) ''' 4122514777272343416 4122514777272343416 # 在运行一次, 得到: 8389699687043686450 8389699687043686450 ''' # ※在python的一次执行中, 对于相同的可hash的对象, hash()函数返回的都是相同的数字(因为对象在一个生命周期内) |
5 ● filter()函数和map()函数
|
# filter()函数, 类似于列表推导式 def func1(n): if n % 2 == 0: return True li = [3,2,6,9,8]
print(filter(func1, li)) # filter()方法返回一个迭代器 print(list(filter(func1, li)))
''' <filter object at 0x00000000025E99B0> [2, 6, 8] ''' |
|
def func2(n): if n % 2 == 0: return n li2 = [3,2,6,9,8]
print(list(map(func2, li2))) ''' [None, 2, 6, None, 8] '''
def func3(n): return n**2
print(list(map(func3, li2))) ''' [9, 4, 36, 81, 64] ''' |
|
典型案例: 删除 None 或者空字符串 |
|
# def deter(m): # if m and len(m.strip()) > 0: # 注意m必须写在len(m.strip()) > 0之前, 否则会被警示None无法计算长度 # return True
# 等价于: def deter(m): return m and len(m.strip())
lst = ['test', None, '', 'str', ' ', 'END']
print(list(filter(deter, lst))) ''' ['test', 'str', 'END'] |
6 ● 内置函数__import__()
|
__import__('a') # 导入 a.py 模块 |
十五. Python基础(15)--内置函数-1的更多相关文章
- 十六. Python基础(16)--内置函数-2
十六. Python基础(16)--内置函数-2 1 ● 内置函数format() Convert a value to a "formatted" representation. ...
- python基础(15):内置函数(一)
1. 内置函数 什么是内置函数? 就是python给你提供的,拿来直接⽤的函数,比如print,input等等,截⽌到python版本3.6.2 python⼀共提供了68个内置函数.他们就是pyth ...
- python基础(内置函数+文件操作+lambda)
一.内置函数 注:查看详细猛击这里 常用内置函数代码说明: # abs绝对值 # i = abs(-123) # print(i) #返回123,绝对值 # #all,循环参数,如果每个元素为真,那么 ...
- 第六篇:python基础_6 内置函数与常用模块(一)
本篇内容 内置函数 匿名函数 re模块 time模块 random模块 os模块 sys模块 json与pickle模块 shelve模块 一. 内置函数 1.定义 内置函数又被称为工厂函数. 2.常 ...
- Python基础:内置函数
本文基于Python 3.6.5的标准库文档编写,罗列了英文文档中介绍的所有内建函数,并对其用法进行了简要介绍. 下图来自Python官网:展示了所有的内置函数,共计68个(14*4+12),大家可以 ...
- Python基础编程 内置函数
内置函数 内置函数(一定记住并且精通) print()屏幕输出 int():pass str():pass bool():pass set(): pass list() 将一个可迭代对象转换成列表 t ...
- 学习PYTHON之路, DAY 4 - PYTHON 基础 4 (内置函数)
注:查看详细请看https://docs.python.org/3/library/functions.html#next 一 all(), any() False: 0, Noe, '', [], ...
- Python基础_内置函数
Built-in Functions abs() delattr() hash() memoryview() set() all() dict() help() min() setat ...
- python基础(16):内置函数(二)
1. lamda匿名函数 为了解决⼀些简单的需求⽽设计的⼀句话函数 # 计算n的n次⽅ def func(n): return n**n print(func(10)) f = lambda n: n ...
随机推荐
- 安装edusoho
1.更新第三方源并升级系统 (CentOS默认的标准源里没有nginx软件包) 1.1.安装CentOS第三方yum源 #安装下载工具wget yum install wget #下载atomic y ...
- makefile 里的 := , = , +=
:= 是在这行代码的时候,直接展开右边的变量. = 是在最终左边变量被使用的时候,才把右边的变量展开. https://stackoverflow.com/questions/10227598/wha ...
- 开机出现grub界面(待尝试)
开机出现grub界面 试一下这个命令: grub> rootnoverify (hd0,0) grub> chainloader +1 grub> boot 这样就可以进入到wind ...
- TStringList 常用方法与属性
/TStringList 常用方法与属性 :varList: TStringList;i: Integer;begin List := TStringList.Create;List.Add('Str ...
- POJ-2955 Brackets(括号匹配问题)
题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...
- pytorch backward问题
pytorch中关于backward的很有意思的一个问题 <https://blog.csdn.net/shiheyingzhe/article/details/83054238> 但是我 ...
- 小程序分享转发功能实现demo
/** * 用户点击右上角分享 */ onShareAppMessage: function() { //分享 console.log("分享") var that = this ...
- 『计算机视觉』Mask-RCNN_推断网络终篇:使用detect方法进行推断
一.detect和build 前面多节中我们花了大量笔墨介绍build方法的inference分支,这节我们看看它是如何被调用的. 在dimo.ipynb中,涉及model的操作我们简单进行一下汇总, ...
- web中集成shiro
Shiro提供了与Web集成的支持,其通过一个ShiroFilter入口来拦截需要安全控制的URL,然后进行相应的控制,ShiroFilter类似于如Strut2/SpringMVC这种web框架的前 ...
- JQuery Tree插件
转载这个,这个非常的全,有时间可以去学习学习:http://ztreeapi.iteye.com/ http://ztreeapi.iteye.com/blog/2028608