python有哪些小练习案例
小编给大家分享一下python有哪些小练习案例,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!
四个python小练习:
1、打印出1-100之间的所有偶数
<preclass="brush:python;gutter:true;">defeven_print():foriinrange(1,101):ifi%2==0:print(i)even_print()#列表解析式的方式:k=[nforninrange(1,101)ifn%2==0]print(k)
2、设计一个函数,在桌面上创建10个文件,并以数字命名
deftext_creation():path='C:/Users/Administrator/Desktop'fornameinrange(1,11):withopen(path+str(name)+'.txt','w')astext:text.write(str(name))text.close()print('done')text_creation()
3、复利计算函数
definvest(amount,rate,time):print('principalamount:{}'.format(amount))fortinrange(1,time+1):amount=amount*(1+rate)print('year{}:${}'.format(t,amount))invest(100,.05,8)invest(2000,.025,5)
4、随机验证码
importrandomcheckcode=''foriinrange(4):current=random.randrange(0,4)ifcurrent!=i:temp=chr(random.randint(65,90))else:temp=random.randint(0,9)checkcode+=str(temp)print(checkcode)
看完了这篇文章,相信你对python有哪些小练习案例有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。