Python is a convenient language that’s often used for scripting, data science, and web development.
In this article, we’ll look at how to use booleans and conditional statements in our Python programs.
Boolean values take the value
False . They always start with uppercase.
They can be used in expressions like anything else. For example, we can write:
foo = True
Comparison operators are used for comparison 2 values ane evaluate operands to a single boolean value.
The following comparison operators are included with Python:
==— equal to
!=— not equal to
<— less than
>— greater than
<=— less than or equal to
>=— greater than or equal to
For example, we can write the following:
1 == 1
1 != 2
'hello' == 'Hello'
== is the equal to comparison operator, while the
= is the assignment operator that assigns the right operand to the variable on the left.
and operator takes 2 boolean values and then return one boolean value given the 2 operands.
True if both operands are
True . Otherwise, it returns
or operator takes 2 boolean values and returns one boolean value given the 2 operands.
True if one or both operands are
True . Otherwise, it returns
not operator is a unary operator, which means it takes one operand.
It returns the negated value of the operand. This means that
not True returns
not False returns
Mixing Boolean and Comparison Operators
We can mix booleans and comparison operators since comparison operators return booleans.
For example, we can write:
(1 < 2) and (4 < 5)
(1 == 2) and (4 == 5)
We can combine conditions and blocks of code to create a program that has flow control.
The conditions can be used with the
if or a combination of
elif , or a combination of
elife , and
Blocks are indented. They begin when indentation increases and they can have blocks nested in it.
Blocks end when the indentation decreases to zero or to the containing block’s indentation.
For example, we can write the following
print('Enter your name') name=input() if name == 'Mary': print('Hello Mary')
The code above asks for the name and displays ‘Hello Mary’ if the
name entered is
We can add a nested
if block as follows:
print('Enter your name') name=input() print('Enter your age') age=input() if name == 'Mary': print('Hello Mary') if int(age) < 18: print('You are a girl') else: print('You are a woman')
In the code above, we have a nested
if block that nest the
age check in the
We have the
else block which runs if the
int(age) < 18 returns
If we have more than 2 cases, we can use the
elif keyword for checking and running code if alternative cases are
For example, we can use it as follows:
print('Enter your name') name=input() if name == 'Mary': print('Hello Mary') elif name == 'Alex': print('Hello Alex') elif name == 'Jane': print('Hello Jane') else: print('I do not know you')
Now if we enter
Jane , we’ll see the
Hello sentences displayed. Otherwise, we see
I do not know you displayed.
Note that we always have a colon at the end of a
The blocks are also indented. This is mandatory in Python to denote blocks.
Booleans are variables that can take the value
Comparison operators can be used to build expressions from other values. We can compare numbers and check if strings are equal.
They return boolean values, so they can be combined with the name operators to return boolean values.
or operators are used to combining expressions with comparison operators.
We can then use them in
if statements to run code conditionally. For alternative cases, we can add them to
else keywords to denote them. They have to be used with