Python Forum
simple syntax question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: simple syntax question (/thread-16687.html)



simple syntax question - speedskis777 - Mar-10-2019

Hi.

Why does the order matter here? What would be the correct syntax to print two True statements?
var = "*"
print(var == ("/" or "*"))
print(var == ("*" or "/"))
Thanks


RE: simple syntax question - Archie - Mar-10-2019

print("/" or "*") # yields "/"
print("*" or "/") # yields "*"
The doc says that with "or", only the first parameter is used in the logical comparison.
Thus, for your example; False outputs for the 1st print, and True outputs for the second print.


RE: simple syntax question - speedskis777 - Mar-10-2019

Thanks, Archie. I'm not sure I completely follow, though.

Docs says:

x or y
if x is false, then y, else x
(1) This is a short-circuit operator, so it only evaluates the second argument if the first one is false.


So if I'm reading this correctly,

var = "*"
print(var == ("/" or "*")) # var == "/" is False, so it will evaluate var == "*" which is True, making the whole statement True
print(var == ("*" or "/")) # var == "*" is True, so the statement is True

Also, I put parentheses around ("/" or "*") to hopefully show the scope of OR but it evaluates the same either way Huh


RE: simple syntax question - Yoriz - Mar-10-2019

Multiple expressions with "or" keyword


RE: simple syntax question - speedskis777 - Mar-10-2019

(Mar-10-2019, 05:34 PM)Yoriz Wrote: Multiple expressions with "or" keyword

Thanks Yoriz. I'll stick to the methods advised in that link. So I will use:
print(var == "/" or var == "*")
I'm still really confused though about one thing which hopefully you can help explain... The "*" argument should make this statement True but it evaluates False:

var = "*"
var == ("/" or "*")
I double-checked the docs again and it does say it will evaluate the second argument if the first is false. So... what am I not understanding here??? Sad


RE: simple syntax question - Yoriz - Mar-10-2019

You are missing that the first argument is true.
Any string value other than "" is evaluated to True.


RE: simple syntax question - speedskis777 - Mar-11-2019

(Mar-10-2019, 11:58 PM)Yoriz Wrote: You are missing that the first argument is true.
Any string value other than "" is evaluated to True.

Okay I get it now, thanks!

Clear example of what's going on for anyone else:

var = 10
var == True
Evaluates false, because 10 is not the same as True, and like the thread above noted, any non-empty string will be changed to True.


RE: simple syntax question - snippsat - Mar-11-2019

(Mar-11-2019, 12:11 AM)speedskis777 Wrote: Evaluates false, because 10 is not the same as True
This is not a good example,as it's the bool() value of 10 that normally in almost all cases is used in conditions with if statements and loops.
>>> var = 10
>>> bool(var) 
True
>>> bool(var) == True
True

# So if test with "if" var is True
>>> if var:
...     print('This is True')
...     
This is True
Also in first code you use or without if statements which is not normal.
>>> "/" or "*"
'/'
>>> "Hello" or "*"
'Hello'
>>> "" or "*"
'*'

# Here get a True or False value because use "if"
>>> if "/" or "*":
...     print('This is True')
... else:    
...     print('This is False')
...     
This is True

# Both False then is False
>>> if 0 or "":
...     print('This is True')
... else:    
...     print('This is False')
...     
This is False
Quiz True or False in this list?
>>> lst = ['a', '', {}, (), 9, 0, [], None, ('hello'), {'a': 99}]
>>> [bool(i) for i in lst]