DEV Community

Msaghu
Msaghu

Posted on • Updated on

Day 5: Conditionals and Booleans

Boolean Values

True or False
True is a Boolean value, however, TRUE/true/'true'/'True' are not
They can be assigned to variables and can be put into tuples or lists
Image description

Boolean Operators

and, or, not

Truth values

We can find the truth value of any function by passing the bool function.
The result will be True or False depending on the truth value of what we passed in the code
Some values are inherently falsy;
*Any numeric representation of zero i.e. 0, 0.0
*The values None and False
*Empty sequences i.e. empty strings, empty tuples, empty lists
Image description

Comparison Operators

There are 8 operators that yield a boolean function:
< | yields True if the 1st value is less in value than the 2nd value

| yields True if the 1st value is greater in value than the 2nd value
<= | yields True if the 1st value is less than or equal to the 2nd value
= | yields True if the 1st value is greater than or equal to the 2nd value
== | yields True if the two values are equal in value
!= | yields True if the 1st value is NOT the second value
is | yields True only if the two values are same
Image description

Conditional Statements

if, else, elif
These are used to control the flow of code
IF only allows a block of code to run, only when a particular condition has been met
ELSE used when we have to compare 2 statements, where you judge one based on the outcome of the other, is executed ONLY when the if statement is false. It does not have a condition
ELIF statement is used when you have many conditions that you want executed, it only follows an IF statement or another ELIF statement, and provides conditions that is checked only if any of the previous conditions were false, it contains a condition
The print function after the if conditional statement should have 4 indentation spaces
Image description

Nested If Statement

Exercise 5

Image description

Top comments (0)