Python基础练习

1.打印空心正方形

lines = int(input("please set a number:"))
for line in range(lines):
    if line == 0 or line == lines-1 :
        print(lines*"* ")
    else:
        print("*"+ (2*lines-3)*" "+ "*")

1.2对称打印

n=5
e=-n//2
for i in range(e,n+e):
    if i==e or i==n+e-1:
        print('*'*n)
    else:
        print('*'+' '*(n-2) + '*')

1.3 9*9乘法表

for i in range(1,10):
    for j in range(1,i+1):
        print('{} * {} = {}'.format(j,i,i*j),end="\t")
    print("\n")
for i in range(1,10):
    s=""
    for j in range(i,10):
        #三元表达式,2是if的第一个条件的输出
        s+='{}*{}={:<{}}'.format(i,j,i*j,2 if j<4 else 3)
    #:>是固定写法 向右对齐,宽度是66
    print('{:>66}'.format(s))    

 

1.4判断是不是质数

lines = int(input("please set a number:"))

for i in range(2,lines):
    if lines % i == 0:
        print("不是质数")
        break
else:
    print("是质数")

1.5打印菱形

lines = int(input("please set a number:"))
tmp = lines//2
v = 1
for line in range(1,lines+1,2):
    print(tmp*" " + line*'*' + tmp*" ")
    tmp-=1

for vline in range(lines-2,0,-2):
    print(v*" " + vline*"*" + v*" ")
    v+=1

1.5.2打印菱形

lines = int(input("please set a number:"))
first = lines//2+1
for i in range(lines):
    if i <first:
        print((lines//2-i)*" " + (2*i+1)*"*" + (lines//2-i)*" ")
    else:
        print((lines//2-lines+1+i)*" "+ (2*lines-2*i-1)*"*" +(lines//2-lines+1+i)*" ")

1.5.3打印菱形

lines = int(input("please set a number:"))  #5
opp_lines = -lines//2                       #-3


for line in range(opp_lines,lines+opp_lines):  #-1
    if line == -1:
        print(lines*"*")
    elif line<-1:
        print((-line-1)*" "+ (lines-2*(-line-1))*"*" +(-line-1)*" ")
    else:
        print((line+1)*" "+ (lines-2*(line+1))*"*"+(line+1)*" " )

1.6打印斐波那契数列

x=0
y=1
count = 1

while y<100:
    print(y)
    tmp=y
    y=x+y
    x=tmp    

1.6.2打印斐波那契数列

count = 1   #次数
x=0
y=0

while count < 102:
    if count==1:
        y=1
    elif count==2:
        x=1
        y=1
    else:
        tmp=y
        y=x+y
        x=tmp    
    count=count+1
print(y)

 

  

  

posted @ 2018-03-21 16:59  亚洲哈登  阅读(231)  评论(0编辑  收藏  举报