Get started with Python programming quickly - automate tedious work - Chapter 2 exercises and answers

Get started with Python programming quickly - automate tedious work - Chapter 2 exercises and answers

1. What are the two values ​​of the boolean data type? How to spell it?

Answer: True and False, use uppercase T and uppercase F, other letters are lowercase.

2. What are the 3 Boolean operators?

答:and、or和not。

3. Write out the truth table of each Boolean operator (that is, every possible combination of operands, and the result of the operation)

答:
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. What is the result of evaluating the following expression?

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 )

Answer: 
 4.1 ,
(5>4) and (3 == 5)
True  and False
     False #final     result 4.2                
, not
 ( 5 > 4 )
 not True
   False      #Final result 
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   #final result 
4.5 ,
( True and True ) and ( True == False )
       True       and   False 
               False     #Final result 
4.6 ,
( not False ) or ( not True )
    True      or  Fasle 
          True    #final result

5. What are the 6 comparison operators?

Answer: ==, !=, <, >, <= and >=.

6. What is the difference between the equal operator and the assignment operator?

Answer: == is the equality operator which compares two values ​​and evaluates to a boolean value whereas = is the assignment operator which saves the value in a variable.

7. Explain what conditions are and where can conditions be used?

Answer: A condition is an expression that is used in a control flow statement and evaluates to a boolean value.

8. Identify the 3 statement blocks in this code

spam = 0
if sapm == 10:
    print('eggs')
    if spam > 5:
        print('bacon')
    else:
        print('ham')
    print('spam')
print('spam')
Answer: The 3 statement blocks are all in the if statement, and the lines print( ' bacon ' ) and print( ' ham ' ).
print ( ' eggs ' )
 if spam > 5 :
     print ( ' bacon ' )
 else :
     print ( ' ham ' )
 print ( ' spam ' )

9. Write the code, if the variable spam stores 1, print Hello, if the variable stores 2, print Howdy, if the variable stores other values, print Greetings

Answer:
 # !/usr/bin/env python3 
# -*- coding:utf-8 -*- 
# Author: davie 
spam = input( " Please enter 1 or 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 ( " You did not enter the number 1 or 2 " )

10. What key can you press if the program is stuck in an infinite loop?

A: Press Ctrl-C to stop a program stuck in a wireless loop.

11. What is the difference between break and continue?

Answer:
break:
Terminate the entire loop: When the loop or judgment is executed to the break statement, even if the judgment condition is True or the sequence has not been completely traversed, it will jump out of the loop or judgment.

continue
to jump out of the current loop. When the loop or judgment is executed to the continue statement, the statement after the continue will no longer be executed, it will jump out of the current loop, and continue to execute the next loop in the loop.

12. In a for loop, what is the difference between range(10), range(0,10) and range(0,10,1)?

Answer:
The effect is the same, it prints the numbers 0-9. The range(10) call produces a range from 0 up to (but not including) 10, range(0,10) explicitly tells the loop to start at 0, and range(0,10,1)
explicitly tells the loop to increment the variable by 1 each iteration.

13. Write a small program that uses a for loop to print out numbers from 1 to 10. Then use the while loop to write an equivalent program that prints out numbers from 1 to 10

#Write a small program that prints out numbers from 1 to 10 using a for loop. 
for i in range(1,11 ):
     print ( ' for loop : %s ' % i)
 #Using the while loop, write a program that prints out numbers from 1 to 10 
count = 0
 while count <10 :
    count += 1
    print("while loop:%s"%count)

14. How to have a function named bacon() in the module named spam, how to call it after importing the spam module?

答:
from spam import bacon
spam.bacon()

15. Additional question: Look up the round() and abs() functions on the Internet and figure out what they do. Try them in an interactive environment

answer:
round(): round up
abs(): find the absolute value
>>> 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 ' 
>>>  
Both must accept numeric data, otherwise an error will be reported.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970642&siteId=291194637