Python comparison operation usage example

Source: Internet
Author: User
Tags numeric lowercase in python


This article introduces the comparison operation in Python, which can be used to compare the size of two numeric data and the size of a string or list.


The comparison of numeric data is very simple, and if the expression is true, return True and False for false, consider the following example:

>>> 1>2
False
>>> 1<2
True
#比较两个数值数据是否相等的时候, you need to use the double equals sign
>>> 1==1
True
>>> 2>=1
True
>>> 2>=2
True
#1不等于2
>>> 1!=2
True

0x2. Comparison List
The size comparison of the list compares the elements in the list (the elements at the same index position), and returns True if they are exactly the same, and see the following example:

#创建两个列表, the first element is not the same
>>> a=["Www.111cn.net", "Clear Blade"]
>>> b=["111cn.net", "Clear Blade"]

#因为第一个元素不同, so return false
>>> Print (A==B)
False

#比较运算会逐个比较列表中的元素值, in this case, because the ASCII value of the first character W of the first element in B is greater than the ASCII value of the first character W of the first element in a, the answer is false, which is described in detail in the following string comparisons
>>> Print (A<B)
False

#下面这个表达式看起来有点复杂, first a[-1] and B[-1] are able to navigate to the last element of the two list (clear Blade), [-len (A[-1]):-1] The Len () function calculates the length of the element (2), and the minus sign allows the count to be counted backwards, and the last one is the -1,- 2 is positioned to "clear", the description number of fragments will be from-2 position, take to-1 position, but does not contain-1 position, so the final intercept is the character "clear", two "clear" Comparison definitely return true
>>> Print (A[-1][-len (a[-1]): -1]==b[-1][-len (A[-1]):-1])
True

>>> b=["Www.111cn.net", "Clear Blade"]
>>> Print (A==B)
True

The comparison method for the list also applies to tuples and dictionaries, the dictionary is slightly different, in the dictionary, as long as the key and value can match, then two dictionaries are the same, do not need key value index position is the same, see the following example:

#虽然a和b字典中键值对的索引位置不同, but each key and value can be matched to the same data in another dictionary, Python thinks the two dictionaries are the same, and lists and tuples need to match the values of the index and index bits.
>>> a={1: "A", 2: "B", 3: "C"}
>>> b={2: "B", 3: "C", 1: "A"}
>>> Print (A==B)
True

>>> c={1: "X", 2: "2", 3: "Three"}
>>> Print (A==C)
False

0x3. Comparison string
The comparison of strings is actually a comparison of ASCII code, in which each character has a corresponding decimal number, see the following example:

#比较单个字符, in ASCII, uppercase letters precede lowercase letters, uppercase a corresponds to a decimal number of 65, and a corresponding decimal in lowercase A is 97, so the expression is true
>>> print ("A" > "a")
True
>>> print ("A" > "B")
False

#多个字符组成的字符串比较大小时, a comparison is made from the position of index 0, and the result is returned as long as a character is not equal, and the following characters are not compared (note that the comparison is by comparison, rather than by converting all characters to decimal addition), You can try to compare the size of bdef and azzz, obviously azzz the decimal addition of all characters is larger than Bdef, but the first character A is smaller than B in comparison, so Python thinks bdef is bigger than azzz.
>>> a= "ABCD"
>>> b= "ABCD"
>>> c= "C"
>>> d= "Abcdmdzz"
>>> Print (A==B)
True
#c和a比较, obviously C has a larger decimal number in the ASCII code than a.
>>> Print (A<C)
True

#虽然b和d的前4个字符相等, but there are four characters behind D, compared to the fifth character, A has no characters to compare, so Python thinks D is bigger than a
>>> Print (A<D)
True

In string comparisons, there are two methods that are more practical, upper () and lower (), which convert strings to uppercase or lowercase, and see the following example:

#upper () can convert strings to uppercase, and lower () is just the opposite
>>> a= "ABCD"
>>> B=a.upper ()
>>> print (b)
Abcd
>>> C=b.lower ()
>>> Print (c)
Abcd

#有时候字符串中包含大小写形式, and we just want to compare each character to the same, you can convert them to uppercase or lowercase and compare them to each other.
>>> x= "Www.111cn.net"
>>> y= "WWW.111cn.net"
>>> print (X.upper () ==y.upper ())
True

#最后补充一个not运算符, reverse the result
>>> 1==1
True
>>> not 1==1
False

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.