Thursday, June 30, 2022

What are boolean expressions?

The boolean expression is expression that can be True or False. There are multiple operators wich can be used to obtain boolean expression. First we are going to use "==" operator to see if two variables are equal or not. If they are equal the result will be True, and if they are not equal the result will be False.
Example 1 Test following expressions 4 == 40, 2 == 10, 4 == 4, 3 == 4, 5==5 and print the output.
Solution: Each expression will be tested inside print function. The form is to formulate string in form of "Is 5 = 5?" separated with comma and after that type the expression. The example of the print function formulation is shown for first expression (4 == 40).
print("Is 4 = 40?, 4 == 40)
Since 4 is not equal to 40 the result will be False. The other problems in this example are written in the same way. The entire code for this example is given below.
print("Is 4 = 40 ? ", 4==40)
print("Is 2 = 10 ? ", 2==10)
print("Is 4 = 4 ?", 4==4)
print("Is 3 = 4 ?", 3==4)
print("Is 5 = 5 ?", 5==5)
In this example we used the operator "==" which compares two operands and produces True if the two numbers are equal, and False if they are not equal. In first case we have two numbers 4 and 40. They are not equal so the output is False. In second line we have 2 and 10 and they are not equal so the output is false. In third line we compared two numbers 4 and 4 and the output is True. In fourth line of code the numbers are 3 and 4 and they are not equal so the output is False. In the last line of code we have two equal numbers (5 and 5) so the output is True. When the previous code is executed the obtained output is given below.
Is 4 = 40 ?  False
Is 2 = 10 ? False
Is 4 = 4 ? True
Is 3 = 4 ? False
Is 5 = 5 ? True
The True and False values are special values that belong to the type bool; they are not strings. To verify this we will use the type function and investigate the type of True and False.
print(type(True))
print(type(False))
The output is given below.
<type 'bool'>
<type 'bool'>
Besides the "==" operator there are other comparison operators and they are given in Table 1.
Table 1 - List of operators used to obtain boolean expressions
OperatorDescription
x == yx is equal to y
x != y x is not equal to y
x > yx is greater than y
x < yx is less than y
x >= yx is greater than or equal to y
x <= yx is less than or equal to y
x is yx is the same as y
x is not yx is not the same as y
Example 2 Test the following expressions and show the output using print function. The expression are:
  • 4 != 5,5 != 5
  • 2 < 6, 18 < 3
  • 14 > 12, 2 > 4
  • 12 >= 3, 4 >= 20
  • 14 <= 4, 2 <= 12
  • 30 is 20, 40 is 40
  • 20 is not 12, 12 is not 25
Solution: The output will be generated in the same way as it was generated in Example 1. The print function will consist of string and expression that are separated with comma.
print("4 != 5 is", 4 != 5)
print("5 != 5 is", 5 != 5)
print("2 < 6 is", 2 < 6)
print("18 < 3 is", 18 < 3)
print("14 > 12 is", 14 > 12)
print("2 > 4 is ", 2 > 4)
print("12 >= 3 is", 12 >= 3)
print("4 >= 20 is", 4 >= 20)
print("14 <= 4 is", 14 <= 4)
print("2 <= 12 is", 2 <= 12)
a = b = 30; c = 20
print("a = b =",a)
print("c =", c)
print("a is c? ", a is c)
print("a is b? ", a is b)
print("a is not b ", a is not b)
print("a is not c ", a is not c)
In the first code line print("4 != 5", 4 != 5) the Python will test the expression and see if 4 != 5. The operator != means not equal. In our case 4 is not equal to 5 so the output will be True. In the second code line print("5 != 5 is", 5 != 5). This expression will be Flase since 5 is equal to 5 and we are testing 5 != 5 (five not equal to 5). In third code line print("2 < 6 is ", 2 < 6) the Python will test if 2 < 6 or in words: if 2 is less than 6. Since 2 is less than 6 the return value will be True. In fourth code line print("18 < 3 is", 18 < 3) the Python will test if 18 < 3 or in words: if 18 is less than 3. Since 18 is greater than 3 the return value will be False. In fifth line print("14 > 12 is", 14 > 12) the Python will test and show the output of the expression 14 > 12 (is 14 greater than 12?). since 14 is greater than 12 the result will be True. In sixth code line print("2 > 4 is", 2 > 4) we want to see if 2 is greater than 4. Since 2 is less than 4 the return result will be False. In seventh code line print("12 >= 3 is", 12 >= 3) we want to see if 12 is greater then or equal to 3. Since 12 is greater than 3 the return result will be True. In eighth code line print("14 <= 4 is", 14 <= 4) we want to know is 14 less than or equal to 4. Since 14 is greater than 4 the reutrn result will be False. In ninth code line print("2 <= 12 is", 2 <= 12) we want to know if 2 is less then or equal to 12. Since 2 is less than 12 the expression is True.
To test the is and is not operators we have to define variables and assign values to those variables. The is operator is the identity operator thet checks whether both the operands refer to the same object or not. Two operands have to be present in the same memory location so that is value is Ture. In tenth line we have assigned one value (30) to two variables (a and b), and we have assinged a value of 20 to the variable c. In twelvth line we printed out the values of a and b, and in thirteenth code line we have created a code to show the output of variable c using print function. In fourtheenth line print("a is c?, a is c) we want to know if variable a is variable c. Since both variables are not stored in the same memory location the result of this test will be False. In fiftheenth line print("a is b?", a is b) we want to know if a is b. The result will be True since both a and b are stored in the same memory location. Finally in sixteenth and seventeenth code line we are testing the "is not" operator. This operator is inverse of is operator which means that if two variables are not stored at same memroy location the output will be True, and if the two values are stored at same location the result will false. In sixteenth line of code print("a is not b ", a is not b) will return "a is not b False" since a and b are stored at the same memory location. In the seventeenth line of code print("a is not c", a is not c) the result will be "a is not c True" since a and c are stored at different memory locations which confirms expression a is not c. The output is given below.
4 != 5 is True
5 != 5 is False
2 < 6 is True
18 < 3 is False
14 > 12 is True
2 > 4 is False
12 >= 3 is True
4 >= 20 is False
14 <= 4 is False
2 <= 12 is True
a = b = 30
c = 20
a is c? False
a is b? True
a is not b False
a is not c True

No comments:

Post a Comment