十五. 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 ...
随机推荐
- mybatis-generator使用心得
通过web service给前端返回数据 首先后台先建表, 再针对表进行CRUD的各种sql, 然鹅,现在流行做法是使用mybatis,直接xml把sql融合了,什么事都有利弊,像我这样的手写sql党 ...
- Bulk RNA-Seq转录组学习
与之对应的是single cell RNA-Seq,后面也会有类似文章. 参考:https://github.com/xuzhougeng/Learn-Bioinformatics/ 作业:RNA-s ...
- MapReduce处理气象数据
老师:MissDu 提交作业 1. 用Python编写WordCount程序并提交任务 程序 WordCount 输入 一个包含大量单词的文本文件 输出 文件中每个单词及其出现次数(频数),并按照单 ...
- 三种css样式表及其优先级
1.行内样式 body内: <p style="text-indent: 2em;color: red"> 我是行内样式 </p> 2.内部样式表 body ...
- CF1010F Tree
真·毒瘤题 这个题面写错了一句话.要求的是每个节点的石子树>=它的两个儿子石子数的和. 首先考虑怎么算石子分配的方案. 如果对这棵树每个节点的石子数都和儿子差分一下的话,可以唯一对应一颗每个点都 ...
- First Bad Version leetcode java
问题描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- 『Python』PIL图像处理_形变操作
使用PIL.Image进行简单的图像处理 # coding=utf-8 from PIL import Image import matplotlib.pyplot as plt def show_i ...
- yarn基本命令
参考文章:https://blog.csdn.net/mjzhang1993/article/details/70092902 1.安装 windows: 下载地址 mac: brew install ...
- ubuntu chmod命令的使用
我推荐的地址:http://blog.163.com/bluesky_07_06_1/blog/static/164440083201161451735773/ 这个非常的牛逼.
- 转-如何使用iTunes制作iPhone铃声
新版iTunes(iTunes11)推出以后,界面上发生了一些改变,给人带来一种面貌一新的感觉,但也给许多朋友带来一些操作上的不太适应.下面就大家比较关心的iPhone的铃声制作方法,我在iTunes ...