Not x.islower() has different output than x.isupper() in list output...

pavlovevidence at gmail.com pavlovevidence at gmail.com
Tue May 3 06:00:22 EDT 2016


On Friday, April 29, 2016 at 6:55:56 PM UTC-7, Christopher Reimer wrote:
> On 4/29/2016 6:29 PM, Stephen Hansen wrote:
> > If isupper/islower were perfect opposites of each-other, there'd be no 
> > need for both. But since characters can be upper, lower, or *neither*, 
> > you run into this situation.
> 
> Based upon the official documentation, I was expecting perfect opposites.
> 
> str.islower(): "Return true if all cased characters [4] in the string 
> are lowercase and there is at least one cased character, false otherwise."
> 
> https://docs.python.org/3/library/stdtypes.html?highlight=islower#str.islower
> 
> str.isupper(): "Return true if all cased characters [4] in the string 
> are uppercase and there is at least one cased character, false otherwise."
> 
> https://docs.python.org/3/library/stdtypes.html?highlight=isupper#str.isupper


Just to take this discussion in a more pure logic direction.  What you call perfect opposites (that is, the functions being negations of each other) is not what the similar wording in the documentation actually implies: you shouldn't have been expecting that.

What you should have been expecting is a symmetry.  Say you have a string G.  islower(G) will return a certain result.  Now take every letter in G and swap the case, and call that string g.  isupper(g) will always return the same result is islower(G).

More succinctly, for any string x, the following is always ture:

islower(x) == isupper(swapcase(x))

But that is not the same thing, and does not imply, as the following identity (which it turns out is not always true, as we've seen):

islower(x) == not isupper(x)


Another example of functions that behave like this are ispositive and isnegative.  The identity "ispositive(x) == isnegative(-x)" is always true.  However, "ispositive(x) == not isnegative(x)" is false if x == 0.


However, I can understand your confusion, because there are some pairs of functions where both identities are true, and if you've seen a few of them it's fairly easy for your intuition to overgeneralize a bit.  An example I can think of offhand is iseven(x) and isodd(x), for any integer x.  The identities "iseven(x) == isodd(x^1)" and "iseven(x) == not isodd(x)" are both always true.


Carl Banks



More information about the Python-list mailing list