Getting started with python (8) catching exceptions and built-in parameters

Syntax for catching exceptions

try:
运行代码
except(名称)
出现异常的运行代码
else
没有出现异常的运行代码
raise
引发一个异常
finally
不论有没有异常都运行

example:

try:
    2/0
except Exception as e:(商量的语法)
    print(“0不能当作除数”)
else:
    print(“可以正常运行”)
finally
     print(“这是一个除法运算”)

Running result:
0 cannot be used as a divisor
This is a division operation
Example 2:

try:
    2/1
except Exception as e:(商量的语法)
    print(“0不能当作除数”)
else:
    print(“可以正常运行”)
finally
 print(“这是一个除法运算”)

Running result:
can run normally
This is a division operation

There are many built-in functions in python, such as the time function time
, when we load the time function, we need to introduce the time module. These modules are all python functions written by others. We can directly refer to
import time
and use the strftime method of the time module to format the date.

Time and date formatting symbols in python:

%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身

For example:
import time
time.strftime("%Y/%m/%d_%H:%M:%S")
output:
2018/04/23_11:11:23
time.strftime("%a %b %d % H:%M:%S %Y")
output:
Mon Apr 23 11:11:33 2018
There are many built-in functions of time:
time.time()
seconds (float) from 1970 to the present
time.localtime ()
Returns the current timestamp as a tuple, a floating-point number from 1970 to the present
time.gmtime same as
above
time.asctime
same as time.strftime
time.ctime same as
above

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324732917&siteId=291194637