Python编程快速上手-让繁琐工作自动化-第二章习题及其答案

Python编程快速上手-让繁琐工作自动化-第二章习题及其答案

1、布尔数据类型的两个值是什么?如何拼写?

答:True和False,使用大写的T和大写的F,其他字母是小写。

2、3个布尔操作符是什么?

答:and、or和not。

3、写出每个布尔操作符的真值表(也就是操作数的每种可能组合,以及操作的结果)

答:
and:
True and True  -> True
True and False -> False
False and True -> False
Fasle and False -> False

or:
True or True -> True
True or False -> True
False or True -> True
False or False -> False

not:
not True -> False
not False -> True

4、以下表达式求值的结果是什么?

4.1、( 5 > 4 ) and ( 3 == 5 )
4.2、not ( 5 > 4 )
4.3、( 5> 4 ) or ( 3 == 5 )
4.4、not (( 5 > 4 ) or ( 3 == 5 ))
4.5、( True and True ) and ( True == False )
4.6、( not False ) or ( not True )

答: 
4.1、
(5>4) and (3 == 5)
True  and False
     False    # 最终结果               
4.2not (5 > 4)
not True
   False     # 最终结果
4.3、
( 5> 4 ) or ( 3 == 5 )
  True   or   False
        True # 最终结果
4.4not (( 5 > 4 ) or ( 3 == 5 ))
not (True      or    False)
not     True 
   False  # 最终结果
4.5、
( True and True ) and ( True == False )
       True       and   False 
               False    # 最终结果
4.6、
( not False ) or ( not True )
    True      or  Fasle 
          True   # 最终结果

5、6个比较操作符是什么?

答:==、!=、<、>、<=和>=。

6、等于操作符和赋值操作符的区别是什么?

答:==是等于操作符,它比较两个值,求值为一个布尔值,而=是赋值操作符,将值保存在变量中。

7、解释什么是条件,可以在哪里使用条件?

答:条件是一个表达式,它用于控制流语句中,求值为一个布尔值。

8、识别这段代码中的3个语句块

spam = 0
if sapm == 10:
    print('eggs')
    if spam > 5:
        print('bacon')
    else:
        print('ham')
    print('spam')
print('spam')
答:3个语句块是if语句中的全部内容,以及print('bacon')和print('ham')这两行。
print('eggs')
if spam > 5:
    print('bacon')
else:
    print('ham')
print('spam')

9、编写代码,如果变量spam中存放1,就打印Hello,如果变量中存放2,就打印Howdy,如果变量中存放其它值,就打印Greetings

答:
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author: davie
spam = input("请输入1或者2->:")
if spam.isdigit():
    spam = int(spam)
    if spam == 1:
        print("Hello %s"%spam)
    elif spam == 2:
        print("Howdy %s"%spam)
    else:
        print("Greetings!")
else:
    print("您输入的不是数字1或者2")

10、如果程序陷在一个无限循环中,你可以按什么键?

答:按Ctrl-C来停止陷在无线循环中的程序。

11、break和continue之间的区别是什么?

答:
break:
终止整个循环:当循环或判断执行到break语句时,即使判断条件为True或者序列尚未完全被历遍,都会跳出循环或判断。

continue
跳出当次循环。当循环或判断执行到continue语句时,continue后的语句将不再执行,会跳出当次循环,继续执行循环中的下一次循环。

12、在for循环中,range(10)、range(0,10)和range(0,10,1)之间的区别是什么?

答:
效果一样,都是打印出0-9的数字。range(10)调用产生的范围是从0直到(但不包括)10,range(0,10)明确告诉循环从0开始,range(0,10,1)
明确告诉循环每次迭代变量加1。

13、编写一小段程序,利用for循环,打印出从1到10数字。然后利用while循环,编写一个等价的程序,打印出从1到10的数字

# 编写一小段程序,利用for循环,打印出从1到10数字。
for i in range(1,11):
    print('for loop : %s'%i)
# 利用while循环,编写一个程序,打印出从1到10的数字
count = 0
while count <10:
    count += 1
    print("while loop:%s"%count)

14、如何在名为spam的模块中,有一个名为bacon()的函数,那么在导入spam模块后,如何调用它?

答:
from spam import bacon
spam.bacon()

15、附加题:在网上查找round()和abs()函数,弄清楚它们的作用。在交互式环境中尝试使用它们

答: 
round():四舍五入
abs():求绝对值
>>> round(3.1414926,3)
3.141
>>> round(3.1415926,3)
3.142
>>> round('4.5678',1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type str doesn't define __round__ method
>>> 
>>> abs(-10)
10
>>> abs(8)
8
>>> abs('09')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
>>> 
这两个都必须接受数字类型的数据,否则会报错。

猜你喜欢

转载自www.cnblogs.com/bjx2020/p/8963310.html