Python core programming version 2, 75th page, Chapter 4 exercises

Source: Internet
Author: User

4-1.
Python object. What are the three attributes related to all Python objects? Please briefly describe it.
[Answer]
All Python objects have three features: identity, type, and value.
Identity: each object has a unique identity. The identity of any object can be obtained using the built-in function id. This value can be considered as the memory address of the object.
Type: the object type determines what type of value the object can save, what operations it can perform, and what rules it follows. You can use the built-in function type () to view the object type. In Python, the type is also an object.
Value: The data item represented by the object.

4-2.
Type. What does immutable mean? Which types of Python are changeable and which ones are not?
[Answer] These answers are found online.
If an object supports the update operation, its value can be changed. Otherwise, its value is read-only. Whether the value of an object can change the mutablility of an object ).
Unchangeable means that a new object (id) will be generated after the change, and the opposite can be changed.
Mutable types include list and dict.
Immutable types include str, int, and tuple.

4-3.
Type. Which Python types are accessed in order, and what are their differences from the ing types?
[Answer]
Data types can be classified based on the method of accessing the stored data. There are three access methods: direct access, sequence, and ing.
All numeric types are accessed by direct access.
Strings, lists, and tuples are accessed in sequence. This means that the element is accessed in the order of index from 0.
Dictionary is a ing access. For the elements in the dictionary, their indexes do not use the sequential numeric offset values. Their elements are stored unordered and accessed by a unique key.
Currently, dict is the only ing type.

4-4.
Type (). What is built-in function type? What is the object returned by type?
[Answer] These answers are found online.
The built-in function type () returns the Data type of any Python object, not limited to the standard type. The possible data types are listed in the types module. This is useful for helper functions that process multiple data types.

[Reference] recommended a Website: Python Research (Dive Into Python)
Http://woodpecker.org.cn/diveintopython/index.html

4-5.
Str () and repr (). What is the difference between the built-in str () and repr () functions? Which is equivalent to the inverse quotation mark ('') operator?
[Answer]
The built-in functions str (), repr (), and quotation mark operator ('') can easily obtain the object content, type, numeric attributes, and other information in string mode. The str () function makes the string readable, while the string obtained by the repr () function can be used to obtain the object again. Generally, obj = eval (repr (obj )) this equation is true. These two functions take an object as its parameter and return an appropriate string.
Repr () outputs are friendly to Python, while str () outputs are friendly to users.
''= Repr (), but the unquoted ('') operator is not encouraged to continue using it.
Str () is used to generate a readable string representation of an object, and its return results cannot be used for eval () evaluate.

4-6.
Objects are equal. What is the difference between type (a) = type (B) and type (a) is type (B? Why is the latter selected? What is the relationship between the isinstance () function and this function?
[Answer] These answers are found online.
"=" Compares values and is compares objects. Because each object can only have one type of value, you can directly compare their IDs without calculating the values before comparison.
The isinstance () function is used to determine whether the object belongs to this type or whether the instance belongs to a class.

4-7.
Built-in function dir (). In several exercises in Chapter 2nd, we have done several experiments with the built-in function dir (), which accepts an object and then gives the corresponding attributes. Perform the same experiment on the types module. Write down the types you are familiar with, including your understanding of these types, and then write down the types you are not familiar. In the course of learning Pythopn, you should gradually become "unfamiliar" types.
[Answer]
.

4-8.
List and metadata. What are the similarities between a list and a group? What are the differences?
[Answer] These answers are found online.
A list is a data structure that processes a group of ordered projects. You can store a series project in a list. Project in the list. The items in the list should be included in square brackets so that python knows that you are specifying a list. Once you create a list, you can add, delete, or search for projects in the list. Since you can add or delete a project, we say that the list is a variable data type, that is, this type can be changed. The list can be nested.
The ancestor is very similar to the list, but the tuples are immutable. That is, you cannot modify the tuples. The tuples are defined by commas (,) in parentheses. Tuples are usually used to securely use a group of values for statements or user-defined functions. That is, the values of the used tuples do not change. Tuples can be nested.

Http://hi.baidu.com/pangpengde/blog/item/3c782b008e8b33db277fb510.html/cmtid/7f7b96282f2284f098250a2f (reference)

4-9.
Practice: Assign the following values:
A = 10
B = 10
C = 100.
D = 100
E = 10.0
F = 10.0
What is the output of the following expressions? Why?
(A) a is B
(B) c is d
(C) e is f
[Answer]
The Code is as follows:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C: \ Documents ents and Settings \ root> python
Python 2.7 (r27: 82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> A = 10
>>> B = 10
>>> C = 100
> D = 100
>>> E = 10.0
>>> F = 10.0
>>> A is B
True
>>> C is d
True
>>> E is f
False
>>>

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.