0

I'm trying to settle a debate here. We have a python-quiz in our cs-class with the question:

"The Expression behind the if has to be of the type boolean." True or false?

And I immediatly thought: Well you can put strings into ifs, so this has to be false.

My teacher is arguing otherwise. They say, because everything can be interpreted as a boolean, the statement from the quiz is true.

Who is right and why?

xtay2
  • 453
  • 3
  • 14
  • 1
    (almost) Everything in python is either truthy or falsy, and can be used in if statements. See [the docs](https://docs.python.org/3/library/stdtypes.html#truth) for a summary of what types are considered truthy or fasly. In general, empty collections, 0, and `None` are falsy everything else is truthy. – mousetail Dec 02 '22 at 06:53
  • Note: Truthy doesn't mean `True`. A truthy or falsy value is not the same as a boolean, just that it can be convereted to a boolean using the `bool()` built in function – mousetail Dec 02 '22 at 06:55
  • The concept of truthy and falsy values doesn't answer the question tho. I'm interested in the types that are allowed in an if. – xtay2 Dec 02 '22 at 06:58
  • @xtay2, you already know that you can use a string, so you have a trivial counterpoint. And mousetail has already pointed you at [the documentation](https://docs.python.org/library/stdtypes.html#truth-value-testing) in case you need a quote: "Any object can be tested for truth value, for use in an `if` or `while` condition or as operand of the Boolean operations below…" The reason _why_ this works, and a way to discuss this with your instructor, lies in the concept of truthy and falsy. – ChrisGPT was on strike Dec 02 '22 at 18:42

0 Answers0