
In programming, sometimes we want values to be either True or False which has assigned in programming as Boolean values.
We can compare two values directly which will return True or False accordingly as a Boolean value.
Example
print(10 == 10) print(10 == 9) print(10 > 9) print(10 < 9)
Output
True False True False
In Python, we use bool() function to check True or False.
bool() function always returns True until we don’t pass something from the following–
- 0
- empty string
- empty list, tuple, dictionary
- other empty objects like a set
- anything like a function which returns a function
Example
print(bool(0)) # passing 0 print(bool("")) # passing an empty string print(bool([])) # passing an empty list print(bool({})) # passing an empty dict print(bool(())) # passing an empty set
Output
False False False False False
bool() will return True if the value is not an empty one.
Example
print(bool(1)) print(bool(12.2)) print(bool(23j)) print(bool("aString")) print(bool([1, 2, "Hello"])) print(bool({"Key":"value"}))
Output
True True True True True True
Keep Learning
Also Read:
- The system of the binary conversionThe binary number system defines a number in a binary system. You only find the number in a two-number system the 1 and the 0. The binary number system is an alternative system of representation to the decimal number system. The decimal number system is from (0 to 9) and has a base of 10…
- What is web development for beginners?Introduction In web development, we refer to website so web development refers to website development. “Web” word has been taken from the spider’s web because of the analogy that like a web is connected similarly websites are also connected to each other through the Internet. History of web development 3 Pillars of web development HTML…
- Guide to Proxy Servers: How They Work and Why You Need Them?What is a web proxy? During our college days, we often heard the term “proxy” which referred to the act of someone else marking our attendance, allowing us to be present in a class even if we were not physically there. In the context of the Internet, a web proxy works similarly, acting as an…
- Python | Check Armstrong Number using for loopAn Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. Consider 153. It has 3 digits, so we raise each digit to the power of 3(the number of digits) and sum them up:1^3 + 5^3 + 3^3 = 1 +…
- Python | Factorial of a number using for loopThe factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. The factorial of 0 is defined to be 1. The factorial formula can be expressed as: n! = 1 * 2 * 3 * … * n Code to calculate factorial of…