Categories
Python

Intro to Python Boolean and Conditional Statements

Spread the love

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

Boolean values take the value True or False . They always start with uppercase.

They can be used in expressions like anything else. For example, we can write:

foo = True

Comparison Operators

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

returns True .

1 != 2

returns False .

'hello' == 'Hello'

also returns False .

== is the equal to comparison operator, while the = is the assignment operator that assigns the right operand to the variable on the left.

Boolean Operators

The and operator takes 2 boolean values and then return one boolean value given the 2 operands.

It returnsTrue if both operands are True . Otherwise, it returns False .

The or operator takes 2 boolean values and returns one boolean value given the 2 operands.

It returns True if one or both operands are True . Otherwise, it returns False .

The 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 False and not False returns True .

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)

which returns True .

Or:

(1 == 2) and (4 == 5)

which returns False .

Flow Control

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 if , elif , or a combination of if , elife , and else together.

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 if block;

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 'Mary' .

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 name check.

We have the else block which runs if the int(age) < 18 returns False .

If we have more than 2 cases, we can use the elif keyword for checking and running code if alternative cases are True .

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 Mary , Alex or 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 if , elif and else lines.

The blocks are also indented. This is mandatory in Python to denote blocks.

Conclusion

Booleans are variables that can take the value True or False .

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.

and and 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 elif and else keywords to denote them. They have to be used with if blocks.

Leave a Reply

Your email address will not be published. Required fields are marked *