+ 19
I'm told that Python is beginner friendly which I translate to mean intuitive but I don't quite understand this.
How do you explain the result of or/and operations on strings within a print statement as in the following? https://code.sololearn.com/cx5Vl0ra8t9m/?ref=app Maybe I should read the Python tutorial more or look it up on Google but if any Python expert wants to jump in and explain, be my guest. And was it Ruby that was considered the intuitive language, rather than Python?
17 Respostas
+ 18
or:
-I'd like to have icecream. I don't mind what sort. Do you have chocolate?
-yes.
-Then that!
(First True value returned.)
-no.
-Okay, do you have strawberry?
(Check the next for True or False. )
and:
-I want to bake a cake, and I need eggs, floor and...
-I can stop you right there, we don't have floor.
-What a pity...
(So only True if all items are True.)
Real life example:
value = input() or 'No input given'
So in case there's input, value will be that, otherwise (no input == '' == False) it will be that string after or.
It is a short circuit that stops at the point where the answer is determined.
And for convenience it actually returns the last checked value instead of 0 or 1 so that you can lazily assign or print stuff.
+ 11
Thanks guys! Good explanation HonFu .
+ 11
Yes this is all an educational experience and a bit of reverse engineering to determine the behaviour of the algorithm that generates these lists. One observation I have, which is quite obvious is that when your follower count increases, the amount of effort you need to put into try to get into these lists diminishes.
Another observation is the synergistic effect that coupling a code to a question can have. If this sort of experiment is not educational programming wise, it certainly is from an online marketing point of view.
+ 9
Amazingly that code I just posted got 17 views in 5 minutes and somehow made the Hot Today list again in 5 minutes! 😯😯😯
+ 9
a or b return a if a else b
a and b return b if a else a
By the way, you can use help("keyword") to get help about keyword
+ 7
Edwin Pratt, I think your code makes the behavior visually more obvious if you use numbers (False == 0) instead of strings.
+ 6
Thanks HonFu but why does 'and' give the last true value? Because it has to check all values?
+ 6
Did some study
The or keyword returns the first value while the and keyword returns the second value:
https://code.sololearn.com/cQCsQZABddXP/?ref=app
+ 6
Exactly, Sonic. Unless an earlier value is False, then it will return that value.
Try: print('' and 'Bob')
Well, or better: print(0 and 42)
+ 6
Maybe it becomes comprehendable translated to pseudocode?
def or(conditions):
for c in conditions:
if c: return True
return False
def and(conditions)
for c in conditions:
if not c: return False
return True
+ 6
Even if there's no badge to gain for you, as an experiment it's still interesting.
Hot Today and Trendy looks quite random and magical to me - good to get a hint on what's going on there.
+ 5
Sonic, that was your aim to begin with, confess! 😂
+ 4
I have added what HonFu had said to the code.
+ 3
I wonder if it has to do with the bare bits🤔
0
Hi