So far, we have learned how to collect basic data from the user. Wouldn’t it be useful if we could do different things depending on what the user typed in? Well, this happens to be a very core concept of computer programming, and we can do exactly as previously described with these things called conditional statements. These are basic statements which allow us to do certain things only in certain conditions.

Before we dive into the world of conditions, it’s important that you understand how relational operators work.

RELATIONAL OPERATORS

The relational operators compare one value to another. The comparison operators are ==, !=, <, >, <=, and >=. All of the relational operators result in a Boolean value.

The relational operators have the following general meaning:

OPERATOR DESCRIPTION EXAMPLE
== Checks if the values of two operands are equal or not, if yes then condition becomes true. (A==B) is not true.
!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. A=15
B=12
(A != B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

NOTE:
There is a difference between x=y and x==y.

  • x=y will assign the value of y to x
  • x==y will check to see if x is equal to y

Sample Python Code

Without getting into how the code runs at this point. Let’s take a look at some of the conditional statements at work. Executing the following Python script:

[python]
a = 21
b = 10
c = 0

if ( a == b ):
print "Line 1 – a is equal to b"
else:
print "Line 1 – a is not equal to b"

if ( a != b ):
print "Line 2 – a is not equal to b"
else:
print "Line 2 – a is equal to b"

if ( a < b ):
print "Line 3 – a is less than b"
else:
print "Line 3 – a is not less than b"

if ( a > b ):
print "Line 4 – a is greater than b"
else:
print "Line 4 – a is not greater than b"

a = 5;
b = 20;
if ( a <= b ):
print "Line 5 – a is either less than or equal to b"
else:
print "Line 5 – a is neither less than nor equal to b"

if ( b >= a ):
print "Line 6 – b is either greater than or equal to b"
else:
print "Line 6 – b is neither greater than nor equal to b"
[/python]

will produce the following result:

[python]
Line 1 – a is not equal to b
Line 2 – a is not equal to b
Line 3 – a is not less than b
Line 4 – a is greater than b
Line 5 – a is either less than or equal to b
Line 6 – b is either greater than or equal to b
[/python]

You Try

Copy the code above and execute it. Does it create the result you expected?

Check Your Understanding

Before moving to the next section, let’s see how well you know the current material.

  1. What are the seven relational operators in Python? Give their descriptions
  2. What is the difference between speed=5 and speed==5?
  3. How does A<=B differ from A
  4. What is the difference between A!=B and A<>B?