Booleans are most important aspects of programming languages.
Booleans are most important aspects of programming languages.
Booleans are the most important aspects of programming languages. Because they give codes the power to make decisions. It is only right to learn everything about them. When we compare two values in Python, we get a boolean answer.
In Python, booleans can have two values.
In Python, only True and False are Boolean class values.
## get class names of Boolean value True and False
print(type(True))
print(type(False))
<class 'bool'>
<class 'bool'>
Python0, False, None, or Empty
then everything else is True.# Use bool to turn values into boolean.
False values Output
print('False Values') False Values
print('bool(0) \t',end='')
print(bool(0)) bool(0) False
print('bool(False) \t',end='')
print(bool(False)) bool(None) False
print('bool() \t',end='')
print(bool()) bool(None) False
print('bool(None) \t',end='')
print(bool(None)) bool(None) False
True values Output
print('True Values') True Values
print('bool(1) \t',end='')
print(bool(1)) bool(1) True
print('bool(True) \t',end='')
print(bool(True)) bool(True) True
print('bool("this is a string") \t',end='')
print(bool('this is a string')) bool('this is a string') True
lis=[1,2,3]
print(f'{bool(lis)} \t',end='')
print(bool(lis)) bool([1,2,3])
Python offers numbers to be used as boolean values; any number other than 0 is considered True.
# True is greater than 10 Output
print(True>10) False
# False is equal to ,is equal to -1
print(False==-1) False
# True is equal to ,is equal to 1
print(True==1) True
# False is equal to ,is equal to 0
print(False==0) True
# false plus false is zero Output
print(False+False) 0
# true adds to true equals to two.
print(True+True) 2
Let’s use numbers as boolean values in conditional statements.
# use 0 as false
if 0:
print('True')
else:
print('False')
False
Python# use any number but 0 as false
if 1:
print('True')
else:
print('False')
True
Python# 25 as true
if 25:
print('True')
else:
print('False')
True
Python# -120 as true
if -120:
print('True')
else:
print('False')
True
Pythonnot
operatorand
operatoror
operatorA | B | not A | not B | A and B | A or B |
---|---|---|---|---|---|
True | True | False | False | True | True |
True | False | False | True | False | True |
False | True | True | False | False | True |
False | False | True | True | False | False |
As you can see, the above operators operate on boolean values.
Let’s look at some examples of these operators.
# see how or is used
print(' A B \tResult')
print('True or False \t',end='')
print(True or False)
# use true or true with or operator
print('True or True \t',end='')
print(True or True)
# use false or false with or operator
print('False or False \t',end='')
print(False or False)
A B Result
True or False True
True or True True
False or False False
Python# see how and is used
print(' A B \tResult')
print('True and False\t',end='')
print(True and False)
print('False and False\t',end='')
print(False and False)
print('True and True\t',end='')
print(True and True)
A B Result
True and False False
False and False False
True and True True
Python“not” is used to negate a statement’s value.
# see how to use not.
print('Statement\tOutput',end='')
print('not True\t',end='')
print(not True)
print('not False\t',end='')
print(not False)
print('not (False and False)\t',end='')
print(not(False and False))
Statement Output
not True False
not False True
not (False and False) True
Python==
!=
>
<
>=
<=
is
operatorin
operatorA boolean value is returned by comparison operators. When a statement is executed, it returns a boolean value.
is equal
to (==) operator# 'Apple' is equal to 'Apple'
print('Apple'=='Apple')
# 'Apple' is equal to 'Cherry Blossom'
print('Apple'=='Cherry Blossom')
True
False
Pythonis not
equal to (!=) operator# 'Apple' is not equal to 'Cherry Blossom'
print('Apple'!='Cherry Blossom')
# 4 is not equal to 5
print(4!=5)
True
True
PythonGreater than
(>) operatora=4
b=9
c=14
# b is greater than a
b>a
# a is greater than b
a>b
True
False
PythonLess than
(<) operatora=4
b=9
c=14
# b is lesser than a
b<a
# a is lesser than c
a<c
False
False
PythonGreater than or equal to
(>=
) operator# c is greater than equal to a
a=4
b=9
c=14
c>=a
True
Python
Lesser than or equal to
(<=
) operator# c is lesser than equal to b
a=4
b=9
c=14
c<=b
True
Pythonin
operator In operator is used to find values in iterable objects.
# find 'apple' in string
string='An apple a day, keeps the doctor away'
print('apple' in string)
# find 'banana' in string
print('banana' in string)
# confirm if there is not 'banana' in string
print('banana' not in string)
True
False
True
PythonThese statements can be used in code logic to control the flow of code.
# use in operator to control flow of logic.
if 'apple' in string:
print('Apple is present')
elif 'banana' in string:
print('Banana is present')
if 'banana' not in string:
print('banana is not present')
Apple is present
banana is not present
Pythonis
is not
is not
operator is the inverse of is
.# find if a is b
a=5
b=5
c=4
print(a is b)
# find if a is not c
print(a is not c)
True
True
Pythona=[1,2,3,4,5]
b=[1,2,3,4,5]
# find if the list a is equal to b list
print(a is b)
# find if the list a is not equal to b list
print(a is not b)
True
False
PythonOther data types, such as lists, sets, and tuples, can also be compared.
a=[1,2,3,4,5]
b=[1,2,3,4,5]
# find if the list a is equal equal to b list
print(1,a==b)
a=(1,2,3,4,5)
b=(1,2,3,4,5,6)
# find if the tuple a is not equal to b tuple
print(2,a!=b)
a={1,2,3,4,5,6}
b={1,2,3,4,5}
# find if the set a is greater than b set
print(3,a>b)
a={1:1,2:2,3:3,4:4,5:5}
b={1:1,2:2,3:3,4:4,5:5}
# find if the dictionary a is equal to equal to b dictionary
print(4,a==b)
1 True
2 True
3 True
4 True
Python# chain comparison operators
# 1 is less than 8 is greater than
print(1, 1 < 8 > 7)
# 1 is greater than 8 is greater than
print(2, 1 > 8 > 7)
# 1 is less than 8 is less than
print(3, 1 < 8 < 7)
# 1 is less than or 8 is less than
print(4, 1 < 8 or 8 < 7)
# 1 is less than or 8 is greater than
print(5, 1 < 8 and 8 > 7)
1 True
2 False
3 False
4 True
5 True
PythonRange selection using comparison operators and chaining operators
# create a list
num=[x for x in range(25)]
print(num)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
PythonAs you can see from the list above, there are 25 numbers. Let’s look at the Range selection.
# print numbers greater than 10
for i in num:
if i>10:
print(i,end=' ')
11 12 13 14 15 16 17 18 19 20 21 22 23 24
Python# print numbers lesser than 10
for i in num:
if i<10:
print(i,end=' ')
0 1 2 3 4 5 6 7 8 9
PythonIn the preceding examples, we saw ranges of greater than and less than ten. Let’s try picking a particular number from a group now.
# print numbers greater than 10 and less than 18
for i in num:
if i>10 and i<18:
print(i,end=' ')
11 12 13 14 15 16 17
Python# print numbers less than 10 and great than 4
for i in num:
if i<10 and i>4:
print(i,end=' ')
5 6 7 8 9
PythonBe cautious when configuring range conditions.
# print numbers greater than 10 and lesser than 0
for i in num:
if i>10 and i<0:
print(i,end=' ')
11 12 13 14 15 16 17 18 19 20 .......... Ꝏ
Python# create a list of prime and odd numbers
prime=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
odd=[x for x in range(51) if x%2==1]
print(prime)
print(odd)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49
Python# create a large chain rule
not 1 < 8 and 8 < 7 or 1 > 7 and 1 < 8
False
Python# create a large chain rule
not (1 < 8 and 8 < 7 or 1 > 7 and 1 < 8)
True
PythonAs shown in the preceding example, changing the order of these chains can result in a change in logic.
Exercise caution when constructing chains in your code.
Check out the example below.
# Creating a Short circuit example by avoiding an inevitable error
True or 1/0
True
Python# Creating a Short circuit example by avoiding an inevitable error
False or 1/0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-43-4197a8a91ebb> in <module>
1 # Creating a Short circuit example by avoiding an inevitable error
----> 2 False or 1/0
ZeroDivisionError: division by zero
PythonThis error is avoided because it is cut off by the obvious conclusion of “false” and being false.
False and 1/0
False
PythonThe above statement didn’t cause an error because and requires both conditions to be True . So if there is one False statement other statement won’t matter whether it is true or false, the result will be False.
Dictionary, List, Tuple, and Byte are used for creating boolean values.
# tuple as boolean
a=()
bool(a)
False
Python# bytes as boolean
a=bytes()
bool(a)
False
Python# bytes as boolean
a=bytes()
bool(a)
False
Python# bytes as boolean
a=bytes(123)
bool(a)
O/P:
True
Python# dictionary as boolean
a={}
bool(a)
False
Python# dictionary as boolean
a={1:2,3:4}
bool(a)
True
Python# list as boolean
a=[1,2,3,4]
bool(a)
True
Python# create a hollow function
def function1():
pass
# use function as bool value
bool(function1)
True
PythonHowever, if you call a function, it will be dependent on what it returns.
# using function returntype as bool value.
def function1():
pass
bool(function1())
False
Python# using function returntype as bool value.
def function1():
return True
bool(function1())
True
Python# using function returntype as bool value.
def function1():
return None
bool(function1())
False
PythonANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.
What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!
Start using NotebookLM today and embark on a smarter, more efficient learning journey!
This can be a super guide for you to start and excel in your data science career.
Solve this quiz for testing Manova Basics
Test your knowledge on pandas groupby with this quiz
Observe the dataset and try to solve the Visualization quiz on it
To perform ANCOVA (Analysis of Covariance) with a dataset that includes multiple types of variables, you’ll need to ensure your dependent variable is continuous, and you can include categorical variables as factors. Below is an example using the statsmodels library in Python: Mock Dataset Let’s create a dataset with a mix of variable types: Performing…
How useful was this post? Click on a star to rate it! Submit Rating
Complete the code by dragging and dropping the correct functions
Python functions are a vital concept in programming which enables you to group and define a collection of instructions. This makes your code more organized, modular, and easier to understand and maintain. Defining a Function: In Python, you can define a function via the def keyword, followed by the function name, any parameters wrapped in parentheses,…
Mastering indexing will significantly boost your data manipulation and analysis skills, a crucial step in your data science journey.
Stable Diffusion Models: Where Art and AI Collide Artificial Intelligence meets creativity in the fascinating realm of Stable Diffusion Models. These innovative models take text descriptions and bring them to life in the form of detailed and realistic images. Let’s embark on a journey to understand the magic behind Stable Diffusion in a way that’s…