10 月 122020
 
#打印1-100范围内偶数

#判断是否偶数
number = range(1,101) #定义取值范围1-100赋值给变量

for i in number: #定义for循环,元素赋值变量
    if (i%2) == 0: #判断,比较运算,取模为0时为True
        print(i)

print(bool((3%2 == 0))) #False
print(bool((4%2 == 0))) #True

#改造,将偶数写入列表,并打印列表中的元素

#打印1-100范围内偶数

#判断是否偶数
number = range(1,101) #定义取值范围1-100赋值给变量
list = [] #空列表

for i in number: #定义for循环,元素赋值变量
    if (i%2) == 0: #判断,比较运算,取模为0时为True
        #print(i)
        list.append(i) #改造,将偶数写入列表,并打印列表中的元素

print(bool((3%2 == 0))) #False
print(bool((4%2 == 0))) #True
print(list) #打印列表
PS D:\Python> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe d:/Python/hello.py
False
True
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 
52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
PS D:\Python>
10 月 122020
 
#复利计算,利率5%,输入金额,显示1年-8年的资金总额
#invest(amount, rate, time)

amount = input('principal amount: ') #用户输入初始金额赋值变量
rate = float(1 / 20) #固定利率
time = range(1, 9) #显示1-8年

#定义计算函数
def invest(): #变量作为固定参数,首次来自用户输入    
    total_amount = float(amount) + float(amount) * rate #计算公式
    return total_amount

#循环调用函数并打印结果
for i in time:
    #amount = invest(amount) # 位置很重要,应当在第一次调用函数后再赋值
    print('year ' + str(i) + ': ' + str(invest()))
    amount = invest() #打印完第1年总额后将该总额再赋值给amount变量
PS D:\Python> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe d:/Python/hello.py
principal amount: 100
year 1: 105.0
year 2: 110.25
year 3: 115.7625
year 4: 121.550625
year 5: 127.62815624999999
year 6: 134.00956406249998
year 7: 140.71004226562496
year 8: 147.7455443789062
PS D:\Python> 

#改造,用户输入函数两个参数,打印1-8年总额

#改造,用户输入函数两个参数,打印1-8年总额

#复利计算,利率5%,输入金额,显示1年-8年的资金总额
#invest(amount, rate, time)

amount = input('principal amount: ') #用户输入初始金额赋值变量
rate = input('input rate %: ') #用户输入利率
rate = float(int(rate) / 100) #固定利率
time = range(1, 9) #显示1-8年

#定义计算函数
def invest(): #变量作为固定参数,首次来自用户输入
    total_amount = float(amount) + float(amount) * rate #计算公式
    return total_amount

#循环调用函数并打印结果
for i in time:
    #amount = invest(amount) # 位置很重要,应当在第一次调用函数后再赋值
    print('year ' + str(i) + ': ' + str(invest()))
    amount = invest() #打印完第1年总额后将该总额再赋值给amount变量
PS D:\Python> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe d:/Python/hello.py
principal amount: 200
input rate %: 6
year 1: 212.0
year 2: 224.72
year 3: 238.2032
year 4: 252.495392
year 5: 267.64511552
year 6: 283.7038224512
year 7: 300.726051798272
year 8: 318.76961490616833
PS D:\Python>
10 月 122020
 
#定义一个创建文件的函数
def create_file():
    path = 'C:/Users/harveymei/Desktop/' #最后一个斜线必须有
    fullname = path + filename + '.txt'
    file = open(fullname, 'w')
    file.close()

#使用for循环调用创建文件函数
for name in range(1,11): #使用range()函数提供1-10序列数值
    filename = str(name) #使用str()函数将整数转换为字符串赋值给变量
    create_file() #循环调用函数,创建10个文件
PS D:\Python> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe d:/Python/hello.py
PS D:\Python> ls C:\Users\harveymei\Desktop\


    目录: C:\Users\harveymei\Desktop


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2020/10/12     11:51              0 1.txt
-a----        2020/10/12     11:51              0 10.txt
-a----        2020/10/12     11:51              0 2.txt
-a----        2020/10/12     11:51              0 3.txt
-a----        2020/10/12     11:51              0 4.txt
-a----        2020/10/12     11:51              0 5.txt
-a----        2020/10/12     11:51              0 6.txt
-a----        2020/10/12     11:51              0 7.txt
-a----        2020/10/12     11:51              0 8.txt
-a----        2020/10/12     11:51              0 9.txt
-a----         2020/10/9      9:00           2136 Docker Desktop.lnk
-a----         2020/4/14      8:55           2371 GitHub Desktop.lnk
-a----         2020/8/28     15:20           2312 Kindle.lnk
-a----         2020/8/21     17:59           2255 MockingBot.lnk
-a----        2020/10/10      9:40           2217 Slack.lnk


PS D:\Python>