Python基础|Python基础 - 练习1

练习 1.斐波那契数列

n = 8 n_1 = n_2 = result = 1 for x in range(3,n+1): result = n_1 + n_2 n_2 = n_1 n_1 = result print(result)

2.求100~200中间的素数
print('___________________') sum1 = 0 for x in range(100,201): count = 0 for y in range(2,x): if x % y == 0: count += 1 break if count == 0: print(x) sum1 += 1 print('个数',sum1)

4.
fz = 2 fm = 1 for _ in range(20): # print('%d/%d'%(fz,fm)) fz,fm = fz+fm,fz#fz,fm = 3,2fz = 3, fm = 2 print('%d/%d'%(fz,fm))

5.
num = 12343543 count = 0 while num != 0: count += 1 print(num%10) num //=10 print(count)

day6 - 作业 1. age = 30
if 0 print('幼儿')
elif 7 print('儿童')
elif 13 print('青年')
elif 25 print('中年')
else:
print('老年')
结果:

Python基础|Python基础 - 练习1
文章图片
1.JPG 2.
print('______________') num = 1 for x in range(1,6): num *= x print(num)

结果:

Python基础|Python基础 - 练习1
文章图片
2.JPG 3.
print('3———————————————————————') num = 1 sum1 = 0 for x in range(1,21): num *= x sum1 += num print(sum1)

结果

Python基础|Python基础 - 练习1
文章图片
3.JPG 4.
num = 1 sum1 = 0 for x in range(1,21): num *= x sum1 += 1/num print(sum1)

结果:

Python基础|Python基础 - 练习1
文章图片
4.JPG 5.
num = input('请输入一个大于0的数字(输入0结束):') num = int(num) sum1 = 0 while num != 0: sum1 += num num = input('请输入一个大于0的数字(输入0结束):') num = int(num) print('输入数字的和为:',sum1)

结果

Python基础|Python基础 - 练习1
文章图片
5.JPG 6.
a = int(input('请输入a的值')) n = int(input('请输入次数')) m = 0 num = 0 sum1 = 0 sum2 = 0 while m < n: while m < n: num = a*10**m sum1 += num m += 1 print(sum1) sum2 += sum1 m += 1 print('和:',sum2)

结果:

Python基础|Python基础 - 练习1
文章图片
6.JPG
Python基础|Python基础 - 练习1
文章图片
7.JPG 7.
a = int(input('输入第一个整数:')) b = int(input('输入第二个整数:')) c = int(input('输入第三个整数:')) if a > b: t = a a = b b = t if b > c: t = b b = c c = t if a > b: t = a a = b b = t print(a,b,c)

结果:

Python基础|Python基础 - 练习1
文章图片
8.JPG 8. a.
n = 5 while n > 0: # print('*') m = n while m > 0: print('*',end = '') m -= 1 n -= 1 print('\n')

结果:

Python基础|Python基础 - 练习1
文章图片
9.JPG
n = 4 while n > 0: # print('*') m = n while m > 0: print('*',end = '') m -= 1 n -= 1 print('\n')

结果

Python基础|Python基础 - 练习1
文章图片
10.JPG
【Python基础|Python基础 - 练习1】b.

    推荐阅读