Python程序语言设计基础(第二版)思考与练习

欢迎来到我的友链小屋

 

# Python程序语言设计基础(第二版)思考与练习 --- --- ## 3.5 思考各操作符的优先级,计算下列表达式 一、30-3\*\*2+8//3\*\*2*10 答:
>>> 30-3**2+8//3**2*10 21 # 幂运算第一,然后算地板除和乘法,最后加减 --- 二、3*4**2/8%5
>>> 3*4**2/8%5 1.0 # 先算幂,然后*,/,%依次运算 --- 三、3\*\*2\*\*3
>>> 3**2**3 6561 # 嵌套幂运算,优先算高级幂 --- 四、(2.5+1.25j)*4j/2
>>> (2.5+1.25j)*4j/2 (-2.5+5j) ''' 先看下面运算拆分 4j/2 = 2j 2.5*2j = 5j 1.25j*2j = -2.5+0j 所以 (2.5+1.25j)*4j/2 =2.5*2j + 1.25j*2j =5j + (-2.5+0j) =5j - 2.5 即: -2.5+5j ''' --- --- ## 3.6 请将下列数学表达式用python程序写出来,并运算结果 一、 ![avatar][s1]
>>> x = (2**4+7-3*4)/7 --- 二、![avatar][s2]
>>> x = (1+3**2)*(16%7)/7 --- --- ## 3.7 假设x=1,x*=3+5**2的运算结果是什么?
>>> x = 1 >>> x*=3+5**2 即 >>> x=(3+5**2)*x 28 --- --- ## 3.12 一年有365天,初始水平值为1.0,每工作一天水平增加N,不工作时水平不下降,一周连续工作4天,请编写程序运算结果并填写下表
def runx(): day1=int(input('请输入天数:')) initial = 1.0 initial1 = 1.0 ica = float(input("请输入每天增加量:")) for i in range(day1): if i%7 in [1,2,3,4]: initial = initial*(initial1 + ica) print('{:.2f}'.format(initial)) runx() |增加值|0.001|0.002|0.003|0.004|0.005|0.006|0.007| |----|----|----|----|----|----|----|----|----|----|----| |结果|1.23|1.52|1.86|2.29|2.82|3.47|4.27| |增加值|0.008|0.009|0.010| |----|----|----|----|----|----|----|----|----|----|----| |结果|5.25|6.45|7.92| --- --- ## 3.13 一年有365天,初始水平值为1.0,每工作一天水平增加N,不工作时水平不下降,一周连续工作5天,请编写程序运算结果并填写下表
def runx(): day1=int(input('请输入天数:')) initial = 1.0 initial1 = 1.0 ica = float(input("请输入每天增加量:")) for i in range(day1): if i%7 in [1,2,3,4,5]: initial = initial*(initial1 + ica) print('{:.2f}'.format(initial)) runx() |增加值|0.001|0.002|0.003|0.004|0.005|0.006|0.007| |----|----|----|----|----|----|----|----|----|----|----| |结果|1.30|1.68|2.18|2.82|3.66|4.74|6.13| |增加值|0.008|0.009|0.010| |----|----|----|----|----|----|----|----|----|----|----| |结果|7.94|10.27|13.29| --- --- ## 3.14 一年有365天,初始水平值为1.0,每工作一天水平增加N,不工作时水平不下降,一周连续工作6天,请编写程序运算结果并填写下表
def runx(): day1=int(input('请输入天数:')) initial = 1.0 initial1 = 1.0 ica = float(input("请输入每天增加量:")) for i in range(day1): if i%7 in [1,2,3,4,5,6]: initial = initial*(initial1 + ica) print('{:.2f}'.format(initial)) runx() |增加值|0.001|0.002|0.003|0.004|0.005|0.006|0.007| |----|----|----|----|----|----|----|----|----|----|----| |结果|1.37|1.87|2.55|3.47|4.74|6.47|8.81| |增加值|0.008|0.009|0.010| |----|----|----|----|----|----|----|----|----|----|----| |结果|12.01|16.37|22.30| --- ## 3.15 一年有365天,初始水平值为1.0,以每个月30天计算,在每个月月初连续10天里,每工作一天水平增加N,该月其他时间工作与否都不增加水平值。请运算结果并填写下表。
while 1==1: basics = 1.0 days = 360 increase = float(input("请输入每天的增加值:")) for i in range(days): if i%30 in [1,2,3,4,5,6,7,8,9,10]: basics *= 1 + increase print("{:.2f}".format(basics)) |增加值|0.001|0.002|0.003|0.004|0.005|0.006|0.007| |---|---|---|---|---|---|---|---| |结果|1.13|1.27|1.43|1.61|1.82|2.05|2.31| |增加值|0.001|0.002|0.003| |-|-|-|-| |结果|2.60|2.93|3.30| --- ## 3.16 s="hello", t="world", s+=t, 则 s、s[-1]、s[2:8]、s[::3]、s[-2::-1]分别是多少? |问|s|s[-1]|s[2:8]|s[::3]|s[-2::-1]| |---|---|---|---|---|---| |答|"helloworld"|"d"|"lowor"|"hlod"|"lrowolleh"| > 解释:
>   s[::3]:表示从第一个字母开始,查找间隔第三个的字母
>   s[-2::-1]:反向查找,表示从倒数第二个字母开始查找到顺数第一个字母,并且反向输出 --- ## 3.17 判断题:Python中"4"+"5"的结果为"9": > 错,这是字符串拼接,不是数字计算 --- ## 3.18 采用微实例 3.1 的设计思路还能完成哪些常用计算需求? > 答: --- ## 3.19 s="Python String",写出下列操作的输出结果: > s.upper()、s.lower()、s.find('i')
> s.replace('ing','qni')、s.split(' ') 解: |s.upper()|s.lower()|s.find('i')| |---|---|---| |'PYTHON STRING'|'python string'|10| |s.replace('ing','gni')|s.split(' ')| |---|---| |'Python Strgni'|['Python','String']| 解释: > s.upper() 返回字符串str的副本,全部字符大写 > s.lower() 返回字符串str的副本,全部字符小写 > s.find('i') 查找字符串str中元素'i'的位置 > s.replace('ing','gni') 将字符串str中的元素'ing'替换为'gni' > s.split(' ') 以空格为分隔符,返回分割后的字符串列表 --- ## 3.20 下列表达式错误的是(): |A|'abcd'<'ad'| |B|'abc'<'abcd'>| |-|-|-|-|-| |C|''<'a'||D|'Helo'>'helo'| |-|-|-|-|-| 答: > A选项: 先比第0个元素,a和a比一样大;再比第一个元素,b比d小.那么'abcd'比'ad'小.结果是TRUE. > B选项:前三个元素都相等,但'abcd'有第三个元素,而'abc'没有.所以'abc'<'abcd'. > C选项:这个错了,既不是对,又不是错. > D选项:先比第0个元素,'A'要比'a'小.所以'ABCD'应该小于'abcd'. 结果为False. ------ ## 3.21 请思考并描述下面Python语句的输出结果 > print('{:>15s}:{:<8.2f}'.format('Length',23.87501)) >>> print('{:>15s}:{:<8.2f}'.format('Length',23.87501)) ' Length:23.88 ' --- ## 3.22 格式化输出 389 的二进制、八进制、十进制、十六进制的表达形式,以及对应的Unicode字符。 >>> '{:*^20b}'.format(389) #输出二进制 '*****110000101*****' >>> '{:*<20o}'.format(389) #输出八进制 '605*****************' >>> '{:*>20d}'.format(389) #输出十进制 '*****************389' >>> '{:*^20X}'.format(389) #输出大写十六进制 '********185*********' >>> '{:*^20c}'.format(389) #输出Unicode字符 ‘*********b**********’ --- ## 3.23 格式化输出 0.002178 对应的科学表示法形式,保留4位有效位的标准浮点形式及百分比形式
>>> '{:E}'.format(0.002178) '2.178000E-03' >>> '{:.4f}'.format(0.002178) '0.0022' >>> '{:.2%}'.format(0.002178) #返回百分比形式,并保留小数点后两位 '0.22%' --- ## 3.24 进度条反映了软件的执行速度,请思考并给出至少3种提高软件执行速度的方法。 > 硬件:使用性能更强的处理器;或使用固态硬盘等设备提高计算机整体速度。 > > 软件自身:在编写时进行代码优化。编译时采用静态编译(Python 不存在编译的问题)。 > > 运行平台:类似Linux环境下执行,把硬件的算力提升到更大 --- ## 3.25 str.center()方法的功能是什么? > 将字符串居中 --- ## 3.26 如果将\r 放在print()中字符串的其他部分,会产生什么效果? print('\r你好Python') print('\r你好世界') >>>>>>>>>>>>>>> '你好Python' '你好世界' print('你好啊\rPython') print('你好啊\r世界') >>>>>>>>>>>>>>> 'Python' '世界啊' > 将转义符\r 放到字符串尾部是对程序没有影响; > > 然而如果放到其他地方,会导致\r 前面的所有字符不显示,因为每次输出到\r,指针又退回了行首,如果后面的内容足够长,后面的内容会将其覆盖。 --- [s2]:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAAgCAYAAAD9qabkAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAo9SURBVHhe7Vy/axtJFN4/Y1uBixOkiDptKYGLLKQ4QYoTpAjiikNccQgXh0hjxBVGpAjmCiNcHChFQC4CThFQGoNSGHxFQCkCcpFChYstVGxxxbv35oc0K83u7Mqy9Ws+WGztandnZ9773vfezMoBCwuLvYUlAAuLPYYlAAuLPYYlAAuLPYYlAAuLPYYlAAuLPYYlgI3GCC6PKlA+9CDn5sE/7kMgjlhYrAKWADYYozMfvOMBhPT/uyq4jgP++YgftLBYASwBbCxuoP0UHR4JgEf9AbRcB5ynbTxiYbEa7DYB3PWh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