Use of Boolean Datatype and Python Operators

Use of Boolean Datatype and Python Operators

We bring another post in the series of our python programming language course. As one of the best python training institute in Laxmi Nagar Delhi, we are providing this post related to the use of Boolean Datatype and Python Operators. So let’s move ahead.

bool () method

Use-of-Boolean-Datatype-in-python

The bool () method is used when we want to output or a conversion of a value to a boolean value i.e True or false following the standard testing procedure.

It generally takes only one paramere (let’s suppose X here). we can apply the standard truth testing procedure on it. If there is no parameter passed, then it always shows false by default. so passing a test is an option. It will show any of the two value in its output.

  • Its output shows true if the parameter ou a value is passed as true.
  • Its output shos false if the parameter or value is passed as false.

Here are a few situations where bool() method show false vale. Except these, all other it shows true for all other values.

  • In the output,  if an empty mapping is passed, such as {}.
  • In the output, if Objects of Classes having __bool__() or __len()__ method, returning 0 or False
  •  
  • In the output, a false value is passed.
  • If no value is passed
  • If an empty sequence like (), [], “, etc is passed in the output
  • If a zero is passed in any numeric type like 0, 0.0 etc
  • If an empty mapping is passed in output like {}
  • If an object of a class which have __bool__() or __len()__ method, in the output 0 or shows False.

It will be good if you learn Data Science course in delhi yourself by joining the PHP Training in Laxmi Nagar

Code

# Python program to illustrate

# built-in method bool()

# Returns False as x is False

x = False

print(bool(x))

# Returns True as x is True

x = True

print(bool(x))

Python program to illustrate

# False output as x is not equal to y

x = 5

y = 10

print(bool(x==y))

# False output as x is None

x = None

print(bool(x))

# False return as x is an empty sequence

x = ()

print(bool(x))

# False Returns  as x is an emty mapping

x = {}

print(bool(x))

# False Returns as x is 0

x = 0.0

print(bool(x))

# True Returns  as x is a non empty string

x = ‘GeeksforGeeks’

print(bool(x))

Output

False

True

False

False

False

False

False

True

It will be good if you learn python course in delhi yourself by joining the Django training in delhi

Python operators

There are some symbols in python which carry out arithmetic or logical computation. These are known as operators and operators operates the computation with operands.

>>> 2+3

5

Here “+” is the operator which allows performing addition. 2 & 3 are the operands and the result of it i.e 5 is an output of the operation.

Operator        –        Meaning              –                Example

Python operators

+     –           It show addition of two values                 –                       x + y+ 2

–        subtraction of two values                           –                  x – y- 2

*        multiplication of two values                          –            x * y

/       Division of left operands by the right one  –  x / y

%      Modulus – that reminds the division of left operand by the right  –   x % y (remainder of x/y)

//   –          Floor division – division that results into the whole number adjusted to the left in the number line                                                                                              –   x // y

**         –   Exponent – left sided operand raised to the power of right                      –     x**y (x to the power y)

Other operators are as follows

  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Special operators
  • Identity operators
  • Membership operators

We hope you have got some idea about how Boolean Datatype and Python Operators work and what is the application of it. For more interesting post keep updated yourself with our post. And if you want to explore new thing python under the guidance of experts then you can join our best python course in Delhi.

Recommended Blog:

https://www.youtube.com/watch?v=llXCz7ihqZohttps://www.youtube.com/watch?v=JJLsHGkdEwg&t=6s
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •