目录 # odd or even # calculator + #大公鸡i,母鸡j,小鸡k # 斐波那契额数列
#求50以内的所有质数和 # while_1 1. 起始值;2. 终止值; 3. step; # 质数 #字符串处理 & 列表推倒式子 #字符串处理 & 列表推倒式子 # 9x9乘法表 使用列表推倒式实现list对应元素相乘 # 3x3矩阵 方法1 #方法2 #怎样去定义product #%%函数练习 # 函数定义引用练习 # 函数参数练习 #函数不定长参数练习 #%% #递归函数 N的阶乘
a=input('please input a: ') b=input('please input b: ') c=input('please input c: ')
if a>b: if a>c: print('最大数是a',a) else: print('最大数是c',c) elif b>c: print('最大数是b',b) else: print('最大数是c',c) |
# odd or evena=int(input('please input a number: ')) if a%2==1: print('odd') else: print('even') |
# calculator +
num_1=int(input('please input num_1: ')) num_2=int(input('please input num_2: ')) operator=input('please inut operator: ')
if operator=='+': print('{0}+{1}={2}'.format(num_1,num_2,num_1+num_2)) elif operator=='-': print('%d-%d=%d'%(num_1,num_2,num_1-num_2)) elif operator=='*': print('%d*%d=%d'%(num_1,num_2,num_1*num_2)) print('%s*%s=%s'%(num_1,num_2,num_1*num_2)) |
#大公鸡i,母鸡j,小鸡k
for i in range(0,21,1): for j in range(0,round((100-5*i)/3+1),1): for k in range(0,(100-5*i-3*j)+1,1):
if 5*i+3*j+k==100: print('{0}只公鸡,{1}只母鸡,{2}只小鸡'.format(i,j,k)) #print('%d只公鸡,%d只母鸡,%d只小鸡'%(i,j,k)) break else: pass pass pass pass |
# 斐波那契额数列list0=[1,1] for i in range(2,100-1+1,1): list0.append(list0[i-1]+list0[i-2]) pass print(list0) |
#求50以内的所有质数和num=50 i=int(3)#i必须被初始化 print(i) while i in range(2,num,1): print(i) if num%i==0: print(num,'不是质数') break elif i==num-1: print(num,'是质数') sum=sum+num print('sum=',sum) break else:
i=i+1 print(i) pass |
# while_1 1. 起始值;2. 终止值; 3. step;stop=int(input('please input an integer')) begin=int(1) #while (begin+1)<=stop: # print(begin+1); # begin=begin+2; #else: # 0 for i in range(begin,stop+1,2): print(i) pass |
# 质数# 1. 判断质数;2. 遍历;3. 求和; sum=2 print('2是质数') print('sum=',sum) print('3是质数') print('sum=',sum+3) #遍历 for num in range(4,100+1,1): #判断是否是质数 i=int(3)#i必须被初始化 while i in range(2,num,1):#改成for,就不需用事先定义变量i=int(3),i=i+1 也不需用了 if num%i==0: print(num,'不是质数') break elif i==num-1: print(num,'是质数') sum=sum+num print('sum=',sum) break else: i=i+1 pass #num=num+1 #此处在for in range语句中不需用 pass 0 #while |
#字符串处理 & 列表推倒式子names=['Tom','Billy','Jefferson','Andrew','Wesley','Steven','Joe'] Mm=[name.upper() for name in names if len(name)>3] Ss={name.lower() for name in names if len(name)>2} print([name for name in names if len(name)>=4]) #列表推导式 bigger_than_5_Caps=[name.upper() for name in names if len(name)>=5] print(bigger_than_5_Caps) |
#字符串处理 & 列表推倒式子num=[i*100+j*10+k for i in range(1,10) for j in range(10) for k in range(10) if i**3+j**3+k**3==i*100+j*10+k] print(num) |
a=10 for i in range(1,a+1,1): char=' * '*i print(char.center(30)) pass for i in range(9,0,-1): char=' * '*i print(char.center(30)) pass result: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
# 9x9乘法表for i in range(1,10): for j in range(1,i+1): print('%d*%d=%-d'%(i,j,i*j),end=' ') print() |
使用列表推倒式实现list对应元素相乘a=[1,2,3] b=[4,5,6] c=[i*j for (i,j) in tuple(zip(a,b))]
m=[[1,2,3],[4,5,6],[7,8,9]] n=[[2,2,2],[3,3,3],[4,4,4]] #能否用嵌套式列表推导+zip()语句实现? product0=[p*q for (i,j) in tuple(zip(m,n)) for (p,q) in zip(i,j)] '[2, 4, 6, 12, 15, 18, 28, 32, 36]' # 3x3矩阵 方法1product=[[i*j for (i,j) in zip(a,b)] for (a,b) in zip(m,n)] '[[2, 4, 6], [12, 15, 18], [28, 32, 36]]'
#方法2#怎样去定义productproduct3=[] for i in range(3): list2=[] for j in range(3): list2.append(m[i][j]*n[i][j]) product3.append(list2) # pass #pass '[[2, 4, 6], [12, 15, 18], [28, 32, 36]]'
|
#%%函数练习# 函数定义引用练习def PrintName(Name): print('the name is: ',Name)
PrintName('wangbingkun')
#%% # 函数参数练习def sum0(a,b=10): print('a=',a) print('b=',b) return a+b;
print(sum0(3,4)) #%% #函数不定长参数练习def test(a,b,c,*tup_args,**dict_args): print(a, b, c) print(tup_args) print(dict_args)
test(1,2,3,'a1','a2','a3',name='joe',age=18) result: 1 2 3 #%%a=[1,2,3,[1,2,3]] b=list(a) def test(): # a=[1,2,3,[1,2,3]] a[0]='we' a[3][0]='python' print(a) test() print(a) print(b)
['we', 2, 3, ['python', 2, 3]]
|
#递归函数 N的阶乘def factor(n): if n==1: return 1 elif n==0: return 0 elif n>1: return n*factor(n-1) else: print('输入数值有误,请重新输入自然数:') |