Question about "or" operator


#1

In the tutorials I had this:
"2 ** 3 == 108 % 100 or ‘Cleese’ == ‘King Arthur’"
The answer is “True”, but I don’t understand why

First statement is obviously wrong ( 8 != 1 )
Second is ambiguous imo, ‘Cleese’ == ‘King Arthur’ is false for the strings are not the same
At the same time… ‘Cleese’ == ‘King Arthur’ is true because they are both strings

Can someone clarify this for me?

@edit The math is actually true. My bad. I still want clarification for the second statement. Thanks
@edit2 Well, figured the answer for my doubt the hard way. Welp. Can someone close this post?


#2
>>> ('Cleese' or 'King Arthur') and True
True
>>> ('Cleese' and 'King Arthur') and True
True
>>> 

Every string but an empty one is truthy.

>>> ('' and 'King Arthur') and True
''
>>> ('' or False) and True
False
>>> ('Cleese' or False) and True
True
>>> 

The above is based on truthiness, but we should not equate this as anything to do with identity. Yes “A” and “B” are both strings, but when compared in a boolean expression they are not identical, so !=. Their types are only a factor in determining how to evaluate them.


>>> 2 + "2"
Traceback (most recent call last):
  File "<pyshell#561>", line 1, in <module>
    2 + "2"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> 2 * "2"
'22'
>>> "2" + "2"
'22'
>>> "2" * "2"
Traceback (most recent call last):
  File "<pyshell#564>", line 1, in <module>
    "2" * "2"
TypeError: can't multiply sequence by non-int of type 'str'
>>> 

#3

Thank you for your through response, mtf. Guess I have to take things as literally as possible.

Btw, what’s happening in the example you posted of 2 * “2”? I was expecting an error because you were trying to multiply a non-string by a string


#4

Python permits sequence multiplication…

>>> 3 * "ABC"
'ABCABCABC'
>>> 5 * ['O']
['O', 'O', 'O', 'O', 'O']
>>> 

Yes, try not to interpret. There is a proviso… Be sure you understand the concept and dig a little in the documentation for general and practical details. Stay on station and don’t stray too much, though. Just get through the track a little better informed and save in depth study for the end of the track when you review.


#5

I see, that’s handy. Thanks a lot, mtf. It was simply not mentioned in the tutorial… and I just started on conditionals. Sorry about the trouble.


#6

No trouble at all. Just chipping in with pleasure. These questions are important to more than just you and me, especially if we can extend the lesson rather than just parrot the instructions.

If you are interested we can delve a little further into or (and and, not). It will add value to your question.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.