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. Spoken English Practice(Don't get me wrong, that explanation makes no difference, I'm still mad at you. Come on, be reasonable!)

    绿色:连读:                  红色:略读:               蓝色:浊化:               橙色:弱读     下划线_为浊化 口语蜕变(2017/7/11) ...

  2. rrdtool ubuntu python snmpwalk

    rrdtool install: apt-get install libpango1.0-dev libxml2-dev wget https://packages.debian.org/wheezy ...

  3. python的@classmethod和@staticmethod

    本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象 ...

  4. 前端开发 - Bootstrap

    一.bootstrap简介 插件 == js 在js 的基础上写了一些功能 一个插件就是一个功能/方法组件 = js + css + html 组件包含着插件 官网: http://www.bootc ...

  5. 一条SQL引起的雪崩

    1.问题描述 MySQL服务器卡死,CPU飚到300%多,命令执行缓慢. 2.问题定位 踩了狗屎运,直接找到了问题缘由 发现了一条SQL写的模糊匹配,将%写在了关键字的前面,这样会造成查询不使用索引, ...

  6. java.util.Calendar

    package day14; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import java.util.Cal ...

  7. pytorch中的cat、stack、tranpose、permute、unsqeeze

    Cat 对数据沿着某一维度进行拼接.cat后数据的总维数不变. 比如下面代码对两个2维tensor(分别为2*3,1*3)进行拼接,拼接完后变为3*3还是2维的tensor. import torch ...

  8. HTML5开源RPG游戏引擎lufylegendRPG 1.0.0发布

    经历了几个月的改进,终于发布1.0.0版了.虽然引擎依然存在漏洞,但是比起上次更新还是要好多了.在这里不得不感谢各位网友的大力支持. 首先为引擎做一个开场白吧,也好让大家了解一下它: lufylege ...

  9. C的指针疑惑:C和指针10(结构和联合)

    结构也可以作为传递给函数的参数,它们也可以作为返回值从函数返回,相同类型的结构体变量相互之间可以赋值. 申明结构时使用另一种良好技巧是用typedef创建一种新的类型. typedef struct{ ...

  10. HALCON串口通讯程序

    串口通讯程序   * Note: This example is meant to demonstrate the use of the serial interface * of HALCON.   ...