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

d1cdf06cac6c1752de8930450f23f4c9.png

python内置的数据类型

a4cf73a5cc9af93ee281a4ac4e8167c5.png

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('It is a %s' % A ) # --> It is a dog

# 格式符可以是 %d整数 %f浮点数

print('%06d'% 1111) #-->001111 # 拿0补全6位,不写0就是拿空格补全6位

print('%.3f' %1.2) #-->1.200 # 保留3位小数

print('It is a {}'.format(A) ) # --> It is a dog

关于format函数还可以设置参数,传递对象:format多种用法

逻辑运算符优先级and or not

当not和and及or在一起运算时,优先级为是not>and>or

字符串常见操作

find

检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1mystr.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 = "hello world"

>>> a.title()

'Hello world'

startswith

检查字符串是否是以 hello 开头, 是则返回 True,否则返回 Falsemystr.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\t hello \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}

del a['a'] # 删除指定key

del a      # 删除整个字典在内存里清除

clear a    # 清空字典,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返回列表。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值