Python core programming Second Edition 437th page 14th chapter exercises-Python core programming answers-self-developed-

Source: Internet
Author: User

This is a self-made exercise and may be incorrect. You are welcome to discuss and discuss various optimization and reconstruction solutions.
Based on feedback or code review, the updated answers or related content of this article will be added to the comments of this blog.
We will try to ensure that the answer code for each question is complete, not just functions or classes. Open the Python 2.7 IDLE and copy the complete code to debug and run it.
Welcome to Balian's blog home. Http://www.cnblogs.com/balian

14-1.
Callable object. Specifies the callable object in Python. What is the difference between the exec statement and the built-in function eval.

[Answer]
There are three callable objects in Python:
Functions-including built-in functions (BIF), user-defined functions (udfs), and lambda expressions
Methods-including built-in methods (BIM) and user-defined methods (UDM)
Class-that is, class instance

The exec statement is a BIF. Exec obj indicates executing obj. obj can be a collection of a single Python statement or statement (Code object or string) or a file object (an opened Python script ). The exec statement only has the obj parameter. Refer to the 419th page of the book.

Eval () receives the string in the ARC and evaluates it as a Python expression. Eval () also has optional second and third parameters. Refer to the 418th page of the book.

14-2.
Input () and raw_input (). What is the difference between the built-in function input () and raw_input?
Here is an original question in the English version.

[Answer]
Input () is a built-in function (BIF). It is a combination of eval () and raw_input (), that is, eval (raw_input ()).
Like raw_input (), input () has an optional string parameter, which is a string prompt for the user.

Raw_input () always returns the user's input in the form of a string.
Input () evaluates the input as a Python expression and returns a Python object.

[Reference Code]

>>> b = input("Please input a formular: ... ")Please input a formular: ... 1 + 2>>> b3>>> a = raw_input("Please input a formular: ..." )Please input a formular: ...1 + 2>>> a'1 + 2'>>>

14-3.

Execution environment. Create a Python script to run other Python scripts.

[Answer]

The Code is as follows:

>>> exec '''... print 'Hello World!'... '''Hello World!>>>

14-4.

OS. system (). Select a familiar system command. This command does not need to be input, output to the screen, or output anything during task execution. Call OS. system () to run the program. Additional question: Port your solution to subprocess. call ().

[Answer]

Select the system command dir> test.txt. Dir's result is stored in the test.txt file. Therefore, this command is neither input nor output to the screen.

The Code is as follows:

>>> import os>>> os.system("dir > test.txt")0>>>

The additional question code is as follows:

>>> import subprocess>>> subprocess.call(('dir > test.txt'), shell = True)0>>>

14-5.

Commands. getoutput (). Use commands. getoutput () to solve the preceding problem.

[Answer]

Commands. getoutput () can only run on unix platforms. Reference http://bugs.python.org/issue15073

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.