# python经典练习题之九九乘法表、打印菱形、打印对顶三角形、斐波拉契数列、素数...

## 九九乘法表

help(print)

# 1 方阵

``````# 1 方阵
for i in range(1, 10):
line = ''
for j in range(1, 10):
line += str(i) + '*' + str(j) + '=' + str(i*j) + ' '
print(line)
print('-' * 30)``````

# 2 九九乘法表

``````for i in range(1, 10):
for j in range(1, 10):
if i >= j:
print(str(j) + '*' + str(i) + '=' + str(i*j), end=' ')
print()
print('-' * 30)``````

# 条件合并

``````for i in range(1, 10):
for j in range(1, i+1):
print(str(j) + '*' + str(i) + '=' + str(i*j), end=' ')
print()
print('-' * 30)``````

# 3 九九乘法表 对齐

``````for i in range(1, 10):
for j in range(1, i+1):
product = i * j
product = str(product) + ' ' if j > 1 and product < 10 else str(product)  #这里用表达式（python推荐用）
print(str(j) + '*' + str(i) + '=' + product, end=' ')
print()
print('-' * 30)``````

# 4 九九乘法表 制表符对齐

``````for i in range(1, 10):
for j in range(1, i+1):
print(str(j) + '*' + str(i) + '=' + str(i*j), end='\t')
print()
print('-' * 30)``````

# 5 使用字符串format方法

``````for i in range(1, 10):
line = ''
for j in range(1, i+1):
line += '{0}*{1}={2} '.format(j, i, i*j)
print(line)
print('-' * 30)``````

# 5 对齐

``````for i in range(1, 10):
line = ''
for j in range(1, i+1):
line += '{0}*{1}={2:<2} '.format(j, i, i*j)
print(line)
print('-' * 30)``````
• {2:<2}对应i*j，:<2冒号是分割符号，<表示左对齐，2表示宽度

# 5 对齐改进

``````for i in range(1, 10):
line = ''
for j in range(1, i+1):
product = i * j
line += '{}*{}={}{}'.format(j, i, product, ' ' if j > 1 and product < 10 else ' ')
print(line)
print('-' * 30)``````

# 其它对齐

``````for i in range(1, 10): # row
for j in range(1, i+1): # column [1, i+1) 1 <= j <= i
print("{}*{}={}{}".format(j, i, i*j, ' ' if j==2 and i<5 else ''),
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):
line = ''
print(' '*7*(i-1), end='') # 前置空格
for j in range(i, 10):
product = i * j
line += '{}*{}={}{}'.format(i, j, product, ' ' if product < 10 else ' ')
print(line)``````

``````for i in range(1, 10):
line = ''
for j in range(i, 10):
line += '{}*{}={:<{}}'.format(i, j, i * j, 2 if j < 4 else 3)
print('{:>66}'.format(line))``````

## 打印如下菱形

``````   *
***
*****
*******
*****
***
*``````

``````for i in range(-3,4):
if i<0:
prespace = -i
else:
prespace = i
print(' '*prespace + '*'*(7-prespace*2))``````

``````for i in range(-3, 4):
print(' ' * abs(i) + '*' * (7 - 2 * abs(i)))``````

``````for i in range(-3, 4):
print("{:^7}".format('*'*(7-2*abs(i))))``````

## 打印闪电

``````    *
**
***
********
***
**
*``````

``````行号  *个数  前空格  后空格数  总空格数  数据
1       1      3        3         6       -3
2       2      2        3         5       -2
3       3      1        3         4       -1
4       7      0        0         0        0
5       3      3        1         4        1
6       2      3        2         5        2
7       1      3        3         6        3``````

``````for i in range(-3, 4):
if i < 0:
print(' ' * (-i) + '*' * (4 + i))
elif i > 0:
print(' ' * 3 + '*' * (4 - i))
else:
print('*' * 7)

#延时扩展
n = 9
e = n // 2
x = n - e

for i in range(-e, x):
if i < 0:
print(' ' * -i + (x + i) * '*')
elif i > 0:
print(' ' * e + (x - i) * '*')
else: # i == 0
print('*' * n)``````

## 斐波那契数列，100以内

F(0)=0，F(1)=1, F(n)=F(n-1)+F(n-2)

``````print(0)
print(1)
a = 0
b = 1

while True :
c = a + b
if c > 100 : break
a = b
b = c
print(c)``````

## 求斐波那契数列第101项

``````a = 1
b = 1
print('index={}, fib={}'.format(0, 0))
print('index={}, fib={}'.format(1, a))
print('index={}, fib={}'.format(2, b))
index = 2

while True:
c = a + b
index += 1
print('index={}, fib={}'.format(index, c))
if index == 101: break
a = b
b = c

# index=101, fib=573147844013817084101``````

## 求10万内的所有素数

``````for x in range(2,100):
for i in range(2,x):
if x % i == 0:
break
else:
print(x)``````

``````for x in range(2,100000):
for i in range(2,int(x ** 0.5)+1):
if x % i == 0:
break
else:
print(x)``````

``````for x in range(2,100000):
for i in range(3,int(x ** 0.5)+1,2):
if x % i == 0:
break
else:
print(x)``````

``````for x in range(3,100000,2): # 舍弃掉所有偶数
for i in range(3, int(x ** 0.5) + 1, 2): # 为什么从3开始，且step为2？
if x % i == 0:
break
else:
print(x)``````

``````count = 1
for x in range(3, 100000, 2): # 舍弃掉所有偶数
if x > 10 and x % 10 == 5: # 所有大于10的质数中，个位数只有1,3,7,9。意思就是大于5，结尾是5就能被5整除了
continue
for i in range(3, int(x ** 0.5) + 1, 2):
if x % i == 0:
break
else:
count += 1
print(x, count) # 9592``````

``````count = 0
for x in range(2,100000):
for i in range(2,x):
if x % i == 0:
break
else:
count += 1
print(count)
# 9592``````
``````count = 0
for x in range(2,100000):
for i in range(2,int(x ** 0.5)+1):
if x % i == 0:
break
else:
count += 1
print(count)
# 9592``````

# 两种算法的对比的完整代码

``````import datetime

upper_limit = 100000
delta = [0,0]
counts = [0,0]

start = datetime.datetime.now()
for _ in range(10):
counts[0] = 0
for x in range(2,upper_limit):
for i in range(2,int(x ** 0.5)+1):
if x % i == 0:
break
else:
#print(x)
counts[0] += 1
delta[0] = (datetime.datetime.now() - start).total_seconds()

start = datetime.datetime.now()
for _ in range(10):
counts[1] = 1
#print(2)
for x in range(3,upper_limit,2):
for i in range(3,int(x ** 0.5)+1,2):
if x % i == 0:
break
else:
#print(x)
counts[1] += 1
delta[1] = (datetime.datetime.now() - start).total_seconds()

print(delta, sep="\t")
print(counts, sep="\t")``````

 IT入门 感谢关注 程序员题库→程序员用的单词表→练习地址：www.520mg.com/it