What's going on in this Challenge question? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's going on in this Challenge question?

The question was: What is the output of this code? mylist = [False] string = "a" if mylist else "b" print(string) The answer is: a I expected: b Could somebody explain this? I haven't even seen this type of syntax before (conditional statement inside an assignment). Thanks! Felix

12th Apr 2020, 7:36 AM
Felix Lipo
Felix Lipo - avatar
11 Answers
+ 6
In my understanding, False --> 0 [False] --> [0] Since the array isn't empty "String = 'a' if mylist" is true, so it prints a Using False without the brackets mylist=False "string = 'a' if mylist" is False,prints b
12th Apr 2020, 7:46 AM
Justus
Justus - avatar
+ 3
Kiran Deep Naidu Felix is right. The challenge regards the list, not the items in the list.
12th Apr 2020, 8:25 AM
Oma Falk
Oma Falk - avatar
+ 2
Felix Lipo, when you mention the list in if statement then it would consider the number of elements in the list. So, according to your program it evaluates as string = "a" if 1 else "b" (since mylist length is 1) So, as the condition is True it assign the "a" to variable "string"… To make the answer you desired make the "mylist" as empty (mylist=[])
12th Apr 2020, 7:50 AM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
+ 2
But mylist is not False ,it is a list with 1 entry You mean mylist = [False] string = "a" if mylist[0] else "b" print(string)
12th Apr 2020, 7:52 AM
Oma Falk
Oma Falk - avatar
+ 2
Thanks Oma Falk, I think I have gained some sort of knowledge out of this discussion
12th Apr 2020, 8:28 AM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
+ 1
Booleans can be very confusing at times. But in your code, mylist is true because a list does exist. The code does not check the values in the list. It is just like when you type "False"(False in inverted commas, that is in string). When you remove the brackets, the output will be 'b'. The above syntax means: String=(Value if true) if (condition) else (Value if false).
12th Apr 2020, 7:52 AM
Taranjeet
Taranjeet - avatar
+ 1
Thanks for the quick answers, guys! I think I understood it now! Kiran Deep Naidu: I think the lenght of the list doesn't matter as long as it isn't empty.
12th Apr 2020, 8:20 AM
Felix Lipo
Felix Lipo - avatar
0
And here is the code: https://code.sololearn.com/ciB0dRT5gkse/?ref=app Happy Easter, guys!
12th Apr 2020, 7:40 AM
Felix Lipo
Felix Lipo - avatar
0
mylist = False string = "a" if mylist else "b" print(string)
12th Apr 2020, 7:49 AM
Justus
Justus - avatar
0
In my understanding it matters Felix Lipo, because any value except 0 results in True.
12th Apr 2020, 8:23 AM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
- 1
In my understanding, False --> 0 [False] --> [0] Since the array isn't empty "String = 'a' if mylist" is true, so it prints a Using False without the brackets mylist=False "string = 'a' if mylist" is False,prints b
12th Apr 2020, 9:52 AM
Sumrit Chauhan
Sumrit Chauhan - avatar