{"id":24565,"date":"2021-10-21T10:10:19","date_gmt":"2021-10-21T04:40:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=24565"},"modified":"2021-11-05T19:37:48","modified_gmt":"2021-11-05T14:07:48","slug":"python-bool-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-bool-function-with-examples\/","title":{"rendered":"Python bool() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for abs() Function<\/a> Using the standard truth testing procedure, the Python bool() function returns or converts a value to a Boolean value, i.e., True or False.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n In general, the bool() method takes only one parameter (value), to which the standard truth testing procedure can be applied. If no parameters are passed, it returns False by default. As a result, passing a parameter is optional.<\/p>\n Return Value:<\/strong><\/p>\n bool() function can only return one of two values.<\/p>\n Here are some examples of when Python’s bool() method returns false. Except for these, all other values are True.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Program for abs() Function bool() Function in Python: Using the standard truth testing procedure, the Python bool() function returns or converts a value to a Boolean value, i.e., True or False. Syntax: bool([value]) Parameters In general, the bool() method takes only one parameter (value), to which the …<\/p>\n
\nbool() Function in Python:<\/strong><\/p>\nbool([value])<\/pre>\n
\n
\n
Given value = False<\/pre>\n
False<\/pre>\n
Given value = 'btechgeeks'<\/pre>\n
True<\/pre>\n
Program for bool() Function in Python<\/h2>\n
\n
Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the value as False and store it in a variable.\r\np = False\r\n# Get the Boolean value of the given value using the bool() function\r\n# and print the result.\r\nprint(bool(p))\r\n# Give the value as True and store it in another variable.\r\nq = True\r\n# Get the Boolean value of the given value using the bool() function\r\n# and print the result.\r\nprint(bool(q))\r\n# Give the two numbers as static input and store them in two separate variables.\r\na = 4\r\nb = 4\r\n# Check if the given two numbers are equal using the bool() function\r\n# and print the result.\r\nprint(bool(a == b))\r\n# Give the value as None and store it in another variable.\r\ns = None\r\n# Get the Boolean value of the given value using the bool() function and\r\n# print the result.\r\nprint(bool(s))\r\n# Take an empty tuple and store it in another variable.\r\nt = ()\r\n# Get the Boolean value of the given empty tuple using the bool() function\r\n# and print the result.\r\nprint(bool(t))\r\n# Take an empty set and store it in another variable.\r\nv = {}\r\n# Get the Boolean value of the given empty set using the bool() function\r\n# and print the result.\r\nprint(bool(v))\r\n# Give the floating value as static input and store it in another variable.\r\ng = 0.0\r\n# Get the Boolean value of the given value using the bool() function\r\n# and print the result.\r\nprint(bool(g))\r\n# Give the string as static input\u00a0and store it in another variable.\r\nd = 'btechgeeks'\r\n# Get the Boolean value of the given string using the bool() function\r\n# and print the result.\r\nprint(bool(d))\r\n<\/pre>\n
False\r\nTrue\r\nTrue\r\nFalse\r\nFalse\r\nFalse\r\nFalse\r\nTrue<\/pre>\n
Example for Even or Odd Program using bool() Function<\/h4>\n
def Even_or_odd(gvn_numb):\r\n return(bool(gvn_numb % 2 == 0))\r\n\r\n\r\ngvn_numb = 9\r\nif(Even_or_odd(gvn_numb)):\r\n print(\"The given number is Even\")\r\nelse:\r\n print(\"The given number is Odd\")\r\n<\/pre>\n
The given number is Odd<\/pre>\n
\n