Output of Python Programs | Set 21 (Bool)
Prerequisite : Boolean
Note: Output of all these programs is tested on Python3
1. What is the output of the code:
print(bool('False')) print(bool()) |
- False, True
- None, None
- True, True
- True, False
Output:
4. True, False
Explanation: If the argument passed to the bool function does not amount to zero then the Boolean function returns true else it always returns false. In the above code, in first line ‘False’ is passed to the function which is not amount to 0. Therefore output is true. In the second line, an empty list is passed to the function bool. Hence the output is false.
2. What is the output of the code:
print(not(4>3)) print(not(5&5)) |
- False, False
- None, None
- True, True
- Ture, False
Output:
1. False, False
Explanation: The not function returns true if the argument is false, and false if the argument is true. Hence the first line of above code returns false, and the second line will also returns false.
3. What is the output of the code:
print(['love', 'python'][bool('gfg')]) |
- love
- python
- gfg
- None
Output:
2. python
Explanation: We can read the above code as print ‘love’ if the argument passed to the Boolean function is zero else print ‘python’. The argument passed to the Boolean function in the above code is ‘gfg’, which does not amount to zero and hence the output is: “python”.
4. What is the output of the code:
mylist =[0, 5, 2, 0, 'gfg', '', []] print(list(filter(bool, mylist))) |
- [0, 0, ]
- [0, 5, 2, 0, ‘gfg’, ”, []]
- Error
- [5, 2, ‘gfg’]
Output:
4. [5, 2, 'gfg']
Explanation: The code above returns a new list containing only those elements of the list mylist which are not equal to zero. Hence the output is: [5, 2, ‘gfg’].
5. What is the output of the code:
if (7 < 0) and (0 < -7): print("abhi") elif (7 > 0) or False: print("love") else: print("geeksforgeeks") |
- geeksforgeeks
- love
- abhi
- Error
Output:
2. love
Explanation: The code shown above prints the appropriate option depending on the conditions given. The condition which matches is (7>0), and hence the output is: “love”.
Recommended Posts:
- Output of Python programs | Set 8
- Output of Python programs | Set 7
- Output of Python Programs | Set 19 (Strings)
- Output of Python Programs | Set 24 (Sets)
- Output of Python Programs | Set 22 (Loops)
- Output of Python Programs | Set 20 (Tuples)
- Output of Python programs | Set 9 (Dictionary)
- Output of Python Programs | (Dictionary)
- Output of Python Programs | Set 18 (List and Tuples)
- Output of Python programs | Set 10 (Exception Handling)
- Output of Python Programs | Set 23 (String in loops)
- Output of C programs | Set 63
- Output of C++ programs | Set 42
- Output of C++ programs | Set 21
- Output of C programs | Set 48
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



