美文网首页
python基础用法汇总

python基础用法汇总

作者: 倦意羽毛 | 来源:发表于2019-10-31 09:57 被阅读0次
# random模块
import random
num=random.randint(1,10)
all_choices = ['石头', '剪刀', '布']
computer = random.choice(all_choices)


# range用法
# range(10)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# range(6, 11)  # [6, 7, 8, 9, 10]
# range(1, 10, 2)  # [1, 3, 5, 7, 9]
# range(10, 0, -1)  # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# range(1, 101)  #1=>100

# 列表解析
['192.168.1.%s' % i for i in range(1, 255) if i%2 ==0]

# 文件操作
with open('aaa.txt', 'rb') as fr:
    with open('bbb.txt', 'wb') as fw:
        while True:
            data = fr.read(4096)
            if not data:
                break
            fw.write(data)

# string模块
import string
print(string.digits)    # 数字
print(string.ascii_letters) # 大小写英文字符

相关文章

网友评论

      本文标题:python基础用法汇总

      本文链接:https://www.haomeiwen.com/subject/yhuzvctx.html