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 ...
随机推荐
- oracle常用操作命令总结
一. 默认安装带来的用户名/密码:--sys/change_on_install SYSDBA 或 SYSOPER 不能以 NORMAL 登录,可作为默认的系统管理员 --system/安装时输入的密 ...
- rabbitmq延迟队列相关
https://blog.csdn.net/qq_26656329/article/details/77891793 --------------rabbitmq queue_decla ...
- python中的itertools
在量化数据处理中,经常使用itertools来完成数据的各种排列组合以寻找最优参数 import itertools #1. permutations: 考虑顺序组合元素 items = [1, 2, ...
- JS之for...in和for...of
for...in输入键: for...in循环有几个缺点. 数组的键名是数字,但是for...in循环是以字符串作为键名“0”.“1”.“2”等等. for...in循环不仅遍历数字键名,还会遍历手动 ...
- 由link和@import的区别引发的CSS渲染杂谈
我们都知道,外部引入 CSS 有2种方式,link标签和@import. 它们有何本质区别,有何使用建议,在考察外部引入 CSS 这部分内容时,经常被提起. 如今,很多学者本着知其然不欲知其所以然的学 ...
- 解决scrollView中嵌套编辑框导致不能上下滑动的问题
EditText设置maxLines之后,文本行数超过maxLines,会网上折叠,上下滑动能够浏览全部文本. 若EditText外层有scrollView.在EditText上下滑动,不会像正常情况 ...
- 【开发者笔记】插入排序过程呈现之java内置GUI表示
先给代码,再给过程视频: package com.dyi.wyb.sort; import java.awt.Color; import java.awt.Graphics; import java. ...
- Oracle获取当前session ID的方法
1.使用v$mystat视图获取当前session的ID select sid from v$mystat; 2.使用userenv内部函数获取当前session的ID select userenv( ...
- (转)MFC中Doc,View,MainFrmae,App各指针的互相获取
App是应用域,所有的域中的东西都可以通过全局函数访问到它. MainFrame是主框架,也基本可以用全局函数访问到. MainFrame下是若干个ChildFrame,ChildFrame中若干个V ...
- PHPCMS 小节
当前栏目id: {$catid}当前栏目名: {$CATEGORYS[$catid][catname]}当前栏目 ...