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语句其实和tryfinally语句是一样的,只不过,他只对支持上下文管理协议context

management protocol,比如我们的mysql链接数据库,会在完成业务之后他有关闭,打开文件也会有关闭文件,

close()

我们通过with语句打开一个文件对象,如果没有出错,文件对象就是file

之后我们做的一系列操作不论是否会发生错误,抛出异常,都会执行内存清理的语句,刷新缓冲区,关闭文件等

-----------------------------------------------异常------------------------------------------------

python-内置函数及捕获异常的更多相关文章

  1. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  2. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

  3. Python基础篇【第2篇】: Python内置函数(一)

    Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...

  4. [python基础知识]python内置函数map/reduce/filter

    python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...

  5. Python内置函数进制转换的用法

    使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...

  6. Python内置函数(12)——str

    英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string  ...

  7. Python内置函数(61)——str

    英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...

  8. 那些年,很多人没看懂的Python内置函数

    Python之所以特别的简单就是因为有很多的内置函数是在你的程序"运行之前"就已经帮你运行好了,所以,可以用这个的特性简化很多的步骤.这也是让Python语言变得特别的简单的原因之 ...

  9. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  10. 【转】实习小记-python 内置函数__eq__函数引发的探索

    [转]实习小记-python 内置函数__eq__函数引发的探索 乱写__eq__会发生啥?请看代码.. >>> class A: ... def __eq__(self, othe ...

随机推荐

  1. Hibernate与数据库的触发器协同工作

    Hibernate 与数据库中的触发器协同工作时, 会造成两类问题 1触发器使 Session 的缓存中的持久化对象与数据库中对应的数据不一致:触发器运行在数据库中, 它执行的操作对 Session ...

  2. session------>防表单重复提交

    方法一:用js控制表单提交--->但是容易在客户端被篡改代码,还是要加的 方法二:session 先给每一个表带上唯一的标志,再把标志存入session 当session中标志和表上标志都不为空 ...

  3. Yii框架2.0的控制器

    控制器是继承[[yii\base\Controller]]类的对象,负责处理请求和生成响应. 具体来说,控制器从应用主体接管控制后会分析请求数据并传送到模型, 传送模型结果到视图,最后生成输出响应信息 ...

  4. Qt JSON解析生成笔记(把JSON转成一个类对象)

    对于这样一段json { "name": "布衣食", "gender": "Male", "age" ...

  5. apt-get 报 The following signatures were invalid: KEYEXPIRED 错误

    apt-get 原理: 参考:https://blog.csdn.net/a13526758473/article/details/79247478 apt对它所管理的每一个程序包都有一对公钥和私钥, ...

  6. Period---hdu1358(循环节 kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题意 :求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k(k>1); 例如: ...

  7. Unmet dependencies. Try 'apt-get -f install' with no packages

    在ubuntu14.04上用sudo apt-get install percona-xtrabackup安装xtrabackup时提示 zhj@my-SERVER:~$ sudo apt-get i ...

  8. Spring-ApplicationContext容器

    Spring ApplicationContext容器 ApplicationContext是spring中比较高级的容器.和BeanFactory类似,它可以加载配置文件中定义的bean,并将所有b ...

  9. 【市场调研与分析】Intel发力移动安全领域——By Me at 20140613

                                                    [市场调研与分析]Intel发力移动安全领域                               ...

  10. centos 配置yum源

    1.yum配置 yum的配置文件在  /etc/yum.conf [root@mini ~]# cat /etc/yum.conf [main] cachedir=/var/cache/yum/$ba ...