Python Forum
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
Reply
#2
Both of these statements will return a syntax error.
Reply
#3
Nope. As you see above, one is "True" and the second one is "False"
Reply
#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
Reply
#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
Reply
#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
Reply
#7
Thanks. (1,2) is tuple. What is [1, 2]...a list?
Reply
#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
Reply
#9
Thanks a lot
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 504 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  [solved] Basic question on list matchiing paul18fr 7 1,808 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  Very basic calculator question BoudewijnFunke 4 1,883 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  basic question isinstance tames 5 2,772 Nov-23-2020, 07:20 AM
Last Post: tames
  basic question about tuples and immutability sudonym3 6 2,828 Oct-18-2020, 05:11 PM
Last Post: sudonym3
  Basic Pyhton for Rhino 6 question about variables SaeedSH 1 2,107 Jan-28-2020, 04:33 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 2,707 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Basic coding question with Python Than999 3 3,058 Jul-17-2019, 04:36 PM
Last Post: jefsummers
  basic question???????? cemdede 2 2,301 Jan-18-2019, 03:05 AM
Last Post: ntd9395
  basic plotting question Devilish 0 1,863 Dec-27-2018, 10:35 PM
Last Post: Devilish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020