Python core Programming-fourth chapter-Personal notes

Source: Internet
Author: User
Tags logical operators

1. All Python objects have three features:

① Identity: Each object has a unique identity identity, which can be obtained using the built-in function ID (). It's basically not going to work, don't care too much

>>> a = 2>>> ID (2)39411616l>>> ID (a)39411616L

This example also shows that the assignment in Python is actually a "reference"

② type: The type of object determines what type of value can be saved. The type of the object can be obtained with the built-in function 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: The data represented by the object

In addition to the value, the other two properties of the object are read-only

2. Standard type

Python standard types are also referred to as "basic data types" because they are basic data types that are built into python. Mainly include:

①integer ' int ' integral type

②long integer ' Long ' integer

③floating point real number ' float ' float type

④complex number ' Complex ' plural type

⑤boolean ' bool ' Boolean type

⑥string ' str ' string

⑦list ' list ' lists

⑧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, a type object that outputs a string

② the types of all types of objects are type.

(3) Null object

The null object is a special type in Python, also known as Nonetype. It has only one value, which is none, does not support any operations and does not have any built-in methods.

(4) Notes

4. Standard type operator

(1) Comparison of object values

Comparison operators are used to determine if the same type of object is equal, so the built-in type supports comparison operations, and the comparison operation returns a Boolean value of TRUE or false.

Actual comparison operations vary by type, such as numeric types based on the size and symbol comparisons of numeric values, strings being compared by character sequence values, and so on.

Cases

Multiple comparison operators can be on the same line, in order from left to right

Attached: standard type value comparison operator

(2) Object identity Comparison

The standard type value comparison operation is performed on the value of the object, comparing the value of the object to the object itself. Python also supports comparisons of the object itself as a complement to the object value comparison.

Standard type Object identity comparison operator:

Specific operation:

In the example above, when a and B are assigned to a list, Python creates different objects for a B, even if the values are equal, a B itself is still unequal;

However, when an integer assignment is made to a B, the result of each assignment is that a B itself is equal

The reason for this is that Python caches simple integers, so a B points to the same object, and ' A is B ' returns true. In Python 2.7, the simple integer range of the cache is (-5,256)

① validation Upper bound method

1NUM1 =02num2 =03  whileTrue:4     ifNum1 is  notnum2:5         Print "%d is the upper limit!"% (num1-1)6          Break7NUM1 + = 18Num2 + = 19 TenOutput: 256 isThe Upper limit! One  A         

② verifying the lower bound method

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

(3) Boolean type

Boolean logical operators include not, and, or

Where not the highest priority, and the second, or the lowest

5. Standard type built-in functions

The standard types of built-in functions include:

(1) Type () function

The type () takes an object as a parameter, returns the parameter's types, and the return value is a type Object

(2) CMP ()

The built-in function cmp () is used to compare two objects obj1 and obj2: If the obj1 is less than obj2, a negative integer is returned, and if Obj1 is greater than obj2, a positive integer is returned, and 0 is returned if Obj1 equals Obj2.

(3) Str () repr () "

All three can be conveniently used as a string to obtain the object's content, type, numeric properties and other information.

The STR () function gets a good string readability, and the returned result is usually not evaluated with eval (), but is well suited for print statement output

The repr () and ' operations are very similar in character and function, they return an "official" string representation of an object, and in most cases the object can be re-acquired by Eval ().

In summary, the REPR () output is more friendly to Python, the STR () output is more user friendly, and in many cases the output of the three is exactly the same.

(4) type () Isinstance ()

Isinstance () is a built-in function of Python, and the syntax is Isinstance (object,classinfo). The first parameter is the object, the second parameter is a tuple of type or type, and the returned value is a Boolean type. Returns true if the type of the object is the same as the type of the second parameter, or if the type of the object is the same as one of the second arguments given as a tuple. Otherwise, returns false

Python core Programming-fourth chapter-Personal notes

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.