用了这么久的python,这些零碎的基础知识,你还记得多少?

Python内置数据类型

Python3.7内置关键字
['False','None','True','and','as','assert','async','await','break','class','continue','def', 'del','elif','else','except','finally','for','from','global','if','import','in','is','lambda', 'nonlocal','not','or','pass','raise','return','try','while','with','yield']
格式化输出
A='dog'
print('Itisa%s'%A)#-->Itisadog
#格式符可以是%d整数%f浮点
print('%06d'%1111)#-->001111#拿0补6位,不写0就拿空间补6位
print('%.3f'%1.2)#-->1.200#保留3位小数保留3位小数
print('Itisa{}'.format(A))#-->Itisadogformat函数也可以设置参数,传输对象:format的多种用法
逻辑操作符优先级andd or not
当not与and和or一起操作时,优先级是not>and>or
常见的字符串操作
find
检测 str 是否包含在 在mystr中,如果是返回开始索引值,否则返回-1
mystr.find(str,start=0,end=len(mystr))
index
就像find()方法一样,只是如果str不在 在mystr中报告一个异常.
mystr.index(str,start=0,end=len(mystr))
count
返回 str在start和end之间 在 mystr中出现的次数
mystr.count(str,start=0,end=len(mystr))
replace
把 mystr 中的 str1 替换成 如果,str2 count 指定时,替换不得超过 count 次.
mystr.replace(str1,str2,mystr.count(str1)
split
以 str 分隔符切片 mystr,如果 如果maxsplit有指定值,则只分隔 maxsplit 个子字符串
mystr.split(str="",2)
capitalize
大写字符串的第一个字符
mystr.capitalize()
title
大写字符串的每个单词的首字母
>>>a="helloworld" >>>a.title() 'Helloworld'
startswith
检查字符串是否正确 hello 开头, 是则返回 True,否则返回 False
mystr.startswith(hello)
endswith
检查字符串是否以obj结束,如果返回True,否则返回 False.
mystr.endswith('.jpg')lower
转换 mystr 所有的大写字符都是小写字符
mystr.lower()
upper
转换 mystr 中小写字母为大写
mystr.upper()
ljust
返回原字符串左对齐,用空间填充至长度 width 的新字符串
mystr.ljust(width)
rjust
返回右对齐的原始字符串,并用空间填充到长度 width 的新字符串
mystr.rjust(width)
center
回到原字符串中间,用空间填充到长度 width 的新字符串
mystr.center(width)
lstrip
删除 mystr 左边的空白字符
mystr.lstrip()
rstrip
删除 mystr 字符串末尾的空白字符
mystr.rstrip()
strip
删除mystr字符串两端的空白字符
>>>a="\n\thello\t\n" >>>a.strip() 'hello'
字典
查找元素
a={'a':1}
print(a.setdefault('b',2))#-->2#找到字典中是否添加到字典中
print(a.get('c'))#-->None#找不到就不报错
print(a)#-->{'a':1,'b':2}删除元素
a={'a':1,'b':2}
dela['a']#删除指定key
dela#删除内存中的整个字典
cleara#清空字典,a={}常见的字典操作
dict.len()
在测量字典中,键值对的数量
dict.keys()
返回包含字典中所有KEY的列表
dict.values()
返回包含字典所有value的列表
dict.items()
返回包含所有(键,值)元祖的列表
python内置函数
max() 返回元素
min() 返回最小元素
len(容器)
del(变量) 删除变量
map(function, iterable, ...)
根据提供的函数对指定序列进行映射
reduce(function, iterable[, initializer]) # initializer是初始参数
积累参数序列中的元素
filter(function, iterable)
用于过滤序列,过滤掉不合格元素,返回由合格元素组成的迭代对象(py3)。py2返回列表。
