I was testing python code and I faced some questions. I've learned that when using conditional statement, if
, you need to write conditions that comes out as an boolean value, true
,false
.
However, when a list is placed in the position of a conditional expression, Python determines the truthfulness based on whether the list is empty or not.
Why does python determines like that? I want to know what is going on inside python more deeply.
I thought syntax error will occur because I didn't put boolean variable. But code went just right.
The code that I was testing with is here:
programming = ["Python", "Java", "C++"]
math = ["Algebra", "Calculus"]
pe = ["Running", "Swimming"]
if programming and math and pe:
for i in range(m):
print(programming[i] + 1, end=' ')
print(math[i]+ 1, end=' ')
print(pe[i]+ 1)
else:
print(0)