Practices 1:
0 1 2 3 4 5 6 7 8 |
$ cat 1.py #!/usr/bin/env python3 # Author: Byrd # Created Time: Thu 10 May 2018 11:20:39 AM CST #!/usr/bin/env python3 sum = 0 for i in range(1, 11): sum += 1.0 / i print("{:3d} {:6.4f}".format(i , sum)) |
说明:
for循环,i取值范围为1-10,不包括11;
sum += 1.0 / i表示 sum = sum + 1.0 / i;
3d表示如果位数不足3位,则用空格补齐;
6.4f表示如果小数后面位数不足4位,则用0补齐,如果整个小数不足 6 位(小数点占 1 位), 整数前用空格补足;
format格式化,i采用3d的格式.而sum采用6.4f的格式; SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
演示:
0 1 2 3 4 5 6 |
$ cat 1.py #!/usr/bin/env python3 # Author: Byrd # Created Time: Thu 10 May 2018 11:20:39 AM CST print("{:3d} {:8.4f}".format(13 , 1.33)) $ python3 1.py 13 1.3300 |
Pracitces 2:SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
0123456789101112131415 $ cat 2.py#!/usr/bin/env python3# Author: Byrd# Created Time: Thu 10 May 2018 11:49:30 AM CSTimport matha = int(input("Enter value of a: "))b = int(input("Enter value of b: "))c = int(input("Enter value of c: "))d = b * b - 4 * a * cif d < 0:print("ROOTS are imaginary")else:root1 = (-b + math.sqrt(d)) / (2 * a)root2 = (-b - math.sqrt(d)) / (2 * a)print("Root 1 = ", root1)print("Root 2 = ", root2)
说明:SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/导入math模块;
a、b、c等待用户输入,且转换为整数;
d的值为 运算结果;(1 * 1 - 4 * 5 * 0) = 1
如果d的值小于0,则打印ROOTS are imaginary;
否则
root1= (-1 + 1.0) / (2 * 5)
root2= (-1 - 1.0) / (2 * 5) = -2 / 10 = 0.2 SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/演示:
SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
012345 $ python3 2.pyEnter value of a: 5Enter value of b: 1Enter value of c: 0Root 1 = 0.0Root 2 = -0.2
Practices 3: SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
012345678910111213 $ cat 3.py#!/usr/bin/env python3# Author: Byrd# Created Time: Thu 10 May 2018 01:55:34 PM CSTbasic_salary = 1500bonus_rate = 200commision_rate = 0.02numberofcamera = int(input("Enter the number of inputs sold: "))price = float(input("Enter the total prices: "))bonus = (bonus_rate * numberofcamera)commision = (commision_rate * numberofcamera * price)print("Bonus = {:6.2f}".format(bonus))print("Commision = {:6.2f}".format(commision))print("Gross salary = {:6.2f}".format(basic_salary + bonus + commision))
说明:SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/numberofcamera = 3
price = 2
bonus = 200 * 3
commision = 0.02 * 3 * 2
打印bonus,保留2位2小数,整个字符长度为6位
同上 SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/演示:
SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
012345 $ python3 3.pyEnter the number of inputs sold: 3Enter the total prices: 2Bonus = 600.00Commision = 0.12Gross salary = 2100.12
Practices 4:SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
01234567 #!/usr/bin/env python3# Author: Byrd# Created Time: Thu 10 May 2018 03:22:13 PM CSTimport mathR = int(input("Please input the number: "))PI = math.picirclearea = PI * R * Rprint('{:4.10f}'.format(circlearea))说明:SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
import math #导入模块
R = 3
PI = 3.141592653589793 #π
circlearea = 3.141592653589793 * 3 * 3 #圆的面积公式 πR²
{:4.10f} 表示整个长度为4位以上,如果长度不够整数前用空格补齐(包括小数点),取小数点后10位 SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/演示:
SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
012 $ python3 4.pyPlease input the number: 328.2743338823
Practices 5:SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
0123456 #!/usr/bin/env python3# Author: Byrd# Created Time: Thu 10 May 2018 04:34:53 PM CSTa, b = 0, 1while b < 100:print(b)a, b = b, a + b
说明:SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/a=0 b=1
当b小于100的时候,循环进行
第一次循环 a = 0 b = 1 且打印b
赋值 a = b = 1
第二次循环 a = 1 b = 1
第N次循环 a = 34 b = 55
第N+1次循环 a = 89 b = 144 不符合条件,跳出循环SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
演示:
SourceByrd's Weblog-https://note.t4x.org/basic/python-exercise-test/
01234567891011 $ python3 5.py1123581321345589
Practices 6:
01234567 $ cat 6.py#!/usr/bin/env python3# Author: Byrd# Created Time: Thu 10 May 2018 04:34:53 PM CSTa, b = 0, 1while b < 100:print(b, end =' ')a, b = b, a + b
说明:print(b, end =' ') 将默认换行符换成空格
演示:
01 $ python3 6.py1 1 2 3 5 8 13 21 34 55 89 [www@Practice code]$
Practices 7:
012345678 $ cat 7.py#!/usr/bin/env python3# Author: Byrd# Created Time: Thu 10 May 2018 04:34:53 PM CSTa, b = 0, 1while b < 100:print(b, end =' ')a, b = b, a + bprint()
说明:演示:
01 $ python3 7.py1 1 2 3 5 8 13 21 34 55 89
Pracitces 8:
012345678910111213 $ cat 8.py#!/usr/bin/env python3# Author: Byrd# Created Time: Thu 17 May 2018 03:26:43 PM CSTx = float(input("Enter the value of x: "))n = term = num = 1result = 1.0while n <= 100:term *= x / nresult += termn += 1if term < 0.0001:breakprint("No of Times= {} and Sum= {}".format(n, result))
说明:x = 2.0
n = term = num = 1
n小于或者等于100 循环成立
term = term * x / n = 1 * 2.0 = 2.0
result = result + term = 1.0 + 2.0 =3.0
n = n + 1演示:
012 $ python3 8.pyEnter the value of x: 2No of Times= 12 and Sum= 7.389046015712681