Python code percentage number compare problem - python

when i compared tow percentage number it gives me wrong answer
a="{0:%}".format(85/100)
b="{0:%}".format(9/100)
if b>a :
print("done")
it should be pass if condition while its give me done in answer

when i compare two percentage numbers it gives me wrong answer
No, the interpreter gives the correct answer.
The variables get these string values:
a = '85.000000%'
b = '9.000000%'
You are complaining about this string comparison result:
>>> '9' > '85'
True
Or more simply, since they differ in the 1st character, about this result:
>>> '9' > '8'
True
If you would prefer a numeric comparison,
then strip the percent and recover the numbers:
>>> float(b[:-1]) > float(a[:-1])
False

Related

How to do slicing and use any() in python?

I am trying to find the numbers smaller than the number(here i) towards the right of a list
If here 1,2,3,4 for any number there is no number towards its right which is smaller than that.I want to implement with any() and slicing.
But when I did that in python with the following code I am getting True but it should be False where am I missing the logic? and why is the output as True?
num=[1,2,3,4]
for i in range(1,len(num)-1):
print (any(num[i+1:])<num[i])
Output:
True
True
You need to check what's actually happening here. You have:
any(num[i+1:]) < num[i]
any returns true if any of the elements of the list equivalent to true. Since all your numbers are non-zero, they are all equivalent to true. Then the right side compares to True to num[i], so you have True < 2 and True < 3. Since True is equivalent to 1 these both result in 1.
You probably want something like:
print( any(x < num[i] for x in num[i+1:]))
The any function should take a sequence of booleans, usually given by a generator expression. The reason your code outputs True is because num[i+1:] is a list of non-zero ints, which are considered "truthy", so the answer to "are any of them true?" is "yes".
You can write something like this:
num = [1,2,3,4]
for i in range(1, len(num) - 1):
print(any( x < num[i] for x in num[i+1:] ))
Output:
False
False

How do string comparisons work when they contain both numbers and letters?

I'm trying to compare time in Python, and came up with some weird comparisons. I've got no idea how the following statements work:
>>> "17:30" > "16:30"
True
>>> "12:30" > "13:30"
False
>>> '18:00 - asdfj' > '16:30 - asdfj'
True
My guess is that it takes the first number from before the colon, I'm not completely sure about it.
Basically, in python, it is lexicographical comparison.
Example 'a' comes before 'b', hence 'a' < 'b' is true. Similarly '2' < '3'. Hence '199' < '2' is true because 1 comes before 2.
As others have pointed out, a comparison between strings is a question of lexicographical ordering.
What that means procedurally:
two strings are compared one character at a time
the first character that's different decides which string is 'greater than' the other
if no characters are different and the strings are the same length, they are 'equal'.
if two characters are different, their 'ordinal value' decides which is 'greater'
a character is 'greater than' no character
For example, 'ab' > 'a' is True, because 'a' == 'a', but the first string has an extra character. And 'abc' < 'abd' because 'c' < 'd'.
'a' < 'b' because ord('a') < ord('b'). The ordinal value of a character is typically its ASCII value for normal characters, or more precisely, its the Unicode code point (https://docs.python.org/3/library/functions.html#ord). This also means that 'A' < 'a', because uppercase letters come before lowercase letters in Unicode. And '1' < 'A' because numbers come before letters.
Note that this may sometimes give surprising results (note the dots on the Ӓ):
>>> 'Ӓ' > 'a'
True
>>> 'A' > 'a'
False
There are many online tables and overviews of Unicode, but here's a fairly plain example: https://www.tamasoft.co.jp/en/general-info/unicode.html
As for your example:
>>> '18:00 - asdfj' > '16:30 - asdfj'
True
This makes sense, because '8' > '6' - the rest of the string doesn't matter.

What is the lexicographically smallest string in Python?

What is the lexicographically smallest string in Python? In other words, what is the string x such that x < y is always True (where y is a string)?
For example, in C++ the empty string is the smallest one (see Smallest lexicographical value of a string). Can anyone confirm that the same answer holds for Python?
This is what I've tried so far:
import string
for x in list(string.printable):
assert("" < x)
With x as empty string, x < y for any string y holds True in Python.
We can confirm this:
>>> all('' < x for x in string.printable)
True
all() returns a True if all elements of the iterable are true (or if the iterable is empty). The less than (<) operation of empty string with all string printables is thus a True.
This is true for non-printable characters as well.
The total range vary from 0 to 1,1141,111(0x10FFFF in base 16) (thanks to #AlexHall in comments).
>>> all('' < chr(i) for i in range(0x110000))
True
"" is the smallest string you can get, since its length is 0 (that's also the string returned by str())
However, I did not find anything in the documentation to explicitly confirm that...
https://docs.python.org/3.7/library/stdtypes.html#comparisons
https://docs.python.org/3.7/reference/datamodel.html#object.__lt__

python numeric string comparison [duplicate]

This question already has answers here:
How are strings compared?
(7 answers)
Closed 7 years ago.
I have numerical values that are loaded from a JSON object and are therefore all strings.
I am having issues with making numerical comparisons with these strings. The following makes no sense to me and I was hoping one of you champions could explain..
In[2]: print '100' < '45'
True
In[3]: print '99' < '45'
False
Using Python 2.7
When comparing strings they're compared by the ascii value of the characters. '1' has a value 49, and '4' is 52. So '1' is < '4'. '9' however is 57, so '9' is > '4'.
If you want to compare them numerically you could just int() the strings first like:
print int('100') < int('45')
It basically checks for lexicographic ordering.
Check documentation here -
>>> 'b' <'a'
False
>>> 'a' < 'b'
True
In above example, a comes before b, hence 'a' <'b' is true. But, not vica versa. Similarly '1'<'2'.
Hence '199999999999' < '5' is true because 1 comes before 5.

Ordering of string representations of integers [duplicate]

This question already has answers here:
How are strings compared?
(7 answers)
Closed 4 years ago.
This is another huge trap I've met today.
I spend hours on debugging my code and finally I found it caused by this weird setting
Below is my python prompt interface
'3' > '2'
True
'4' > '3'
True
'15' > '11'
True
'999233' > '123'
True
# At this point, you must think compare string numbers is just like compare numbers.
# Me too, but...
'5' > '15'
True
# What's this !!!???
# Meanwhile I am asking this question. I want to something exaggerated to mockerying
# this mechanism, and I find something surprised me:
'5' > '999233'
False
# What!!!???
# Suddenly an idea come across my mind, are they comparing the first string number
# at first, if they are equal and then compare the second one?
# So I tried:
'5' > '13333333333333333'
True
'5' > '61'
False
# That's it.
# my old doubt disappeared and a new question raised:
Why they designed such a mechanism instead of use natural number comparison mechanism?
What's the benefit to use this mechanism in "string number" comparison?
You are comparing Strings not numbers.
20 > 9 evaluates True for numeric types like integers and floats but with lexicographical comparison (strings) then '20' < '9' evaluates to True
Example:
$ python
>>> 5 > 10
False
>>> '5' > '10'
True
>>> '05' > '10'
False
>>> 'abc05' > 'bca10'
False
>>> 'dog' > 'cat'
True
>>> type('10')
<type 'str'>
>>> type(10)
<type 'int'>
It is a lexicographical comparison. As soon as an element greater than the other is found the comparison stops, so
'5' > '15'
is true because '5' is greater than '1'
Indeed the ascii value of the character '5' is greater than the value '1' so '5' > '15' evaluates to True. As string comparison is byte by byte, just like the length of a word in the dictionary doesn't effect it's position '5' > '1412423513515' is also True.
>>> '5' > '15'
True
>>> ord('5')
53
>>> ord('1')
49
Think of the string representation of the integers like alphabetical characters i.e 'z' > 'abc' evaluates to True because 'z' comes after 'a'. This is called lexicographic ordering.
The answer is that strings are compared from the leftmost character, or "lexicographical ordering". This gives intuitive results for, for example,
"an" < "b" # True
If you want to compare the values of the numbers the strings represent, you should be explicit about that:
int("15") < int("5") # False
You are comparing strings since your numbers are surrounded by single quotes.
In python < and > operators applied on strings compare them using lexicographic order.
You can test this putting a '0' before '5':
'05' > '11'
It's comparing the initial digit. Just as if it was alphabetical. 5 > 1 that's why you're getting 5 > 16

Categories

Resources