Learn about Boolean in Python

Introduction

A boolean variable is one that has the values True or False. We state that a variable's data type may be boolean. It resembles a number that can either be 0 or 1 in a numerical context. Electronics either have high or low voltage. You can compare it to a light switch because it can only be turned on or off. It is based on a bit, the smallest computing unit. 0 (true) or 1 (false) is the value of a bit. For now, you can think of "bit" as a synonym for "boolean."

Boolean in Python

A boolean value has two possible outcomes: false or true. Boolean built-ins in Python are capitalized, such as True and False. The data type can be set to boolean without having to be defined explicitly. As in C or Java, you don't need to explicitly state that you wish to use a boolean. As a result of the value you enter, Python understands that the variable is a boolean. Compare the boolean definition code below:

# Java
boolean fun = true;

# Python
fun = true

The foundation of Python and the majority of computer languages is boolean logic. It enables programmers to apply standard algorithms, run conditional statements, and do comparisons. Python's comparison operators include the "greater than" (>) and "equals to" (==) symbols, while its logical operators include and and or. The usage of Python's Boolean operators is covered in this lesson, along with explanations of Boolean logic and expressions.

Python requires the following syntax to define a boolean:

a = False

This generates a boolean with the value False and the variable name (a). You would type: if you wanted to turn it on.

a = True

The print function can be used to display a variable's value. You can simply type the variable name in the Python shell:

>>> a = True
>>> print(a)
True
>>> a
True
As an alternative, type
>>> light = 0
>>> light
0

Even though you might assume that's a boolean, if you ask for the type, you'll get int (number, integer):

>>> type(light)
<class 'int'>

To turn a number into a boolean in Python, you must call the bool() method.

Things to remember

  1. A genuine number of any size, whether positive or negative, can never be false. False are the numbers 0 and 0.0.
  2. When performing mathematical operations on real numbers, rounding mistakes might produce results that are unclear or deceptive.
  3. Even though a variable "should" be zero, rounding processes may result in a very tiny non-zero value.
  4. This would be a True evaluation. A text, list, set, or dictionary that is empty evaluates to False.
  5. Data structures or strings that are not empty are True. None is a unique Python value that is False.
  6. The special values NaN (for non-representable or undefinable values), inf, and -inf are all True. A function is True at all times.

Cast boolean

Using the function bool()., you may convert a value to a boolean. The data type of a variable is altered through casting. The data type in the example below changes from int to boolean.

>>> x = 1
>>> b = bool(x)
>>> print(b)
True
>>> x = 0
>>> b = bool(x)
>>> print(b)
False

Using the type() method call, which returns the data type, you can confirm this.

>>> x = 1
>>> type(x)
<class 'int'>
>>> x = bool(x)
>>> type(x)
<class 'bool'>

Boolean strings

Functions occasionally return Boolean values. If you define a string, you can call multiple methods, each of which returns a boolean value.

>>> s = "Hello World"
>>> s.isalnum()
False
>>> s.isalpha()
False
>>> s.isdigit()
False
>>> s.istitle()
True
>>> s.isupper()
False
>>> s.islower()
False
>>> s.isspace()
False
>>> s.endswith("d")
True
>>> s.startswith("H")
True
Different boolean values will be returned if the string value s is changed.
>>> s = "12345"
>>> s.isdigit()
True
>>> s.endswith("d")
False

write your code here: Coding Playground