Python core programming-Chapter 4-personal notes, Chapter 4

Source: Internet
Author: User

Python core programming-Chapter 4-personal notes, Chapter 4

1. All python objects have three features:

① Identity: each object has its own unique identity, which can be obtained using the built-in function id. Basically not used, don't worry too much

>>> a = 2>>> id(2)39411616L>>> id(a)39411616L

This example also shows that the value assignment in python is actually "Reference"

② Type: the object type determines what type of value can be saved. You can use the built-in function type () to obtain the object type.

>>> type(2)<type 'int'>>>> type(2.0)<type 'float'>>>> type(111111111111111111111111)<type 'long'>>>> type('2')<type 'str'>>>> type(False)<type 'bool'>
>>> type(3.14j)
<type 'complex')>>> type(type)<type 'type'>

③ Value: Data represented by an object

Except the value, the other two features of the object are read-only.

2. Standard Type

Python standard types are also known as "Basic Data Types" because these types are the basic data types built in python. It mainly includes:

① Integer 'int' Integer

② Long integer 'long' long integer

③ Floating point real number 'float' Floating point type

④ Complex number 'compule' plural

⑤ Boolean 'bool 'Boolean

⑥ String 'str' String

7. List 'LIST' list

Define Tuple 'tuple' tuples

Dictionary 'dict 'Dictionary

3. Other built-in types

(1) Other data types include:

Type

Null Object (None)

File

Set/Fixed Set

Functions/methods

Module

Class

(2) Type object

① The output of the type () function is actually a type object, which outputs a string type object.

② All types of objects are type.

(3) Null Object

A Null object is a special type in python, also known as NoneType. It has only one value, that is, None. It does not support any operation or any built-in method.

(4) Notes

4. Standard Operators

(1) Comparison of object values

Comparison operators are used to determine whether objects of the same type are equal. Therefore, comparison operations are supported for built-in types. boolean values True or False are returned for comparison operations.

The actual comparison operations vary by type. For example, the numeric type is compared by the value size and symbol, and the string is compared by the Character Sequence Value.

Example

Multiple comparison operators can be performed on the same row in the order of left to right.

Appendix: Comparison operators of standard type values

(2) Object Identity comparison

The Value Comparison operation of the standard type is performed on the value of the object, comparing the value of the object rather than the object itself. Python also supports comparison of objects as a supplement to object Value Comparison.

Comparison operator of Standard Object identities:

Specific operations:

In the preceding example, python creates different objects for a B when assigning values to the list of a and B respectively. Even if the values of the two are equal, the owner of a B is not equal;

However, when an integer value is assigned to a B, the results of the values are the same as those of a B.

This is because python caches simple integers, so a B points to the same object, and 'a is B 'returns True. In python 2.7, the cache's simple Integer Range is (-5,256)

① Verification Upper Limit Method

1 num1 = 0 2 num2 = 0 3 while True: 4 if num1 is not num2: 5 print "% d is the upper limit! "% (Num1-1) 6 break 7 num1 + = 1 8 num2 + = 1 9 10 output: 256 is the upper limit! 11 12

② Verification lower Limit Method

Num1 = 0num2 = 0 while True: if num1 is not num2: print "% d is the lower limit! "% (Num1 + 1) break num1 + =-1 num2 + =-1 Output:-5 is the lower limit!

(3) Boolean Type

Boolean logical operators include not, and, or

Among them, not has the highest priority, and followed, or has the lowest priority.

5. Standard built-in functions

Standard built-in functions include:

(1) type () function

Type () accepts an object as a parameter and returns the type of the parameter. The returned value is a type object.

(2) cmp ()

The built-in function cmp () is used to compare the obj1 and obj2 objects. If obj1 is smaller than obj2, a negative integer is returned. If obj1 is greater than obj2, a positive integer is returned; if obj1 is equal to obj2, 0 is returned.

(3) str () repr ()''

You can easily obtain the object content, type, numeric attributes, and other information in string mode.

The str () function makes the string readable. The eval () value cannot be used in the returned results, but it is suitable for print statement output.

Repr () and ''operations are very similar in terms of features and functions. They return an object's" official "string representation. In most cases, eval () can be evaluated () obtain the object again.

In short, the repr () output is more friendly to python, And the str () output is more user-friendly. In many cases, the three outputs are exactly the same.

(4) type () isinstance ()

Isinstance () is a built-in function of python. Its syntax is isinstance (object, classinfo ). The first parameter is an object, the second parameter is a type or a type consisting of tuples, and the returned value is a Boolean value. If the object type is the same as the type of the second parameter or the object type is the same as a type of the second parameter given as a tuples, True is returned. Otherwise, False is returned.

 

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.