Python operator-the most commonly used comparison operator in programming (instance resolution)

Source: Internet
Author: User
Today in this article we say Python comparison operators, a total of seven different operators are equal, not equal to, greater than, less than, greater than or equal to, less than equals, these seven are more commonly used comparison operators. This is one of the things that will often be used in future programming. I hope this article will help you with your reading.

comparison operators: The comparison operator, by definition, is an operational symbol used for comparison.

The following assumes that the variable A is 10 and the variable B is 20:

= =      equals-The comparison object is equal    (a = = B) returns FALSE. !    =      does not equal-compares two objects that are not equal    (a! = B) returns true.    <>      not equals-compares two objects for unequal    (a <> B) returns True. This operator is similar to! =.    >       Greater-Returns FALSE if X is greater than Y    (a > B).    <       less-returns whether x is less than Y. All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false.    (A < b) returns TRUE.    >=      is greater than or equal-returns whether X is greater than or equal to Y.    (a >= B) returns FALSE.    <=      is less than or equal-returns whether X is less than or equal to Y.    (a <= B) returns True.

It is not difficult to see the comparison operator from the above symbol, and an example is given below:

#!/usr/bin/python#-*-coding:utf-8-*-a = 15b = 9c = 0if (A = = B):   print "1-a equals B" Else:   print "1-a not equal to B" if  (A! = b):   print "2-a not equal to B" else:   print "2-a equals B" if (a <> b):   print "3-a not equal to B" else:   print "3 -a equals B "if (A < b):   print" 4-a less than B "else:   print" 4-a greater than or equal to B "if (A > B):   print" 5-a greater than B "Else:
  print "5-a less than equals B" # modifies the values of variables A and b = 5b = 20if (a <= b):   print "6-a less than or equal to B" else:   print "6-a greater than  b "If (b >= a):   print" 7-b greater than or equal to a "else:   print" 7-b less than a "

The answers to the above examples are:

The value of the 1-C is: The value of the 452-c: -153-c is: The value of 4504-C is: 05-c value is: 156-c value is: 87-c is not equal to 21-a not equal to b2-a not equal to B4-a greater than or equal to b5-a greater than b6-a equals b7-b greater than or equal to a

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.