(Python Programming (3rd Edition) Chapter 1 After-Class Answers

1.______ is a commonly used Python extension library management tool.

Answer: pip

2. The operator %______ (can, cannot) performs remainder operation on floating point numbers.

Answer: yes

3. A number 5______ (is, is not) a legal Python expression.

the answer is

4. In Python 3.x, the return value type of the input() function is always ______.

Answer: string

5. Brief instructions on how to choose the correct Python version.

Answer: slightly

6. Why is it said that Python adopts a value-based memory management model?

Answer:
Python uses a value-based memory management method. If you assign the same value to different variables in the same statement in the same program or in interactive mode, there is only one copy of this value in the memory, and multiple variables point to the same memory. address. In addition, variables in Python programs do not store values, but store value references. Iterable objects such as lists, tuples, dictionaries, and sets also contain references to elements. When calling a function, references to actual parameters are also passed to formal parameters.

7. Explain the difference between operators / and // in Python.

Answer:
The operator "/" corresponds to true division, and the result of the operation is a real number. The operator "I/" corresponds to integer division, and the operation result is an integer after "rounding down". For example, the result of 15/4 is 3.75, and the result of 15//4 is the largest integer smaller than 3.75 on the number line, which is 3. For another example, the result of -15/4 is -3.75, and the result of -15//4 is the largest integer smaller than -3.75 on the number line, which is -4.

8. What are the ways to import objects in modules in Python?

Answer:
There are three commonly used methods, namely
import module name/package name [as alias]
from module name/package name import object name/module name [as alias]
from module name import *
For example,

import foo
#Import a package or module named foo, you need to use foo as a prefix
to access its members
import foo.bar.baz
#Import module foo.bar.baz, you need to use foo.bar.baz
as a prefix to access its members
import foo.bar.baz as fbb
#Import foo.bar.baz as alias fbb, and then use fbb to access its members
from foo.bar import baz
#Import foo.bar.baz, and then access its members through baz
from foo import attr
#Import the member attr in the foo module, which can be accessed directly using attr

9. Explain the __name__ variable of Python script program and its function.

Answer: Every Python script has a " name__
" attribute when it is run . If the script is imported as a module, the value of its " name " attribute is automatically set to the module name; if the script is run independently, the value of its " name " attribute is automatically set to ' main '. When you use the import statement to import a Python program file, the code in it will be executed. If a piece of code does not want to be executed when importing the module, you can add a conditional judgment "if_name__ == 'main_'" in front of the code .

10. Write a program where the user inputs an integer with more than three digits and outputs a number with more than one hundred digits. For example, if the user inputs 1234, the program will output 12 (using integer arithmetic).

Answer:

num = int(input('Enter a positive integer with more than three digits:'))
print( num // 100)

Guess you like

Origin blog.csdn.net/R1se_/article/details/118113024