Examples of comparing different Python objects and python objects

Source: Internet
Author: User

Examples of comparing different Python objects and python objects

The source of evil:

Q ):

>>> filter( lambda x: x > 2, [ 1, [ 1, 2, 3 ], 2, 3 ] ) [[1, 2, 3], 3]

? :

>>> 1 < [ 1 ] True >>> int < list True >>> dict < int < list True
>>> int < map False

After several twists and turns, I discussed it with Fireboo.

1. Compare different objects (except number) according to type names,

2. When objects of the same type do not support proper comparison, use address comparison.

3. list and list. tuple and tuple are compared in Lexicographic Order.

>>> x = 1 >>> y = [ 1 ] >>> type( x ) <type 'int'> >>> type( y ) <type 'list'> >>> x < y True
>>> type( int ) <type 'type'> >>> type( list ) <type 'type'> >>> id( int ) 505552912 >>> id( list ) 505555336 >>> int < list True
>>> type( map ) <type 'builtin_function_or_method'> >>> type( list ) <type 'type'> >>> map < list True

What are Python classes, methods, objects, and instances? The concepts of methods, objects, and instances are rather vague,

Class is a set of functions. In this set, you define many functions. methods are actually the functions you define. In the following example, Class Plus is a Class. The two functions nested in this Class are called methods, but _ init _ is only used to initialize this Class, so it is not a method. The get_result function is a method.
For example:
Class Plus:
Def _ init _ (self, a, B)
Self. a =
Self. B = B
Def get_result (self)
Return self. a + self. B
In the above example, self is an object, which has two parameters: self. a. The other is self. b. The object is simply a variable with multiple attributes (or sub-variables. If the object is a general object, the instance is a specific object. The object is just a template with some attributes, and the instance is to fill in the data in this template. For example, if you write c1 = Plus (), c1 is an instance, and you can add c2 = Plus (), c2 is also an instance, however, they share common attributes and templates. The following example calls the method in the class:
Result1 = c1.get _ result () >>> 3. The output result is 3.
Result2 = c2.get _ result () >>> 5. The output result is 5.

Python comparison size

Note that the comparison string is different from the comparison number. You need to manually convert the string to an integer.

Character strings do not depend on the number size, but on the ascii sequence of characters. The data obtained from raw_input is a string, and string 3 is actually greater than string 21. Therefore, to make a correct comparison, you must convert the string to a number.

I modified your program:
Def printMax (a, B): if a> B: print a, 'is maximum' else: print B, 'is maximum' # printMax (3, 4) # directly give literal valuesx = int (raw_input ("x") y = int (raw_input ("y") printMax (x, y) # give variables as arguments
Hope to adopt it. Please follow the time below for adoption. Thank you for your support!


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.