对列表内的元素进行排序。
在列表中最后追加
统计列表中的元素个数
复制某个列表中的元素
删除列表的中元素
情况一个列表中的所有元素
移除列表中的某个元素(不加下标默认删除最后一个)
返回列表中某个元素的下标
拼接某个列表到现在的列表
将列表中的元素反序排列
往列表里面插入某个元素
如何在字典中插入一个item?
x = dict(a = 1, b = 2)
y = dict(c = 3, d = 4)
x.update(dict(m = 100))
x["n"] = 200
print(x)
import codecs
with codecs.open("aa", "w") as f:
f.XXX
即便Python程序的语法是正确的,在运行它的时候,也有可能发生错误。运行期检测到的错误被称为异常。
当 Python 脚本会引发一个异常,它必须要么处理异常,要么终止并退出。
如果你有一些可疑的代码,可能会引发异常, 可以通过将可疑代码放在一个
try: 块来保护你的程序。在 try:块之后,包括
except: 语句随后的代码块,作为优雅的处理异常问题。
下面是 try….except…else 块的简单的语法 −
try:
You do your operations here
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
以下是有关上述语法几个重要的地方 – 单个 try 语句可以有多个except语句。 当 try 块包含可能抛出不同类型的异常声明这是有用的
也可以提供一个通用的 except 子句来处理任何异常 except子句后,可以包括 else 子句。 如果代码在try:块不引发异常则代码在 else 块执行
else 块是代码的好地方,这不需要 try: 块的保护
这个例子打开一个文件,并写入内容,文件处理完全没有问题 –
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 4/23/2018 1:26 PM
# @Author : zhdya
# @File : demon1.py
import codecs
try:
with codecs.open("aa","w") as f:
f.writelines("he\nshe\nher\nhim\nhis")
except Exception as e:
print("you can't edit this file, pls checking it..")
finally:
print("already write the content to aa.file!!")
输出:
already write the content to aa.file!!
try:
print(2/0)
except Exception as e:
print("分母不能为0")
finally:
print("不管上面对错,都会执行我!!")
输出:
分母不能为0
不管上面对错,都会执行我!!
假如只给r权限:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 4/23/2018 1:26 PM
# @Author : zhdya
# @File : demon1.py
import codecs
try:
with codecs.open("aa","r") as f:
f.writelines("he\nshe\nher\nhim\nhis")
except Exception as e:
print("you can't edit this file, pls checking it..")
else:
print("already write the content to aa.file!!")
输出:
you can't edit this file, pls checking it..
try:
print(2/0)
except Exception as e:
print("分母不能为0")
raise e
finally:
print("不管上面对错,都会执行我!!")
输出:
分母不能为0
Traceback (most recent call last):
不管上面对错,都会执行我!!
File "D:/python/Python Items/Python_Test/9/demon1.py", line 21, in <module>
raise e
File "D:/python/Python Items/Python_Test/9/demon1.py", line 18, in <module>
print(2 / 0)
ZeroDivisionError: division by zero
import datetime
def test():
result = dict()
try:
print(2 / 0)
except Exception as e:
result["msg"] = "分母不可以为0"
result["data"] = datetime.datetime.now()
result["error code"] = 555
finally:
print(result)
if __name__ == '__main__':
test()
输出:
{'msg': '分母不可以为0', 'data': datetime.datetime(2018, 4, 23, 17, 55, 12, 214705), 'error code': 555}
(未来工作中经常用到的一种方式):
result = dict()
result["msg"] = "分母不可以为0"
result["data"] = datetime.datetime.now()
result["error code"] = 555
使用pycharm 创建一个module:
module 和目录的区别,看是否有 init.py 文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 4/23/2018 8:58 PM
# @Author : zhdya
# @File : demon2.py
from datetime import datetime
print(datetime.now())
输出:
2018-04-23 22:20:10.882324
from datetime import datetime
print(datetime.now())
print(datetime.now().year)
print(datetime.now().month)
print(datetime.now().day)
print(datetime.now().hour)
print(datetime.now().minute)
print(datetime.now().second)
输出:
2018-04-23 22:24:34.883997
2018
4
23
22
24
或:
from datetime import datetime
print(datetime.now())
print(datetime.now().strftime("%Y-%m-%d"))
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 4/23/2018 8:58 PM
# @Author : zhdya
# @File : demon2.py
from datetime import datetime, timedelta
nowTime = datetime.now()
nowTime += timedelta(days= -1)
print(nowTime)
输出:
2018-04-22 22:49:23.848551
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 4/23/2018 8:58 PM
# @Author : zhdya
# @File : demon2.py
from datetime import datetime, timedelta
nowTime = datetime.now()
nowTime += timedelta(weeks= -1)
print(nowTime)
输出:
2018-04-16 22:48:44.151617
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 4/23/2018 8:58 PM
# @Author : zhdya
# @File : demon2.py
from datetime import datetime, timedelta
nowTime = datetime.now()
nowTime += timedelta(hours= +3)
print(nowTime)
格式:
%Y 带世纪部分的十制年份
%m 十进制表示的月份
%d 十进制表示的每月的第几天
%H 24小时制的小时
%M 十时制表示的分钟数
%S 十进制的秒数
%c 标准时间,如:04/25/17 14:35:14 类似于这种形式
%a 本地(locale)简化星期名称
%b 本地简化月份名称
%d 一个月中的第几天(01 - 31)
import time
time.sleep(10) //常用
print("111")
输出:
暂停10秒后,输出111
import time
print(time.time()) //常用
输出:是从1970年01月01日00时00分00秒到现在的秒数