I came across this concept but i couldn't get it
my_list = [False]
string = "a" if my_list else "b"
print(string)
The answer for the above code is a. Please help me to understand it.
I came across this concept but i couldn't get it
my_list = [False]
string = "a" if my_list else "b"
print(string)
The answer for the above code is a. Please help me to understand it.
Python interprets an empty list as False and a filled list as True. Just like empty and full strings.
An empty list is treated as False while if anything is present inside the list (e.g 0 or False or anything
), the list will be treated as true value
string = "a" if my_list else "b"
During the condition checking in if block, as one element is present and it was not empty, the condition got True value and the 'a' came as output.
let's say that the list is empty, then the else part will be an answer.
my_list = []
string = "a" if my_list else "b"
print(string) # prints b