Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Basic question
#1
my question here
Hi,
I am new to python. Please explain the below. Why one syntax is returning "True" while the second one returns False.

>>> print "[1, 2] > 'foo' = ", (1, 2) > 'foo'
[1, 2] > 'foo' = True
>>> print "[1, 2] > 'foo' = ", [1, 2] > 'foo'
[1, 2] > 'foo' = False
>>>

Thanks

my code here
Quote
#2
Both of these statements will return a syntax error.
Quote
#3
Nope. As you see above, one is "True" and the second one is "False"
Quote
#4
In Python 2.x, yes. But in Python 3.x this creates a TypeError. Ordering of disparate types was something that was dropped in Python 3.x, precisely because of screwy stuff like this that doesn't make any sense. You should switch to Python 3.x if you can.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
sorry, I just got up, and didn't realize you were using python 2.7

 >>> print "[1, 2] > 'foo' = ", (1, 2) > 'foo' 
I'm not sure exactly how 2.7 compares tuples against strings,
but the ASCII value of 'f' is 102, which is greater than either 1 or 2,
so I would expect it to return False. It shouldn't even allow this comparison
Python 3 gives the following error:

 >>> print("[1, 2] > 'foo' = ", ((1, 2) > 'foo')) 
Error:
>>> print("[1, 2] > 'foo' = ", ((1, 2) > 'foo')) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'tuple' and 'str' >>>
The second one:
print "[1, 2] > 'foo' = ", [1, 2] > 'foo'
gives the following error in python 3:
Error:
>>> print ("[1, 2] > 'foo' = ", [1, 2] > 'foo') Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'list' and 'str' >>>
my suggestion upgrade to python 3.6.2
Quote
#6
Acording to Python 2.x documentation:
Quote:Objects of different types except numbers are ordered by their type names

This will throw an error in Python 3

So it seems like you a doing comparison between 'tuple' and 'str'. 't' is greater than 's'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#7
Thanks. (1,2) is tuple. What is [1, 2]...a list?
Quote
#8
Yes, [1, 2] is a list. So 'l' is less than 's'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#9
Thanks a lot
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Basic Beginner question NHeav 4 177 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Basic coding question with Python Than999 3 778 Jul-17-2019, 04:36 PM
Last Post: jefsummers
  basic question???????? cemdede 2 354 Jan-18-2019, 03:05 AM
Last Post: ntd9395
  basic plotting question Devilish 0 407 Dec-27-2018, 10:35 PM
Last Post: Devilish
  basic def / if question tryingtolearnpython 2 678 Jun-18-2018, 07:11 AM
Last Post: tryingtolearnpython
  Basic Python question vmenon 2 635 Mar-17-2018, 06:35 PM
Last Post: vmenon
  Basic Python Question fs8 7 1,352 Dec-24-2017, 10:24 AM
Last Post: fs8
  Basic input() function question rebubula76 2 935 Nov-27-2017, 08:12 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)