-1

I have recently stumbled upon this bit of code:

def equal_button_press(self):  
    if self.add_trigger or self.sub_trigger or self.mult_trigger or self.div_trigger or self.asin_trigger:

In the if statement there is no specification for the code to check whether any of the variables self.add_trigger, self.sub_trigger, self.mult_trigger, self.div_trigger or self.asin_trigger are true.

Does this mean that it is possible to check if the object is true without the == operator?

Skully
  • 2,882
  • 3
  • 20
  • 31
Ducky
  • 7
  • 3
  • 9
    `==` should *never* be involved in checking whether something is true. – user2357112 Oct 29 '21 at 23:10
  • 1
    See here, https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/ – Blast Off Bronze Oct 29 '21 at 23:11
  • 1
    See also: [What is Truthy and Falsy? How is it different from True and False?](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false) – sj95126 Oct 29 '21 at 23:13

3 Answers3

2

The == operator compares the value or equality of two objects, not if they are True. If the 2 objects are equal, then python will return True.

Here's a pretty great article going in depth about this: https://towardsdatascience.com/comparison-in-python-is-not-as-simple-as-you-may-think-a83eec69ab54

That being said, there are truthy and falsy values in Python:

Values that evaluate to False are considered Falsy. Values that evaluate to True are considered Truthy.

Some truthy values include:

  • Non-empty sequences or collections

  • Numeric values that are not zero.

Some falsy values include:

  • Zero of any numeric type.
  • Empty sequences or collections
  • None and False

So the code is checking which values are truthy, and those values will be outputted.

With the Boolean OR operator, you can connect two Boolean expressions into one compound expression. At least one subexpressions must be true for the compound expression to be considered true, and it doesn’t matter which. If both subexpressions are false, then the expression is false.

krmogi
  • 2,588
  • 1
  • 10
  • 26
0

The clause inside of an if statement to determine whether it will be executed or not is not specifically only done when a value is equal to true (In Python, True) but can also be triggered if the variable or logical statement returns any value that is not 'falsy'.

@sj95126 provided the exact answer in the comments that I was also going to recommend on this!

Documentation Reference: https://docs.python.org/3/library/stdtypes.html#truth-value-testing

Skully
  • 2,882
  • 3
  • 20
  • 31
0

Yes, it is possible to check if a value is True without the == operator. An if statement takes a boolean after the if keyword. The == operator checks if the 2 values on both sides are the same. If they are, it "returns" True. Otherwise, it "returns" False.

foo = True

# These if statments are the same
if foo == True:
    print("foo is True")

if foo:
    print("foo is True")
fCode
  • 16
  • 1