arr=[]
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
num=100*i+10*j+k
if i!=j and j!=k and i!=k and num not in arr:
arr.append(num)
print(len(arr),arr)
解法二:
import itertools#利用自带的排列组合模块
temp_arr = list(itertools.permutations([1, 2, 3, 4], 3))
print(temp_arr)
输出的是元组的列表
完整代码:
import itertools
temp_arr=list(itertools.permutations([1,2,3,4],3))
arr=[100*t[0]+10*t[1]+t[2] for t in temp_arr]
print(len(arr),arr)
money=int(input('净利润:'))
arr=[1000000,600000,400000,200000,100000,0]
rat=[0.01,0.015,0.03,0.05,0.075,0.1]
bonus=0
for i in range(len(arr)):
if money>arr[i]:
bonus+=(money-arr[i])*rat[i]
money=money-arr[i]
print(bonus)
import math #判断一个数是否为完全平方数,可用math.sqrt(i)%1?=0
for i in range(1000):
if math.sqrt(i+100)%1==0 and math.sqrt(i+168)%1==0:
print(i)
import time
date=input('输入时间(例如;2019-09-15):')
st=time.strptime(date,'%Y-%m-%d')#Yb必须大写,m和d必须小写
#print(st)
#输入时间(例如;2019-09-15):2019-09-15
#time.struct_time(tm_year=2019, tm_mon=9, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=258, tm_isdst=-1)
print(st.tm_yday
x=input('first num:')
y=input('second num:')
z=input('third num:')
l=[x,y,z]
arr=sorted(l)
print(arr)
l=[0,1]
for i in range(10):
arr=l[-2:]
l.append(arr[-2]+arr[-1])
print(l)
a = [1, 2, 3]
b = a[:]
1、%s,直接输出字符串。
2、%[n]s,左补空格足n位后输出。-n为右补空格
3、%[m.n]s,从左截取n位后,左补空格足m位后输出。-m为右补空格
print默认是打印一行,结尾加换行。end=’ '意思是末尾不换行,加空格。
for i in range(1,10):
for j in range(1,10):
if j<=i:
string='%d*%d=%d'%(j,i,i*j)
print('%-7s'%string,end=' ')#end=' '表示不换行,空一格
print(' ')#此时end为默认值,表示会换行
解法二:
format功能:
"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
运行结果:'hello world'
"{0} {1}".format("hello", "world") # 设置指定位置
运行结果:'hello world'
"{1} {0} {1}".format("hello", "world") # 设置指定位置
运行结果:'world hello world'
for i in range(1,10):
for j in range(1,i+1):
print('{0}*{1}={2}'.format(j,i,i*j),end='\t')
print()
import time
a = time.time()
time.sleep(1)
b = time.time()
print(b-a)
import time
a=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print(a)
time.sleep(1)
b=time.strftime('%Y-%m_%d %H:%M:%S',time.localtime(time.time()))
print(b)