In Programming, we deal with problems by using different programming languages. We often need to know whether the given expression evaluates to true or false. If a certain condition is given it can have only two outcomes either 0 or 1, 0 means False and 1 means true.
when you compare two values, the expression is evaluated and it returns a boolean value.
For example:
x = 20
y = 30
print(x>y)
print(100<50)
print(30>40)
When you run the above code it returns true or false.
When you want to print the message based on true and false conditions using if.
x = 100
y= 200
if x >y:
print('x is greater than y ')
else:
print('y is greater than x ')
the above code will print x is greater than y if the condition is true else y is greater than x gets printed.
Most values are true.
Any value is evaluated to be true if it has some content.
Any string is evaluated to be true except empty.
Any number is evaluated to be true except 0.
Any list, tuple, or dictionary is true except empty ones.
bool('hello')
bool(123)
bool('python','java','php')
Some values are false
bool(none)
bool(False)
bool(" ")
bool(())
boo([])
bool({})
Comments
Post a Comment