Before stepping into more programming, let's study some basic stuff but of great importance; 'Boolean'.
Just as an integer can take a value of -1, 1, 0, etc
and float can take 0.01, 1.2, etc
Boolean is something which can either be true or false.
print type(True) print type(False)
Type of 'True' and 'False' is bool (bool is for Boolean). And we can also assign any variable 'True' or 'False'. See the example given below.
x = True y = False print x print y
Now, let's do something more. We have already done x = 10 and we know that it means x is 10.
x = 10 print x == 10
'==' operator is used to compare. It is equivalent of asking whether x is 10 or not?
Do this one more time.
x = 10 y = 10 print x == y
Here it checks if x and y are equal.
name = "Sam" print name == "Sam" print name == "Aam"
Here, = assigns a value "Sam" to name and == checks whether name is "Sam" or not.
AND and OR
See this chart first.
So, if we use and with any two operands and if both of them are True, then the result is True. Otherwise, it is False
And if we are using or and if any of the two operands is True, then it is True and it will be False if both the operands are False.
'and' can be understood as both ( first and second both )
'or' can be understood as either ( first or second any ). See the next line to understand it more clearly.
'and' and 'or' of programming are very much similar to English words 'and' and 'or'.
A and B - Both A and B.
A or B - Either A or B.
A and B - Both A and B.
A or B - Either A or B or both.
So, if you are writing A and B, then the expression is true if both A and B are true. Whereas, if you are writing A or B, then the expression is true if either A or B or both are true.
True or False -> As 'or' is used, either of the two is true -> True
True and False -> As 'and' is used, both are not true -> False
Try to understand this or just remember the chart.
x = 10 y = 20 print x == 10 and y == 20 print x == 3 or y == 20
Here, x is 10 and y is 20. So, x==10 is True and also y == 20 is True. So, x == 10 and y == 20 is also True as both operands are true ( and is used ).
In the next line, x == 3 is False but y == 20 is True. So, x == 3 or y == 20 is True because atleast one operand is True ( or is used ).
not -> You can understand 'not' by thinking that it will do the opposite.
not False is True
not True is False
Yeah! It is simple.
not (True or False) -> False
( True or False ) is True because or is used and at least one of the operands is True. Then ( not (True) ) is False.
x = 10 y = 20 print not(x == 10 and y == 20) print not(x == 3 or y == 20)
This is the same example as the previous one. We just used not here and see the answers got reversed. Earlier, those were True but now those are False.
First look at the following table:
|!=||Not equal to||(5 != 2) is True, (5 != 5) is False|
|>||Greater than||(5 > 5) is False|
|<||Less than||(5 < 5) is False|
|>=||Greater than or equal to||(2 >= 2) is True|
|<=||Less than or equal to||(5 <= 2) is false|
!= is not equal to operator. It gives True if both the operands are not equal else it gives False.
> is greater than or equal to operator. It gives True if the first operand is greater than the second operand.
>= is greater than or equal to operator. It gives True if the first operand is greater than or equal to the second operand.
Similarly, < and <= are less than and less than or equal to operators respectively.
You will learn more about operators in the next chapter.
Talent is good, Practice is better, and Passion is Best
-Frank Lloyd Wright