一直一直以来想自学python,几乎每次都只停留在收藏。偶尔一次也学了几天便放弃。希望这次暑期自己可以坚持下来,没有什么要求,只希望每天都能学会一点点东西。
直接通过简单的例子学习基础语法
题库来自于:
Python基础训练100题(带答案) - 知乎 (zhihu.com)
001:数字组合
1、2、3、4,能组成多少个互不相同且无重复数字的三位数?
total=0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if ((i!=j)and(j!=k)and(k!=i)):
print(i,j,k)
total+=1
print(total)
学了c,r,matlab,这个for循环是真够烦的
在python中,for循环语法要点:
for i in range(a,b):
#a b 用逗号,不是冒号
#后面要加一个:
#不用加end
#必须tab才行
#i=a,a+1,a+2,,,b-1
if
#用and 不用 &&
print(a)
#not printf ,without ''
简便方法,itertools中的permutations
import itertools
sum2=0
a=[1,2,3,4]#列表
#a=(1,2,3,4) #iterable 是元组
#列表和元组输出一样
for i in itertools.permutations(a,3):
print(i)
sum2+=1
print(sum2)
原型 itertools.permutations(iterable, r=None),返回可迭代对象 iterable 的长度为 r 的所有数学全排列方式(每种排列方式是以元组形式存在)。
更多参考:(9条消息) itertools 中的 permutations 实现全排列和任意排列_雾隐雾现的至渝博客-优快云博客_itertools全排列
002个税计算
企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
profit = int(input("show me the money"))
bonus = 0
thresholds = [0, 100000, 200000, 400000, 600000, 1000000]
rates = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01]
for i in range(0,6):
if (thresholds[i]<=profit)and (profit<=thresholds[i+1]):
if i == 0:
bonus = profit*rates[i]
else:
for j in range(0,i):
bonus+=(thresholds[j+1]-thresholds[j])*rates[j]
bonus+=(profit-thresholds[i])* rates[i]
print(bonus)
003完全平方数
一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
for i in range(1,85):
if 168 % i ==0:
j=168/i
if i%2==0 and j%2==0 and i>=j:
m=(i+j)/2
n=(i-j)/2
x=n*n-100
print(x)
分析:
假设该整数为x ,x + 100 = nn , x + 100 + 168 = m * m
计算等式:mm - n*n = (m+n)(m-n)=168
设置:m + n = i , m -n = j, i * j = 168,即i 和j 至少有一个偶数
m = (i+j)/2,n = (i-j)/2,因为m 和 n 都是为整数,所以i 和 j 要么都是偶数,要么都是奇数。
从3和4推导可知,i 和 j均是大于等于2的偶数
由于i * j =168, j >= 2 , 则1 < i < 168/2 +1
然后把所有i的数字循环计算即可。
————————————————
https://blog.youkuaiyun.com/liuzhuang2017/article/details/85179327
004这天第几天
输入某年某月某日,判断这一天是这一年的第几天?
def isLeapYear(y):
return (y%400==0 or (y%4==0 and y%100!=0))
DofM=[0,31,28,31,30,31,30,31,31,30,31,30]
res=0
year=int(input('Year:'))
month=int(input('Month:'))
day=int(input('day:'))
if isLeapYear(year):
DofM[2]+=1#如果是闰年多加一天
for i in range(month):
res+=DofM[i]
print(res+day)
def 定义函数,基础用法
def<函数名>(参数):
函数体
return 返回值
更多:(9条消息) 【Python】def函数【新手向】_QAQjiaru233的博客-优快云博客_def函数
005三数排序
输入三个整数x,y,z,请把这三个数由小到大输出。
raw=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw.append(x)
for i in range(len(raw)):
for j in range(i,len(raw)):
if raw[i]>raw[j]:
raw[i],raw[j]=raw[j],raw[i]
print(raw)
#冒泡排序的感觉
python输入数组的方法get
append的作用就是在raw末尾添加x元素

更多:python中append的用法-Python教程-PHP中文网
交换元素: raw[i],raw[j]=raw[j],raw[i]
直接调用sorted函数
raw2=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw2.append(x)
print(sorted(raw2))
交换元素: raw[i],raw[j]=raw[j],raw[i]
也可以直接调用sorted函数
raw2=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw2.append(x)
print(sorted(raw2))
这篇博客介绍了Python编程的基础知识,包括for循环、条件判断、函数定义以及一些实用技巧。通过解决数字组合、个税计算、完全平方数和日期计算等实际问题,展示了Python的语法和逻辑。此外,还探讨了如何使用itertools模块简化全排列计算,并提供了排序算法的实现。
1586

被折叠的 条评论
为什么被折叠?



