九九乘法表
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
1:
for i in range(1,10):
line = ''
for j in range(1,i+1):
all = i * j
kong = ' ' if j ==2 and all < 10 else ' '
line += str(j) + '*' + str(i) + '=' + str(i*j) + kong
print(line)
三元表达式:kong = ' ' if j ==2 and all < 10 else ' '
2:
for i in range(1,10):
line = ''
for j in range(1,i+1):
all =1 if j ==1 else 2
line += '{}*{}={:<{}} '.format(j,i,i*j,all)
print(line)
format用法:line += '{}*{}={:<{}} '.format(j,i,i*j,all)
3:
for i in range(1,10):
for j in range(1,i+1):
print('{}*{}={:<{}}'.format(j,i,j*i,2 if j ==1 else 3),end='\n' if i==j else '')
反转乘法表
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9
2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18
3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27
4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36
5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
6*6=36 6*7=42 6*8=48 6*9=54
7*7=49 7*8=56 7*9=63
8*8=64 8*9=72
9*9=81
for i in range(1,10):
list12 = ''
for j in range(i,10): #10 > j >= i
list12 += '{}*{}={:<{}} '.format(i,j,i*j,2 if j ==1 else 3)
print('{:>{}}'.format(list12,1 if i ==1 else 71))
打印菱形
*
***
*****
*******
*****
***
*
for i in range(-3,4):#-3-2-1 0 1 2 3
line = ' '*abs(i) + '*'* (7-2*abs(i))
print(line)
或者
for i in range(-3,4):
print('{}{}'.format(' '*abs(i),'*'*(7-2*abs(i))))
居中打印
n = 7
e = n//2
for i in range(-e,e+1):
line = '*'*(n-2*abs(i))
print('{:^{}}'.format(line,n))
打印反转菱形
*******
*****
***
*
***
*****
*******
for i in range(-3,4):
print('{}{}'.format(' '*(3-abs(i)),'*'*(2*abs(i)+1)))
n = 7
e = n//2 #
for i in range(-e,n-e):
print('{}'.format((e-abs(i))*' '+ (2*abs(i)+1)*'*'))
居中打印
n = 7
e = n//2 #
for i in range(-e,n-e):
line = (2*abs(i)+1)*'*'
print('{:^{}}'.format(line,n))
打印闪电
for i in range(-3,4):
if i <0:
print('{}'.format(abs(i)*' '+(4-abs(i))*'*'))
if i == 0:
print(7*'*')
if i > 0:
print('{}'.format(3*' '+(4-abs(i))*'*'))
斐波那契数列
斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,
故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……
求第101项
a=1
b=1
print(1,1,sep='\n')
count = 2
while True:
c = a + b
if count == 101:
break
count +=1
print(c,count)
a,b = b,c
打印100以内的素数
判断是否素数
x = int(input('>>>>>>>'))
for i in range(2,x):
if x % 2 ==0:
break
else:
print('good')
优化
x = int(input('>>>>>>>'))
for i in range(2,int(x**0.5)+1):
if x % 2 ==0:
break
else:
print('good')
打印素数
for x in range(3,100,2):
if x > 10 and x %10 ==5 :
continue
for i in range(3,int(x**0.5)+1,2):
if x % i ==0:
break
else:
print(x)