+ 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?

29th Apr 2019, 11:39 AM
Sonic
Sonic - avatar
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.
29th Apr 2019, 11:46 AM
HonFu
HonFu - avatar
+ 13
HonFu in the past I've tried such antics. In this particular case I really wanted to know the answer as I was inspired by your "python" or "java" code. These days I'm not so interested in the Hot Today lists as I am not trying for related badges.
29th Apr 2019, 12:13 PM
Sonic
Sonic - avatar
+ 11
Thanks guys! Good explanation HonFu .
29th Apr 2019, 12:04 PM
Sonic
Sonic - avatar
+ 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.
29th Apr 2019, 12:33 PM
Sonic
Sonic - avatar
+ 9
Amazingly that code I just posted got 17 views in 5 minutes and somehow made the Hot Today list again in 5 minutes! 😯😯😯
29th Apr 2019, 12:09 PM
Sonic
Sonic - avatar
+ 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
29th Apr 2019, 3:34 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 7
Edwin Pratt, I think your code makes the behavior visually more obvious if you use numbers (False == 0) instead of strings.
29th Apr 2019, 12:11 PM
HonFu
HonFu - avatar
+ 6
Thanks HonFu but why does 'and' give the last true value? Because it has to check all values?
29th Apr 2019, 11:54 AM
Sonic
Sonic - avatar
+ 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
29th Apr 2019, 11:56 AM
Edwin Pratt
Edwin Pratt - avatar
+ 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)
29th Apr 2019, 11:56 AM
HonFu
HonFu - avatar
+ 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
29th Apr 2019, 12:00 PM
HonFu
HonFu - avatar
+ 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.
29th Apr 2019, 12:28 PM
HonFu
HonFu - avatar
+ 5
Sonic, that was your aim to begin with, confess! 😂
29th Apr 2019, 12:10 PM
HonFu
HonFu - avatar
+ 4
I have added what HonFu had said to the code.
29th Apr 2019, 12:02 PM
Edwin Pratt
Edwin Pratt - avatar
+ 3
I wonder if it has to do with the bare bits🤔
29th Apr 2019, 11:42 AM
Edwin Pratt
Edwin Pratt - avatar
0
Hi
30th Apr 2019, 6:58 AM
Nwa beke
Nwa beke - avatar