菜鸟教程python3100题_Python作业留底--《菜鸟教程》练习
和习题
写这个是希望有个学习督促,毕竟⾃⼰前前后后拖拉了太多时间。希望正在和我⼀样初学的菜鸟都互相交流,要不⾃⼰⼀个坚持挺难,加上年纪⼤。
2019.11.12菜鸟教程Python 100例-1
import time
"""
题⽬:有四个数字:1、2、3、4,能组成多少个互不相同且⽆重复数字的三位数?各是多少?
程序分析:可填在百位、⼗位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满⾜条件的排列。
"""
start_time=time.perf_counter()
output=[]
constant=['1','2','3','4']
for item in range(110,445):
chr=str(item)
if chr[0]==chr[1] or chr[1]==chr[2] or chr[0]==chr[2] \
or chr[0] not in constant or chr[1] not in constant or chr[2] not in constant:
continue
else:
output.append(item)
end_time=time.perf_counter()
print (len(output))
print (output)
print (end_time-start_time)
2019.11.19菜鸟教程Python 100例-2
def Calculation_of_bonuse(profit):
if profit>0 and profit<=10:
bonus=profit*0.01
elif profit>10 and profit<=20:
bonus = 10*0.01+(profit-10)*0.075
elif profit>20 and profit<=40:
bonus = 10*0.01+10*0.075+(profit-20)*0.05
elif profit>40 and profit<=60:
bonus = 10*0.01+10*0.075+20*0.05+(profit-40)*0.03
elif profit>60 and profit<=100:
bonus = 10*0.01+10*0.075+20*0.05+20*0.03+(profit-60)*0.015
elif profit>100:
bonus = 10*0.01+10*0.075+20*0.05+20*0.03+40*0.015+(profit-100)*0.01
print (bonus*10000)\
if __name__=="__main__":
profit=eval(input("The month's profit(RMB:万元):"))
Calculation_of_bonuse(profit)
"""别⼈使⽤列表嵌套和字典⽅式完成,⽜掰"""
num=eval(input('请输⼊公司利润(万):'))
object={100:0.01,60:0.015,40:0.03,20:0.05,10:0.075,0:0.1}
profit_tags=object.keys()
profit=0
for key in profit_tags:
if num > key:
profit +=(num-key)*(key)
num =key
print ('奖⾦为:{}万元'.format(profit))
2019.12.16菜鸟教程Python 100例-3
"""⼀个整数,它加上100后是⼀个完全平⽅数,再加上168⼜是⼀个完全平⽅数,请问该数是多少?假设该数为 x。""" for k in range(1,13):
n=84/k-k/2#在混合计算时,Python会把整型转换成为浮点数
if int(n)==n:
x=pow(n,2)-100
print (x)
2019.12.23菜鸟教程内部例⼦再现
"""将给定的字符串倒序后输出"""
str_1 = input("请输⼊⼀个字符串:")
new_list=[]
for i in range(len(str_1),0,-1):
new_list.append(str_1[i-1])
print(''.join(new_list))
2019.12.24菜鸟教程内部例⼦再现
input_info=input('请输⼊⼀组数字:')#输⼊的字符串123
new_of_str='〇⼀⼆三四五六七⼋九'#⽐对组
old_of_str='0123456789'
for i in old_of_str:#从old_of_str中逐⼀跳出字符串来⽐对
"""将old_of_str的对应单个字符(存在0123456789中的)替换为s中对应的单个字符,eval(i)将表达式i执⾏,最终变为列表new_of_str 的索引值"""
input_info=place(i,new_of_str[eval(i)])
print(input_info)
2020.1.2菜鸟教程Python 100例-4
#输⼊某年某⽉某⽇,判断这⼀天是这⼀年的第⼏天?
#第⼀版本——————————————————————————————————————————
date=input('请输⼊⽇期(参照2019.1.1格式):')
date_list=date.split('.')
year=date_list[0]
month=date_list[1]
day=date_list[2]
def if_leapyear(year):
"""
:param self: ⾮整百年数除以4,⽆余为闰,有余为平;②整百年数除以400,⽆余为闰有余平
1-12⽉分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天
:param year:year
:return:返回⼀个字典格式每⽉天数
"""
year_tag={}
if (eval(year) % 4 == 0 and eval(year) % 100 != 0) or (eval(year) % 400 == 0 and eval(year) % 3200 != 0) or eval(year) % 172800 == 0:
return {1:31,2:29,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
else:
return {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
year_tag=if_leapyear(year)
day_1=0;day_2=0
for i in year_tag:
if i < eval(month):
day_1 = day_1 + year_tag[i]
elif i==eval(month):
day_2=eval(day)
else:
break
days=day_1+day_2
print(f'这⼀天是这⼀年的第{days}天')
#第⼆版本----------------------------------------------------------------------------------
date=input('请输⼊⽇期(参照2019.1.1格式):')
date_list=date.split('.')
year=date_list[0]
month=date_list[1]
day=date_list[2]
def if_leapyear(year):
"""
:param self: ⾮整百年数除以4,⽆余为闰,有余为平;②整百年数除以400,⽆余为闰有余平
1-12⽉分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天
:param year:year
:return:返回⼀个字典格式每⽉天数
"""
year_tag={}
if (eval(year) % 4 == 0 and eval(year) % 100 != 0) or (eval(year) % 400 == 0 and eval(year) % 3200 != 0) or eval(year) % 172800 == 0:
return {1:31,2:29,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
else:
return {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
year_tag=if_leapyear(year)
day_1=0;day_2=0
days=eval(day)
for i in year_tag:
if i < eval(month):
days +=year_tag[i]
else:
break
print(f'这⼀天是这⼀年的第{days}天')
2020.1.12菜鸟教程Python 100例-5
python菜鸟教程文档inputs=input("输⼊整数:")
input_list=inputs.split(',')
output_list=[eval(i) for i in input_list]
output_list.sort()
print(output_list)
2020.1.14菜鸟教程Python 100例-6
#数列:0、1、1、2、3、5、8、13、21、34、……def fib_1(n):
a,b=0,1
for i in range(n-1):
a,b=b,a+b#先运算b,a+b再赋值
print(f'(第{i+1}次循环)')
print(a,b)
#print(id(a),id(b))
#return a
#print(fib_1(4))
def fib_2(n):#错误⽅法1
a,b=0,1
for i in range(n-1):
a=b
b=a+b
print(f'(第{i+1}次循环)')
print(a,b)
#print(id(a), id(b))
#return a
#print(fib_2(4))
def fib_3(n):#递归
if n==1:
return 0
elif n==1 or n==2:
return 1
else:
return fib_3(n-1)+fib_3(n-2)
#print(fib_3(5))
2020.2.2菜鸟教程Python 100例-7

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。