美文网首页
Python 练手题

Python 练手题

作者: Arudora | 来源:发表于2017-07-03 15:04 被阅读0次

    刚刚学了Python一点皮毛,找东西来练手巩固一下
    从知乎上看的 Python 练习册,每天一个小程序

    第 0000 题:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果。

    from PIL import Image,ImageDraw,ImageFont
    pic = Image.open('D:/1.jpg')
    print(pic.format,pic.size,pic.mode)
    size = 100
    font = ImageFont.truetype('AdobeHeitiStd-Regular.otf',size)
    #img = Image.new('RGB',(300,200),(255,0,0))
    draw = ImageDraw.Draw(pic)
    x = pic.size[0] - size
    draw.text((x,0),u'4',(255,0,0),font = font)
    pic.show()
    pic.save('D:/2.jpg')
    

    **第 0001 题: **做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?

    import random
    import string
    # list1 = range(10000000,99999999)
    # coupond = random.sample(list1,200)
    #print(coupond)
    
    def random_str(randomlength = 8):
        str = ''
        for i in range(randomlength):
            str += (random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'))
        return str
    coupond = []
    for num in range(200):
        a = random_str()
        coupond.append(a)
    print(coupond)
    

    **第 0002 题: **将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。

    安装数据库总是遇到问题啊,FKFKFK。
    

    第 0003 题: 将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。

    同上
    

    第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数。

    txt = open('C:/Users/admin/test.txt','r')
    txt1 = txt.read() #读出txt内容
    txt2 = []  #设一个空的list
    n = ''  #设一个空的字符
    for x in str(txt1):
        if x == ' ':
            txt2.append(n)
            n = ''
        n += str(x)
        # print(n)
    # print(txt2)
    
    mylist = set(txt2)
    for item in mylist:
        print('the %s : %d'%(item,txt2.count(item)))
    

    **第 0005 题: **你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。

    import os,re
    from PIL import Image
    listinfo = 'D:/test/'
    mylist = os.listdir(listinfo)
    print(mylist)
    n1 = 1136
    n2 = 640
    for n in mylist:
        c_x = 0
        c_y = 0
        print(n)
        im = Image.open(listinfo+n)
        (x,y) = im.size
        print(x,y)
        print(c_x,c_y)
        if x > n2:
            c_x = n2
            c_y = int(n2/x*y)
        else:
            if y > n1:
                c_y = n1
                c_x = int(n1/y*x)
            else:
                continue
    
        print(c_x,c_y)
        out = im.resize((c_x,c_y),Image.ANTIALIAS)
        out.save(listinfo+n)
        print(im.size)
    

    第 0006 题: 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。

    import os,re
    listinfo = 'D:/test/'
    mylist = os.listdir(listinfo)
    myword = []
    for a in mylist:
        txt = open(listinfo+a)
        txt1 = txt.read() #读出txt内容
        n = ''  #设一个空的字符
        for x in str(txt1):
            if x == ' ':
                myword.append(n)
                n = ''
            n += str(x)
    mylist = set(myword)
    dic = {}
    for item in mylist:
        dic[item] = myword.count(item)
    for k,v in dic.items():
        if v == max(dic.values()):
            print(k)
    

    **第 0007 题: **有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。

    import os
    listinfo = 'D:/test/'
    mylist = os.listdir(listinfo)
    txtline = 0
    countBlank = 0
    note = 0
    alltxt = ''
    for a in mylist:
        txt = open(listinfo+a)
        while True:
            line = txt.readline()
            if not line:
                break
            txtline += 1
            if line[0] == '#':
                note += 1
            if len(line)==line.count('\n'): #判断是否为空行  
                countBlank +=1
    print('文件行数为:',txtline)
    print('文件空行数为:',countBlank)
    print('文件注释行数为:',note)
    

    相关文章

      网友评论

          本文标题:Python 练手题

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