Skip to content
Related Articles

Related Articles

Improve Article

Output of Python Programs | Set 21 (Bool)

  • Difficulty Level : Easy
  • Last Updated : 11 Jun, 2021

Prerequisite : Boolean 
Note: Output of all these programs is tested on Python3
1. What is the output of the code: 
 

Python3




print(bool('False'))
print(bool())
  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: 
 

Python3




print(not(4>3))
print(not(5&5))
  1. False, False
  2. None, None
  3. True, True
  4. True, 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: 
 



Python3




print(['love', 'python'][bool('gfg')])
  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: 
 

Python3




mylist =[0, 5, 2, 0, 'gfg', '', []]
print(list(filter(bool, mylist)))
  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: 
 

Python3




if (7 < 0) and (0 < -7):
    print("abhi")
elif (7 > 0) or False:
    print("love")
else:
    print("geeksforgeeks")
  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
Recommended Articles
Page :