?:显示该类型是什么(若是函数则显示函数路径)
#in
b = [1,2,3]
b?
#out
Type: list
String form: [1, 2, 3]
Length: 3
Docstring:
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list.
The argument must be an iterable if specified.
??:如果是函数,显示该函数的函数体
#in
def add_numbers(a,b):
return a+b
add_numbers??
#out
Signature: add_numbers(a, b)
Docstring: <no docstring>
Source:
def add_numbers(a,b):
return a+b
File: #马赛克
Type: function
str.format:字符串格式化
字符串中使用{index}提取format中的元素
'a is {0},b is {1}'.format(type(a),type(b))
#字符串中使用{index}提取format中的元素
'{0} {1:s} hello world {2:.2f} {3:d}'.format('SQL','a',3.141592,1)
#out:'SQL a hello world 3.14 1'
字符串可以被认为是一个字符的列表或元组:
#in
a="""
hello world
hello python
SQL hello
"""
a[:10]
#out:
'\nhello wor'
#in
a.count("\n")
#out:
4
#in
list(a)
#out:
['\n','h','e','l','l','o',' ','w','o','r','l','d','\n','h','e','l','l','o',' ','p','y','t','h','o','n',
'\n','S','Q','L',' ','h','e','l','l','o','\n']
日期和时间:datetime
from datetime import datetime,date,time
实例化日期对象/提取内容
dt = datetime(2023,4,11,17,49,0)
print(dt.month,dt.date(),dt.time(),dt)
将datetime类型转为字符串
dt.strftime('%Y-%m-%d %H:%M:%S')
dt.strftime('%F') # %Y-%m-%d = %F
将字符串转为datetime
datetime.strptime('20000430','%Y%m%d')
datetime是不可变类型,更改元素会产生新对象
dt2 = dt.replace(minute = 0)
print(dt,dt2)
datetime类型相减,得到timedelta类型,包含天数和秒数两个参数
dt3 = datetime(2023,4,10,12,0,0)
delta = dt - dt3
delta
反过来,datetime类型减timedelta类型也可以回到datetime类型
dt-delta