DEV Community

Steve Mwangi
Steve Mwangi

Posted on

A quick refresher on python operators and conditionals.

Operators are the symbols Python uses to work with values, whether that means doing math, comparing two things, or combining conditions.
I will go over the three main categories of operators: arithmetic, comparison, and logical operators.
Then show how comparison and logical operators drive conditional expressions (if, if-else, elif, and nested conditions), which let a program choose what to do based on whether a condition is True or False.

Python Operators

Python operators can be divided into three categories: arithmetic, comparison and logical operators.

Types of Operators

A Python operator can be either a unary or a binary operator.

A unary operator is an operator with only one operand.

A binary operator is an operator with two operands.

Python has left-handed binding i.e. the expression is solved from left to right, except for the exponent operator which uses right-handed binding.


1. Arithmetic Operators

Used to perform basic mathematical calculations.

Operator Name What does it do?
+ Addition Adds values together.
- Subtraction Subtracts the right value from the left.
* Multiplication Multiplies operands together.
/ Division Divides two operands and returns a float (decimal).
// Floor division Divides and rounds down to the nearest whole number.
% Modulus Returns the remainder of a division operation.
** Exponent Raises a number to the power of another.

This can be illustrated in the following python code:

value_1 = 10
value_2 = 20
value_3 = 3

print(value_1 + value_2)        # outputs 30
print(value_1 - value_2)        # outputs -10
print(value_1 * value_2)        # outputs 200
print(value_2 / value_1)        # outputs 2.0
print(value_1 // value_3)       # outputs 3
print(value_1 % value_3)        # outputs 1
print(value_1 ** value_3)       # outputs 1000
Enter fullscreen mode Exit fullscreen mode

2. Comparison Operators

A comparison operator compares two values and gives you back True or False.

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

2.1 Equal To (==) Operator

It is a binary operator that compares two values and returns True if both are equal, else it returns False.

value_1 = 10
value_2 = 20
value_3 = 10

print(value_1 == value_2)  # outputs False
print(value_1 == value_3)  # outputs True
Enter fullscreen mode Exit fullscreen mode

2.2 Not Equal To (!=) Operator

It is a binary operator that compares two values and returns True if they are not equal, else it returns False if they are equal.

Comparison operators can be used to compare strings, in which case you must remember that Python is case sensitive.

str_1 = "cow"
str_2 = "Cow"
str_3 = "cow"

print(str_1 != str_2)   # outputs True
print(str_1 != str_3)   # outputs False
Enter fullscreen mode Exit fullscreen mode

2.3 Greater Than (>) Operator

It is a binary operator that compares two values and returns True if the value on the left is greater than the value on the right, else it returns False.

value_1 = 10
value_2 = 20

print(value_1 > value_2)  # outputs False
print(value_2 > value_1)  # outputs True
Enter fullscreen mode Exit fullscreen mode

2.4 Greater Than or Equal To (>=) Operator

It is a binary operator that compares two values and returns True if the value on the left is greater than or equal to the value on the right, else it returns False.

value_1 = 10
value_2 = 20
value_3 = 10

print(value_1 >= value_2)  # outputs False
print(value_2 >= value_1)  # outputs True
print(value_1 >= value_3)  # outputs True
Enter fullscreen mode Exit fullscreen mode

2.5 Less Than (<) Operator

It is a binary operator that compares two values and returns True if the value on the left is less than the value on the right, else it returns False.

value_1 = 10
value_2 = 20

print(value_1 < value_2)  # outputs True
print(value_2 < value_1)  # outputs False
Enter fullscreen mode Exit fullscreen mode

2.6 Less Than or Equal To (<=) Operator

It is a binary operator that compares two values and returns True if the value on the left is less than or equal to the value on the right, else it returns False.

value_1 = 10
value_2 = 20
value_3 = 10

print(value_1 <= value_2)  # outputs True
print(value_2 <= value_1)  # outputs False
print(value_1 <= value_3)  # outputs True
Enter fullscreen mode Exit fullscreen mode

3. Logical Operators

3.1 And (Conjunction) Operator

It is a binary operator that outputs True if both conditions are met, else it outputs False.

value_1 = 10
value_2 = 20

print(value_1 == 10 and value_2 > 10)   # outputs True
print(value_1 == 10 and value_2 < 10)   # outputs False
Enter fullscreen mode Exit fullscreen mode

3.2 Or (Disjunction) Operator

It is a binary operator that outputs False if both conditions are false, else it outputs True.

If either condition or both conditions are true, it outputs True.

value_1 = 10
value_2 = 20

print(value_1 == 10 or value_2 < 10)   # outputs True
print(value_1 != 10 or value_2 < 10)   # outputs False
Enter fullscreen mode Exit fullscreen mode

3.3 Not (Negation) Operator

It is a unary operator that performs a logical negation.

It outputs False if the input was True and outputs True if the input was False.

value_1 = 10

print(not value_1 == 10)    # outputs False
print(not value_1 != 10)    # outputs True
Enter fullscreen mode Exit fullscreen mode

Python Conditional Expressions

Comparison and logical operators are commonly used in conditional expressions where you need to execute a Python statement depending on the Boolean value of the condition during execution.

4.1 If Expressions

This expression performs the statement only once if the condition is True.

If the condition is True, the statement is executed, else the statement is skipped.

value_1 = 10

if value_1 == 10:
    print("Value 1 is equal to 10")   
  # The print is shown since the condition is met.

if value_1 == 20:
    print("Value 1 is equal to  20")   
 # This statement isn't printed since the conditions is not met.

Enter fullscreen mode Exit fullscreen mode

4.2 If Else Expressions

This expression allows one to check a single condition and have two statements where one statement is performed if the condition is True, otherwise the other statement in the else clause is executed.

value_1 = 10

if value_1 == 10:
    print("Value 1 is equal to 10") 
    # The print statement is shown since the condition is met.
else:
    print("Value 1 is not equal to 10")
    # this statement is not printed since the first condition is met.

if value_1 > 10:
    print("Value 1 is greater than 10") 
    # The print statement is not shown since the condition is not met.
else:
    print("Value 1 is not greater than 10")
    # this statement is printed since the first condition is not met.
Enter fullscreen mode Exit fullscreen mode

4.3 elif Expression

This is an expression where you can set multiple conditions in a sequence with a statement for each condition.

The expression is executed from the top to the bottom.

When the first True condition is encountered, the statement for that condition is executed and the remaining expression is skipped.

An else condition is placed at the bottom of the expression with a fallback statement to ensure that if no condition is met, the fallback statement in the else clause is executed, thus closing the expression.

To illustrate where no condition is met, thus resorting to the else clause, consider the following code:

value_1 = 10

if value_1 < 10:
    print("Value 1 is less than 10") 
    # The print statement is not shown since the condition is not met.
elif value_1 > 10 :
    print("Value 1 is greater than 10")
    # this statement isn't printed since the condition is not met.
elif value_1 == 20:
    print("Value 1 is equal to 20")
    # this statement isn't printed since the condition is not met.
else:
    print("Value 1 is not greater than 10, less than 10 or equal to 20")
    # this statement is printed since all the other conditions are not met.
Enter fullscreen mode Exit fullscreen mode

To illustrate where a condition is met, consider the following example:

value_1 = 10

if value_1 < 10:
    print("Value 1 is less than 10") 
    # The print statement is not shown since the condition is not met.
elif value_1 == 10:
    print("Value 1 is equal to 10")
    # this statement is printed since the condition is met.
elif value_1 > 10 :
    print("Value 1 is greater than 10")
    # this statement is not printed since a condition above is met.
else:
    print("Value 1 is not greater than 10, less than 10 or equal to 20")
    # this statement is not printed since a condition above is met.
Enter fullscreen mode Exit fullscreen mode

4.4 Nested Expressions

Python expressions can also be used within other expressions.

This enables one to have finer control of statements where multiple conditions have to be met before the statement is executed.

Consider the following example.

value_1 = 10
value_2 = 20

if value_1 == 10:
    if value_2 == 20:
        print("Value 1 is equal to 10 and value 2 is equal to 20")
        # this statement is printed since both conditions are met
    else:
        print("Value 1 is equal to 10 and value 2 is not equal to 20")
        # this statement is not prinetd since the condition above is already met.

if value_1 == 10:
    if value_2 > 30:
        print("Value 1 is equal to 10 and value 2 is greater than 30")
        # this statement is not printed since the nested condition is not met.
    else:
        print("Value 1 is equal to 10 and value 2 is not greater than 30")
        # this statement is printed since no condition above has been met.
Enter fullscreen mode Exit fullscreen mode

Conclusion

Operators are the foundation of decision-making in Python. Arithmetic operators handle calculations, comparison operators test how values relate, and logical operators combine those tests into more complex conditions. When paired with if, else, and elif, they let a program respond differently depending on the data it receives. Becoming comfortable with these tools, enables you can move on to loops, functions, and other structures that build on the same logic thus building more complex programs.

Have fun in your python journey!

Top comments (0)