+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2020-07(1)

python练习题

发布于2020-07-19 18:59     阅读(860)     评论(0)     点赞(0)     收藏(0)


0

1

2

3

4

5

6

一、打印一个边长为n的正方形

for i in range(5):

print(6*"*")

==============================

for i in range(1,6):

if i == 1 or i == 5:

print(5*'*')

else:

print('*' + " "*3 + '*')

==============================

二、求100以内的奇数和

sum = 0 #这里是累加器,不是计数器

for i in range(1,100,2):

sum += i

else:

print(sum)

===============================

sum = 0 #这里是累加器,不是计数器

for i in range(1,100,2):

sum += i

print(sum)

===============================

sum=0

for i in range(1,100):

if i % 2 !=0:

sum = sum + i

print(sum)

===============================

sum = 0

for i in range(1,100):

if i % 2 == 0:

continue

i += 2

sum += i

print(sum)


三、判断学生成绩,成绩等级A~E;其中90分以上为A,80到89分为B,70到79分为C,60到69分为D,60分一下为E

num = int(input())

if num >= 90:

print("A")

elif 90 > num >= 80:

print("B")

elif 80 > num >= 70:

print("C")

elif 70 > num >= 60:

print("D")

else:

print("E")

=================================

四、求1~5的阶乘之和

5的阶乘是5*4*3*2*1

nums = 1 #

sum = 0

for n in range(1,6):

nums *= n #也可以写成nums = nums * n

sum += nums

print(sum)

=================================

n = int(input())

jc = 1

sum = 0

for i in range(1,n+1):

for j in range(1,i+1):

jc *= j

sum += jc

jc = 1

print(sum)

==================================

五、给一个数,判断它是否是一个质数(质数:一个大于1的自然数只能被1和它本身整数)

num = int(input())

for i in range(2,num):

if num % i == 0:

print("no")

break

else:

print("is")

===================

num = int(input())

i = 2

while num > 2:

if num % i == 0:

print("No")

break

i += 1

else:

print("Yes")

===================

练习题:

1、打印九九乘法表

for i in range(1,10):

for j in range(1,i+1):

print(i,'x',j,'=',j*i,end=' ')

print()

==========================

for i in range(1,10):

for j in range(1,i+1):

product = j*i

if j > 1 and product < 10:

product = str(product) + ' '

print(j,"x",i,"=",product,end=" ")

print()

=====================================

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

#print(0)

print(1,end=' ')

a = 0

b = 1

while True:

c = a + b

if c > 100:

break

a = b

b = c

print(c,end=' ')

==========================

n,a,b = 0,0,1

while n < 101:

a,b = b,a+b

n += 1

print(a,end=" ")


3、求斐波那契数列的101项

第二:

nters = int(input("输入第几项:"))

n1 = 0

n2 = 1

count = 2

if nters <= 0:

print("请输入一个正整数。")

elif nters == 1:

print("斐波那契数列:")

print(n1)

else:

print("斐波那契列:")

print(n1,'',n2)

while count <= nters:

nth = n1 + n2

print(nth)

n1 = n2

n2 = nth

count += 1

==================================

最次:

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

=========================

最优:

import datetime

nters = int(input("输入第几项:"))

n1 = 1

n2 = 1

count = 3

start = datetime.datetime.now()

if nters <= 0:

print("输入正整数")

elif nters == 1:

print(n1)

elif nters == 2:

print(n2)

else:

print("斐波拉契数列:",end="")

# print(n1,end='\n')

# print(n2,end='\n')

for count in range(3,nters+1):

n = n1 + n2

n1,n2 = n2,n

count += 1

print(n)

print(datetime.datetime.now()-start)

4、求10以内的所有素数。

print(1,"Y")

for i in range(2,100):

for j in range(2,i):

if i % j == 0:

break

else:

print(i,"Y")


5、打印99乘法表的倒序。

for i in range(1,10):

line = ''

for j in range(1,10):

if i > j:

line = '{} {} {:<4}'.format(' ',' ',' ')

else:

line = '{}x{}={:<4}'.format(i,j,i*j)

print(line,end= ' ')

print()

===============================

for i in range(1,10):

print(''*7*(i-1),end='')

for j in range(i,10):

product = i*j

if product < 10:

end = ' '

else:

end = ' '

print(str(i)+'*'+str(j)+"="+str(i*j),end = end)

print()

================================

for i in range(1,10): #正序

line = ''

for j in range(1,10):

if i < j:

line = '{} {} {:<4}'.format(' ',' ',' ')

else:

line = '{}x{}={:<4}'.format(i,j,i*j)

print(line,end= ' ')

print()

================================

6、打印菱形

for i in range(-3,4):

if i < 0:

prespace = -i

else:

prespace = i

print(' '*prespace + '*'*(7-prespace*2))

==============================

for i in range(1,6):

if i == 1 or i == 5:

print('','','*','','')

elif i == 2 or i == 4:

print("","*"*3,"")

else:

print("*"*5)

=============================


行号

前置空格

*号数

后置空格

1(-3)

3

2

3

2(-2)

2

3

2

3(-1)

1

5

1

4(0)

0

7

0

5(1)

1

5

1

6(2)

2

3

2

7(3)

3

1

3






7、打印闪电标记

for i in range(-3,4):

if i < 0:

print(' '*(-i) + '*'*(4+i))

elif i > 0:

print(' '*3 + '*'*(4-i))

else:

print('*'*7)

==========================


行号

前空格数

*号数

后空格数

1(-3)

3

1

3

2(-2)

2

2

3

3(-1)

1

3

3

4(0)

0

7

0

5(1)

3

3

1

6(2)

3

2

2

7(3)

3

1

3





8、打印对顶三角形



行号

前空格数

*号数

后空格数

1(-3)

0

7

0

2(-2)

1

5

1

3(-1)

2

3

2

4(0)

3

1

3

5(1)

2

3

2

6(2)

1

5

1

7(3)

0

7

0





*******

*****

***

*

***

*****

*******

for i in range(-3,4):

if i < 0:

prespace = -i

else:

prespace = i

print(' '*(7//2 - prespace) + '*'*(2*prespace + 1))


9、打印倒等边三角形



行号

前空格数

*号数

后空格数

1

3

1

3

2

2

3

2

3

1

5

1

4

0

7

0










for i in range(-3,1):

if i <= 0: #如果这里只小于0,那么打印完之后最后一行是三个*号。

# prespace = -i

# else:

prespace = i

print(' '*(7//2 + prespace) + '*'*(2*(-prespace) + 1))


执行流程:

i = -3

7 // 2 + -3 == 0 *x(2*(3)+1) == 7

i = -2

7 // 2 + -2 == 1 *x(2*(2)+1) == 5

i = -1

7 // 2 + -1 == 2 *x(2*(1)+1) == 3

i = 0

7 // 2 + 0 == 3 *x(2*(0)+1) == 1












0

1

2

3

4

5

6

7



所属网站分类: 资源下载 > python脚本

作者:硅谷001

链接: https://www.pythonheidong.com/blog/article/450410/d5166a2ea5f5a63f72da/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)