Get started with Python programming-automate tedious tasks-chapter two exercises and their answers

Source: Internet
Author: User
Tags for in range stdin

Python programming Quick Start-making tedious work automated-chapter two exercises and their answers 1, what are the two values of the Boolean data type? How do I spell it?

A: True and false, use uppercase T and uppercase F, and the other letters are lowercase.

What are the 2, 3 Boolean operators?

Answer: And, or, and not.

3. Write the truth table for each boolean operator (i.e., each possible combination of operands, and the result of the operation)

For:
And:
True and True
True and False
False and True
Fasle and 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 the following expression evaluation?

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 andfalse False#Final Result4.2, not(5 > 4) notTrue False#Final Result4.3, (5> 4)or(3 = = 5) TrueorFalse True#Final Result4.4, not((5 > 4)or(3 = = 5 )) not(TrueorFalse) notTrue False#Final Result4.5, (True andTrue) and(True = =False) True andfalse False#Final Result4.6, ( notFalse)or( notTrue) TrueorFasle True#Final Result
5. What are the 6 comparison operators?

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

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

A: = = equals the operator, which compares two values, evaluates to a Boolean value, and = is an assignment operator, saving the value in a variable.

7, explain what is the condition, where can I use the conditions?

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

8. Identify the 3 block of statements in this code
Spam =0ifSAPM = = 10:    Print('eggs')    ifSpam > 5:        Print('Bacon')    Else:        Print('Ham')    Print('spam')Print('spam'A : 3 statement blocks are the entire contents of the If statement, and print ('Bacon') and print ('Ham') These two lines. Print('eggs')ifSpam > 5:    Print('Bacon')Else:    Print('Ham')Print('spam')
9, write code, if the variable spam store 1, print Hello, if the variable is stored in 2, print howdy, if the variable holds other values, print greetings
Answer:#!/usr/bin/env Python3#-*-coding:utf-8-*-#Author:davieSpam = input ("Please enter 1 or 2->:")ifspam.isdigit (): Spam=int (spam)ifSpam = = 1:        Print("Hello%s"%spam)elifSpam = = 2:        Print("Howdy%s"%spam)Else:        Print("greetings!")Else:    Print("you are not entering the number 1 or 2")
10. If the program is stuck in an infinite loop, what key can you press?

Answer: Press CTRL-C to stop the program trapped in the wireless loop.

11. What is the difference between break and continue?

For:
Break
Terminates the entire loop: when a loop or judgment is executed into a break statement, it jumps out of the loop or judgment even if the condition is true or if the sequence has not been completely calendar.

Continue
Jump out of the secondary loop. When the loop or judgment executes to the continue statement, the statement after continue is no longer executed, it jumps out of the secondary loop and resumes the next loop in the loop.

12. What is the difference between range (10), Range (0,10), and Range (0,10,1) in the For loop?

For:
As a result, 0-9 of the numbers are printed out. The range (10) call produces a scope from 0 until (but not including) 10,range (0,10) explicitly tells the loop to start at 0, range (0,10,1)
Explicitly tells the loop each iteration variable plus 1.

13, write a small program, using for loop, print out from 1 to 10 numbers. Then, using the while loop, write an equivalent program to print a number from 1 to 10.
# Write a short program that uses a for loop to print out numbers from 1 to 10.   for in range (1,11):    print('forloop:%s'% i) # using the While loop, write a program that prints a number from 1 to 10 count = 0 while Count <10:    + = 1    Print("whileloop:%s"%count)
14, how to in the module named Spam, there is a function named Bacon (), then after importing the spam module, how to call it?

For:
From spam import bacon
Spam.bacon ()

15. Additional questions: Find the Round () and ABS () functions on the Internet to get a clear idea of their role. Try to use them in an interactive environment
Answer: Round (): Rounding abs (): Absolute value>>> Round (3.1414926,3)3.141>>> Round (3.1415926,3)3.142>>> Round ('4.5678', 1) Traceback (most recent): File"<stdin>", Line 1,inch<module>Typeerror:type Str doesn'T define __round__ method>>> >>> ABS (-10)10>>> ABS (8)8>>> ABS (' the') Traceback (most recent): File"<stdin>", Line 1,inch<module>Typeerror:bad operand type forABS ():'Str'>>>Both must accept numeric data, or they will get an error. 

Get started with Python programming-automate tedious tasks-chapter two exercises and their answers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.