Python Learning path 8--python Object 2

Source: Internet
Author: User

1. Standard type operator
1.1 Comparison of object values

Comparison operators are used for equality of objects of the same type, all built-in types support comparison operations, and comparison operations return a Boolean value of TRUE or False.

<span style= "FONT-SIZE:14PX;" >>>> 2 = = 2true>>> 2.33 < 2.44true>>> ' abc ' = = ' xyz ' false>>> ' abc ' < ' XYZ ' Tru  e>>> [3, ' abc '] = = [3, ' abc ']true>>> [3, ' abc '] = = [' abc ', 3]false>>> 3 < 4 < 7       #same As (3 < 4) and (4 < 7) True>>> 4 > 3 = 3      #same as (4 > 3) and (3 = = 3) true</span>

Python comparison operator:< > <= >= = = = <>. (<> equal to! =, may not be supported in the future)


1.2 Object Identity comparison

Python supports not only object-value comparisons, but also the comparison of objects themselves.

Standard type Object Identity comparison operator

Operator Function
Obj1 is Obj2 Obj1 and Obj2 are the same object
Obj1 is not obj2 Obj1 and Obj2 are not the same.

<span style= "FONT-SIZE:14PX;"  >>>> a = [5, ' hat ', -9.3]>>> B = a>>> A is btrue>>> A was not bfalse>>> B = 2.5e-5>>> b2.5e-05>>> a[5, ' hat ', -9.3]>>> A is bfalse>>> A are not bTrue</span>
look at the following example, then the question comes.
Why does A and B point to the same object? As you know, when assigning a variable, the Python interpreter creates a new object and assigns a reference to its object to the variable. That being the case, that a, B should point to different objects. Please continue to see that X and Y,x and Y are indeed pointing to different objects, which are in line with our expected results. Why

It turns out that integer objects and strings are immutable objects, and all python caches them efficiently, which leads us to think that Python should create new objects without the illusion of creating new objects. Python caches only simple integers, and the range of Python-cached integers changes, so don't use this feature. (I don't know how to use it.)

1.3 Boolean type

Boolean type operators have And,or and not three, equivalent to the && in C;, | |, and!. Where not is the highest priority, followed by and and OR.

2 standard type built-in functions

Python provides some built-in functions for these basic object types: CMP (), repr (), str (), type ().

Type ():

Usage: Type (object)

Type () takes an object as a parameter and returns its type. Its return value is a type object.

CMP ():

Usage: CMP (OBJ1,OBJ2), if Obj1 is less than obj2, it returns a negative integer if OBJ1 is greater than obj2, returns a positive integer if equal returns 0


STR () and repr ()

The STR () and repr () functions can be conveniently used to obtain information such as the object's contents, type, numeric properties, etc., by means of a string, and the str () function gets a good string readability, while the repr () function gets a string that can usually be retrieved by the object, typically obj = eval (repr ( OBJ)) is established. In most cases, the output of the two functions is still the same.

<span style= "FONT-SIZE:14PX;" >>>> str (1) ' 1 ' >>> str (2e10) ' 20000000000.0 ' >>> repr (2e10) ' 20000000000.0 ' >>> STR ([0, 5, 5, 9]) ' [0, 5, 5, 9] ' >>> repr ([0, 5, 5, 9]) ' [0, 5, 5, 9] ' </span>





Python Learning path 8--python Object 2

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.