Computer Science, asked by jshyninair100, 5 months ago

15. What is the output produced by following code ?
a, b = bool(®), bool(0.0)
c, d = str(©), str(0.0)
print (len(a), len(b))
print (len(c), len(d))​

Answers

Answered by saran00766
4

search

Python Tutorial

#Must Read

Python Interview Questions

Python bool()

2 Comments

Filed Under: Python

Home » Python » Python bool()

Python Tutorial

1. Python Tutorial for Beginners

2. Python Keywords and Identifiers

3. Python Comments and Statements

4. Python Data Types

5. Python Input Output Import

6. Python Operators

7. Python Variable

8. Python Namespace Variable Scope

9. Python If Else

10. Python For Loop

11. Python While Loop

12. Python break continue

13. Python switch case

14. Python ternary operator

15. Python pass statement

16. Python Loop Example

17. Python Functions

18. Python main function

19. Python print

20. Python print format

21. Python print to file

22. Python input

23. Python import

24. Python Read File

25. Python Recursion

26. Python Anonymous Function

27. Python lambda

28. Python PIP

29. Python command line arguments

30. Python Numbers

31. Python Random Number

32. Python String to int

33. Python Exception Handling

34. Python Custom Exception

35. Python Inheritance

36. Python super

37. Python Multiple Inheritance

38. Python Operator Overloading

39. Python __str__ and __repr__

Python bool() function returns Boolean value for an object. The bool class has only two instances – True and False. This class can’t be extended.

Table of Contents

[

hide

]

1 Python bool()

2 Python bool() example

3 Python bool() with strings

4 Python bool() with numbers

5 Python bool() function with collections and sequences

6 Python bool() function with custom object

Python bool()

Python bool() function uses standard truth testing rules to convert the specified argument object to Boolean value.

Some of the rules used to return Boolean value are:

Any object Boolean value is considered true if it’s not implementing __bool__() function and __len__() functions.

If the object doesn’t define __bool__() function but defines __len__() function, then __len__() function is used to get the boolean value of object. If __len__() returns 0, then bool() function will return False otherwise True.

Boolean value will be False for None and False constants.

Boolean value will be False for zero value numbers such as 0, 0.0, 0j, Decimal(0), and Fraction(0, 1).

Boolean value will be False for empty sequences (tuple, dict) and collections, such as ”, (), [], {} etc.

Python bool() example

Let’s look at some simple examples of bool() with bool instances and None.

x = True b = bool(x) print(type(x))

# <class 'bool'>

print(type(b))

# <class 'bool'>

print(b)

# True

x = False b = bool(x) print(b)

# False

x = None b = bool(x) print(type(x))

# <class 'NoneType'>

print(type(b))

# <class 'bool'>

print(b)

# False

The output is self-explained and provided in the comments.

Python bool() with strings

# string examples

x =

'True'

b = bool(x)

print

(

type

(x))

# <class 'str'>

print

(

type

(b))

# <class 'bool'>

print

(b)

# True

x =

'False'

b = bool(x)

print

(b)

# True because len() is used

x =

''

print

(bool(x))

# False, len() returns 0

Similar questions