python有哪些小练习案例

发布时间:2020-07-01 16:42:53 作者:清晨
来源:亿速云 阅读:112

小编给大家分享一下python有哪些小练习案例,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!

四个python小练习:

1、打印出1-100之间的所有偶数

<pre class="brush:python;gutter:true;">def even_print():
    
for i in range(1,101):
       
 if i % 2 == 0:
            
print (i)
even_print()
#列表解析式的方式:
k = [n for n in range(1,101) 
if n%2 == 0]
print (k)

2、设计一个函数,在桌面上创建10个文件,并以数字命名

def text_creation():    
path = 'C:/Users/Administrator/Desktop'    
for name in range (1,11):        
    with open(path + str(name) + '.txt','w') as text:                
       text.write(str(name))                
       text.close()            
print ('done')text_creation()

3、复利计算函数

def invest(amount,rate,time):
    print('principal amount:{}'.format(amount))    
    for t in range(1,time + 1):
        amount = amount * (1 + rate)        
        print ('year {}: ${}'.format(t,amount))
invest(100,.05,8)
invest(2000,.025,5)

4、随机验证码

import random
checkcode = ''
for i in range(4):
   current = random.randrange(0,4)    
   if current != i:
       temp = chr(random.randint(65,90))    
   else:
       temp = random.randint(0,9)
   checkcode += str(temp)
print (checkcode)

看完了这篇文章,相信你对python有哪些小练习案例有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. jQuery基础--案例练习
  2. JavaScript有哪些小技巧

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:python随机生成数组的方法

下一篇:基于python的大数据分析实战学习笔记-Anaconda

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》