filmov
tv
Python Booleans: 7 Ways To Use (2 Min) | Boolean Operators

Показать описание
In this tutorial, you'll learn how to use Boolean values, operators in Python.
—
—
Video Transcript:
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn seven ways to use booleans in Python. Number one, you can evaluate expressions using greater than less than, and equal to equal to operators.
Here I'm checking whether 2 is greater than 1, 3 is less than 2 and 2 is equal equal to 3. Number two, you can also use the bool function to evaluate a given value. Any string or numerical value will be considered true whereas an empty string or a 0 will be considered false similarly a list with values will be considered true.
However, an empty list will be considered false. Note, the none keyword is also considered false. Number three, you can use the type function to check whether true or false belongs to the bull class or not.
Number four a function can return true or false. Here is my function evaluates to true then I'm printing true else I'm printing false. Number five, booleans are considered numeric data types.
Here, I'm checking if true is equal equal to one, false is equal equal to zero and I'm also adding true plus true. Number six, the not operator returns the opposite boolean value so not of false will return true, and not of true will return false. Last but not the least number seven. You can also use the and or operators between two boolean values.
True and false will return false whereas true or false will return true. There you have it. Make sure you like, subscribe, and turn on the notification bell.
—
# 1. Evaluate expressions using [removed] [removed] ==
print("1a.", 2 [removed] 1)
print("1b.", 3 [removed] 2)
print("1c.", 2 == 3)
# 2. Use bool() function to evaluate a given value
print("2a.", bool("String"))
print("2b.", bool(""))
print("2c.", bool(10))
print("2d.", bool(0))
print("2e.", bool([1, 2, 3]))
print("2f.", bool([]), bool(()), bool({}))
print("2g.", bool(None))
print("2h.", bool(False))
# 3. type() function to check True or False
print("3a.", type(True))
print("3b.", type(False))
# 4. Function can return true or false
def my_func():
return False
print("4. True was returned") if my_func() else print("4. False was returned")
# 5. Booleans are considered numeric data types
print("5.", True == 1, False == 0, True + True)
# 6. not operator returns the opposite result
print("6a.", not False)
print("6b.", not True)
# 7. and/or operators
print("7a.", True and False)
print("7b.", True or False)
—
—
Video Transcript:
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn seven ways to use booleans in Python. Number one, you can evaluate expressions using greater than less than, and equal to equal to operators.
Here I'm checking whether 2 is greater than 1, 3 is less than 2 and 2 is equal equal to 3. Number two, you can also use the bool function to evaluate a given value. Any string or numerical value will be considered true whereas an empty string or a 0 will be considered false similarly a list with values will be considered true.
However, an empty list will be considered false. Note, the none keyword is also considered false. Number three, you can use the type function to check whether true or false belongs to the bull class or not.
Number four a function can return true or false. Here is my function evaluates to true then I'm printing true else I'm printing false. Number five, booleans are considered numeric data types.
Here, I'm checking if true is equal equal to one, false is equal equal to zero and I'm also adding true plus true. Number six, the not operator returns the opposite boolean value so not of false will return true, and not of true will return false. Last but not the least number seven. You can also use the and or operators between two boolean values.
True and false will return false whereas true or false will return true. There you have it. Make sure you like, subscribe, and turn on the notification bell.
—
# 1. Evaluate expressions using [removed] [removed] ==
print("1a.", 2 [removed] 1)
print("1b.", 3 [removed] 2)
print("1c.", 2 == 3)
# 2. Use bool() function to evaluate a given value
print("2a.", bool("String"))
print("2b.", bool(""))
print("2c.", bool(10))
print("2d.", bool(0))
print("2e.", bool([1, 2, 3]))
print("2f.", bool([]), bool(()), bool({}))
print("2g.", bool(None))
print("2h.", bool(False))
# 3. type() function to check True or False
print("3a.", type(True))
print("3b.", type(False))
# 4. Function can return true or false
def my_func():
return False
print("4. True was returned") if my_func() else print("4. False was returned")
# 5. Booleans are considered numeric data types
print("5.", True == 1, False == 0, True + True)
# 6. not operator returns the opposite result
print("6a.", not False)
print("6b.", not True)
# 7. and/or operators
print("7a.", True and False)
print("7b.", True or False)