Python core programming chapter fourth Python objects

Source: Internet
Author: User
Tags scalar

4–1. The Python object. What are the three properties related to all Python objects?  Please describe it briefly. Identity, type, and value.

4–2. Type. What does non-change (immutable) mean? What types of Python can be changed (mutable) and which are not?

If an object supports an update operation, its value can be changed, otherwise its value is read-only. Whether the value of the object can be changed to be known as the object's change (mutability)

Digital Scalar cannot change direct access

String Scalar cannot change sequential access

List Container can change sequential access

Tuple Container cannot change sequential access

Dictionary Container can change map access

Non-modifiable means that once modified, the original variable is recycled, the ID is reassigned, and the new value is assigned

Instead, the ID does not change after the new value is assigned.

4–3. Type. Which Python types are accessed sequentially, and what are the differences between them and the mapping type?

can be accessed directly for non-container types. All numeric types are grouped into this category.

The sequence type refers to the elements within the container that are accessed in index order starting at 0. You can access one or more elements at a time, which is what you know about slices (slice). strings, lists, and tuples are grouped into this category

The mapping type is similar to the indexed property of a sequence, but its index does not use sequential numeric offsets, its elements are stored out of order, accessed through a unique key, which is the mapping type, which holds a collection of hash-value pairs.

Direct access to numbers
Sequential access to strings, lists, tuples
Map Access Dictionaries

Basic data Types 5/6/7 integers, lists, tuples, dictionaries

4–4. Type (). What does the built-in function type () do? What is the object returned by type ()?

You can get the type information for a particular object by calling the type () function:

4–4. STR () and repr (). What is the difference between the built-in function str () and repr ()? Which is equivalent to the inverse quotation mark (') operator?.

The built-in function str () and repr () = anti-quote operator (') can be conveniently used as a string to obtain information about the object's content, type, numeric properties, and so on. The STR () function gets a good readability of the string, and the repr () function gets a string that can often be used to regain the object, which is usually the case with obj = = eval (repr (obj)).

The repr () output is friendly to Python, and the output of STR () is more friendly to people

4–6. objects are equal. What do you think is the difference between type (a) = = Type (b) and type (a) is type (b)? Why would you choose the latter? What does the function isinstance () have to do with this?

Print type (a) = = Type (b) #判断同类型对象是否相等

Print type (a) is type (b) #a is b This expression is equivalent to ID (a) = = ID (b)

#我们用对象身份的比较来替代对象值的比较. If the object is different, it means

#味着原来的变量一定是不同类型的. (because there is only one type object for each type), there is no need to

#检查 (value).

If isinstance (num, int) is equivalent to if type (num) is Inttype

4–7. The built-in function dir (). In the second chapter of the exercises, we use the built-in function dir () to do several experiments, it takes an object, and then gives the corresponding properties. Please do the same experiment on the types module. Make a note of the types you are familiar with, including your knowledge of these types, and then note the types that you are not familiar with. In the process of learning Python, you will gradually become "familiar" with the "unfamiliar" type.
Edit by Vheavens
Edit by Vheavens

4–8. Lists and tuples. What is the same point for lists and tuples? What are the different points?

List Container can change sequential access

Tuple Container cannot change sequential access

4–9. Exercise, given the following assignment:
A = 10
b = 10
c = 100
D = 100
E = 10.0
F = 10.0 What is the output of the following expressions? Why?

A = 1 #第一个数字对象被创建, assigned to a, is a reference to the first object

b = 1

D = c = 10#c and D point to the same object

Built in, because these types are provided by Python by default

Python core programming chapter fourth Python objects

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.