Python练习题(九九乘法表、打印菱形、求素数、求斐波那契数列)

1、打印九九乘法表

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

2、打印以下ling菱形

 

 

 

 

 

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

3、打印100以内的的斐波那契数列

print(0)
print(1)
a = 0
b = 1
while True:
    c = a + b
    if c > 100:
        break
    else:
        a = b
        b = c
        print(c)

4、求斐波那契数列第101项

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

5、求10万内的所有素数

for x in range(3,100000,2):#舍弃掉所有偶数
    for i in range(3,int(x ** 0.5) + 1,2):#外层循环已经过滤了偶数,既然没有偶数,就不用对2取模了
        if x % i == 0:
            break
    else:
        print(x)

 

你可能感兴趣的:(Python练习题(九九乘法表、打印菱形、求素数、求斐波那契数列))