-1

When making an 'if' statement in Python, we have the option to use if boolean:, without making an explicit comparison (which is not mandatory)

The question is: what is the default comparison in this case? Is if x: equivalent to if x == True or if x is True?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    There is no default comparison. `if` statements are not based on comparisons. – user2357112 May 10 '20 at 21:44
  • 1
    the statement following `if` only has to evaluate to some form of python's "truthiness". it doesn't matter how you get there – Paul H May 10 '20 at 21:44
  • The *expression* following the `if` is [simply evaluated](https://docs.python.org/3/reference/expressions.html#index-86). There is no need to add `== True` because the expression before this would already necessarily need to evaluate to `True`. You might as well write `if (x == True) == True`. – Jongware May 10 '20 at 21:48
  • 1
    I think the downvotes here are harsh, it's a perfectly reasonable question for someone learning, perhaps it could do with some background research... – juanpa.arrivillaga May 10 '20 at 21:52

1 Answers1

-2

Not sure if I understand your question, but you can use:

if True:
    print("hi")

which will return "hi" once

If you're looking to make an infinite loop, try something like this:

while True:
    #do whatever here and it will loop infinitely
PythonNerd
  • 57
  • 1
  • 8