0
if print("welcome"):
    print("heelloo!")
else:
    print("bbye!")

This piece of code prints both welcome and bbye. So, is the if condition never executed, or is it something else? Kindly explain as I am fairly new to Python.

1
  • if print("welcome")?
    – Mangohero1
    Sep 28, 2017 at 15:21

2 Answers 2

4

There is supposed to be a condition after the word if. You've put there a print function call. What the if statement will do is that it will call the print function (hence printing your "welcome") and decide whether to execute the if branch or the else branch, by looking at the return value of print.

print always returns None. None is considered a false value (so is 0 btw), so the else branch executes, printing "bye".

0
3

The print function returns None which evaluates to False. Hence both will be printed. You can check the return value of the print function with this,

print(print("welcome")) # prints None which evaluates to False in if
1
  • 1
    print(print("welcome")) will print welcome first, then None.
    – Sweeper
    Sep 28, 2017 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.