Output of Python Programs | Set 21 (Bool)

Prerequisite : BooleanNote: Output of all these programs is tested on Python3

1. What is the output of the code:

filter_none

editclose

play_arrow

linkbrightness_4code

print(bool('False'))

print(bool())

chevron_right

filter_none

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:

filter_none

editclose

play_arrow

linkbrightness_4code

print(not(4>3))

print(not(5&5))

chevron_right

filter_none

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:

filter_none

editclose

play_arrow

linkbrightness_4code

print(['love', 'python'][bool('gfg')])

chevron_right

filter_none

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:

filter_none

editclose

play_arrow

linkbrightness_4code

mylist =[0, 5, 2, 0, 'gfg', '', []]

print(list(filter(bool, mylist)))

chevron_right

filter_none

[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:

filter_none

editclose

play_arrow

linkbrightness_4code

if(7< 0) and(0< -7):

print("abhi")

elif(7> 0) orFalse:

print("love")

else:

print("geeksforgeeks")

chevron_right

filter_none

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”.

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.