python-内置函数及捕获异常
eval:把字符串转换成有效表达式
repr:把有效表达式转换成字符串
round(...)
round(number[, ndigits]) -> number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
pow(x, y, z=None, /)
Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
class map(object)
| map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from
| each of the iterables. Stops when the shortest iterable is exhausted.
>>> a
[1, 2, 3]
>>> b
['a', 'b', 'c']
>>> dict(zip(a,b))
{1: 'a', 2: 'b', 3: 'c'}
>>> mylist.extend([1,2,3])
>>> mylist
[2, 1, 3, 2, 1, 4, 3, 2, 1, 'a', 'a', 1, 2, 3]
>>> mylist.append([1,2,3])
>>> mylist
[2, 1, 3, 2, 1, 4, 3, 2, 1, 'a', 'a', 1, 2, 3, [1, 2, 3]]
-------------------------------------------以上是内置函数------------------------------------------
异常,错误
程序没法处理的任务,会抛出异常
错误一般是,逻辑错误,语法错误,无法生成结果等
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> 1 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> if a = b
File "<stdin>", line 1
if a = b
^
SyntaxError: invalid syntax
>>> b[4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
在python中,大部分异常都是基于Exception 这个类
KeyboardInterrupt Exception exit
基类
Exception
python可以通过
try语句检测异常:
要检测异常的语句
except 语句来处理异常:
>>> try:
... print(a)
... except NameError:
... print('ABC')
...
ABC
>>> try:
... print(a)
... except Exception as error:
... print(error)
...
name 'a' is not defined
try:
要检测异常的语句
except 检测的异常(如果我们使用Execption)则是捕捉所有异常:#但是不建议大家这么做,因为有些异常是我们需要去真实看到的,
执行捕获异常之后的事情
else:
用来在没有任何异常情况下执行这里的内容
finally:
不管异常是否发生,这里面写的语句都会执行
我们一般用来关闭socket,关闭文件,回收线程,进程创建释放,数据库连接,mysql句柄,做一些对象内存释放的工作,
>>> try:
... print(a)
... except Exception:
... print('abc')
... else:
... print('--------')
... finally:
... print('********')
...
abc
********
raise 可以抛出异常
断言:
assert当判断的语句为false 那么会抛出一个AssertionError 异常
断言一般用来判断一些bool语句,在断言成功时不采取任何措施,否则触发AssertionError(断言错误)的异常,
try:
assert 1 == 0
except AssertionError:
print('not equal')
with语句,with语句其实和try,finally语句是一样的,只不过,他只对支持上下文管理协议(context
management protocol),比如我们的mysql链接数据库,会在完成业务之后他有关闭,打开文件也会有关闭文件,
close()
我们通过with语句打开一个文件对象,如果没有出错,文件对象就是file,
之后我们做的一系列操作不论是否会发生错误,抛出异常,都会执行内存清理的语句,刷新缓冲区,关闭文件等
-----------------------------------------------异常------------------------------------------------
python-内置函数及捕获异常的更多相关文章
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- Python基础篇【第2篇】: Python内置函数(一)
Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...
- [python基础知识]python内置函数map/reduce/filter
python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...
- Python内置函数进制转换的用法
使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...
- Python内置函数(12)——str
英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...
- Python内置函数(61)——str
英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...
- 那些年,很多人没看懂的Python内置函数
Python之所以特别的简单就是因为有很多的内置函数是在你的程序"运行之前"就已经帮你运行好了,所以,可以用这个的特性简化很多的步骤.这也是让Python语言变得特别的简单的原因之 ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】实习小记-python 内置函数__eq__函数引发的探索
[转]实习小记-python 内置函数__eq__函数引发的探索 乱写__eq__会发生啥?请看代码.. >>> class A: ... def __eq__(self, othe ...
随机推荐
- Struts2中的类型转换失败
类型转换失败: 若 Action 类没有实现 ValidationAware 接口: Struts 在遇到类型转换错误时仍会继续调用其 Action 方法, 就好像什么都没发生一样. 若 Action ...
- SaltStack自动化安装zabbix-server
使用SaltStack自动化安装zabbix-server 1,设置ntp时间同步 2,安装zabbix-agent 3,安装zabbix-server 4,安装及配置mariadb(mariadb与 ...
- 2017 Multi-University Training Contest - Team 1—HDU6035
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:一棵树有n个点,每个点有自己的颜色,任意两个不同的点可以组成一条路径.也就是说一共有n(n ...
- flask中的blueprint
https://blog.csdn.net/sunhuaqiang1/article/details/72803336
- python基础-第十篇-10.1HTML基础
htyper text markup language 即超文本标记语言 超文本:就是指页面内可以包含图片.链接,甚至音乐,程序等非文字元素 标记语言:标记(标签)构成的语言 网页==HTML文档,由 ...
- wait_event族函数浅析
2017-06-03 周末闲暇无事,聊聊内核中的wait_event*类函数的具体实现,等待事件必定涉及到某个条件,而这些函数的区别主要是等待后唤醒的方式……直奔主题,上源码 wait_event_i ...
- MySql库、表权限管理
#授权表user #该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段db #该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段tables_priv #该表放行 ...
- abap 中modify 的使用
1.modify table itab from wa Transporting f1 f2 ... 表示表itab中符合工作区wa 中关键字的一条数据的 f1 f2字段会被wa中对应的字段值更新. ...
- UVA+POJ中大数实现的题目,持续更新(JAVA实现)
UVA10494:If We Were a Child Again 大数除法加取余 import java.util.Arrays; import java.util.Scanner; import ...
- 创建Java不可变型的枚举类型Gender
创建Java不可变型的枚举类型,其实例如下: // 创建不可变型的枚举类 enum Gender { // 此处的枚举值必须调用对应的构造器来创建 MALE("男"), FEMAL ...