473,388 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

comparing strings and integers

By mistake I coded something like

print ("1" > 1)

and got the result "True". Comparing an integer and a string seems
meaningless to me, and I would prefer to have an exception thrown. Can
someone explain why Python allows comparisons between integers and
strings, and how it handles those cases? Why is "1" > 1?

Pychecker does not warn about the line of code above -- I wish it did.

In my code what I really intended was to convert the "1" to an int and
THEN do a comparison.
Jul 18 '05 #1
5 7573
without knowing any better, its possible "1" translates to 0x31

so it would become

0x31 > 1 ===> True....

David
http://cellphone.duneram.com/index.html

----- Original Message -----
From: <be*******@aol.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Wednesday, May 19, 2004 5:26 PM
Subject: comparing strings and integers

By mistake I coded something like

print ("1" > 1)

and got the result "True". Comparing an integer and a string seems
meaningless to me, and I would prefer to have an exception thrown. Can
someone explain why Python allows comparisons between integers and
strings, and how it handles those cases? Why is "1" > 1?

Pychecker does not warn about the line of code above -- I wish it did.

In my code what I really intended was to convert the "1" to an int and
THEN do a comparison.


Jul 18 '05 #2
On Wednesday 19 May 2004 02:26 pm, be*******@aol.com wrote:
By mistake I coded something like

print ("1" > 1)

and got the result "True". Comparing an integer and a string seems
meaningless to me, and I would prefer to have an exception thrown. Can
someone explain why Python allows comparisons between integers and
strings, and how it handles those cases? Why is "1" > 1?

Pychecker does not warn about the line of code above -- I wish it did.

In my code what I really intended was to convert the "1" to an int and
THEN do a comparison. From the manual:


The operators <, >, ==, >=, <=, and != compare the values of two
objects. The objects need not have the same type. If both are
numbers, they are converted to a common type. Otherwise, objects of
different types always compare unequal, and are ordered consistently
but arbitrarily.

(This unusual definition of comparison was used to simplify the
definition of operations like sorting and the in and not in
operators. In the future, the comparison rules for objects of
different types are likely to change.)

Jul 18 '05 #3
On 19 May 2004 14:26:59 -0700
be*******@aol.com wrote:

#> By mistake I coded something like

#> print ("1" > 1)

#> and got the result "True". Comparing an integer and a string seems
#> meaningless to me, and I would prefer to have an exception thrown.
#> Can someone explain why Python allows comparisons between integers
#> and strings, and how it handles those cases? Why is "1" > 1?

From the Python Reference Manual, chapter 5.9 Comparisons:

"""

The operators <, >, ==, >=, <=, and != compare the values of two
objects. The objects need not have the same type. If both are numbers,
they are converted to a common type. Otherwise, objects of different
types always compare unequal, and are ordered consistently but
arbitrarily.

(This unusual definition of comparison was used to simplify the
definition of operations like sorting and the in and not in operators.
In the future, the comparison rules for objects of different types are
likely to change.)

"""

#> Pychecker does not warn about the line of code above -- I wish it
#> did.

Good point - it probably would be nice if it did. I don't know if it
is possible to check for such things in the general case, though.

--
Best wishes,
Slawomir Nowaczyk
( Sl***************@cs.lth.se )

Never argue with an idiot: first he will drag you down to his level,
then he will beat you with experience.
Jul 18 '05 #4
Thanks to S. Nowaczyk and others for the helpful replies. If one wants

if (a > b)

to raise an exception when the comparison is not "sensible", maybe an
alternative way of writing it is

if ((a - b) > 0)

This should work when 'a' and 'b' are numerical and will fail if one
is numerical and the other is a string. A down side is that it does
not permit string comparisons, but I rarely compare strings except for
equality in my code.
Jul 18 '05 #5
In article <30*************************@posting.google.com> ,
be*******@aol.com wrote:
Thanks to S. Nowaczyk and others for the helpful replies. If one wants

if (a > b)

to raise an exception when the comparison is not "sensible", maybe an
alternative way of writing it is

if ((a - b) > 0)

This should work when 'a' and 'b' are numerical and will fail if one
is numerical and the other is a string. A down side is that it does
not permit string comparisons, but I rarely compare strings except for
equality in my code.


How do you define "sensible"?
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
6
by: C# Learner | last post by:
I want to compare two strings, in a numeric fashion. e.g.: string a = "10"; string b = "2"; int comparison = String.Compare(a, b); // comparison is now <0, but I want it to be >0 in this...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
3
by: Jeff | last post by:
....still new to vb.net 2005 I understand the concept of arrays, and have used them in other languages, but was hoping that someone could get me started with something. I have a fairly long...
6
by: andrews | last post by:
I try to add strings with fixed length who are representing integers So I have to put spaces (I guesse) before short strings(from short integers). F.e. 12345 1 567 5432 54328
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.