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:
[sourcecode language=”Python3″]
print(bool(‘False’))
print(bool())
[/sourcecode]

  1. False, True
  2. None, None
  3. True, True
  4. 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:
[sourcecode language=”Python3″]
print(not(4>3))
print(not(5&5))
[/sourcecode]

  1. False, False
  2. None, None
  3. True, True
  4. 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:
[sourcecode language=”Python3″]
print([‘love’, ‘python’][bool(‘gfg’)])
[/sourcecode]

  1. love
  2. python
  3. gfg
  4. 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:
[sourcecode language=”Python3″]
mylist =[0, 5, 2, 0, ‘gfg’, ”, []]
print(list(filter(bool, mylist)))
[/sourcecode]

  1. [0, 0, ]
  2. [0, 5, 2, 0, ‘gfg’, ”, []]
  3. Error
  4. [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:
[sourcecode language=”Python3″]
if (7 < 0) and (0 < -7):
print("abhi")
elif (7 > 0) or False:
print("love")
else:
print("geeksforgeeks")
[/sourcecode]

  1. geeksforgeeks
  2. love
  3. abhi
  4. 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”.



My Personal Notes arrow_drop_up


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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.