In programming, conditions are utilized heavily. If you never heard of a condition, think of all the times your “crush” lists all his/her parameters of what they seek in a partner (must be x-height, x-weight, x-hair color, x-fit, etc.) and you must see whether or not you fit their parameters to move forward or fall back into the friendzone of no return. Python utilizes boolean logic to evaluate conditions. Boolean values (true and false) are returned when an expression is evaluated.
Example:
x = 1
print(x == 5) # false
print(x == 1) # true
print(x != 0) # true
print(x < 4) # true
print(x > 3) # false
One thing that you may have caught by looking at the code is that you will notice that a variable assignment is done with a single “=” operator while the comparison is done with two.
Here is a chart for the Python comparison operators:
Operator | Description | Example |
---|---|---|
== | If the values of two operands are equal, then the condition becomes true. | (x == y) is not true |
!= | If the values of the two operands are not equal, then the condition becomes true. | (x != y) is true |
> | If the value of the left operand is greater than the value of the right operand, then the condition becomes true. | (x > y) is not true |
< | If the value of the left operand is less than the value of the right operand, then the condition becomes true. | (x < y) is true |
>= | If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes true. | (x >= y) is not true |
<= | If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes true. | (x <= y) is true |
<> | If the values of the two operands are not equal, then the condition becomes true. | (x <> y) is true |
Boolean Operators
For developing complex Boolean expressions, we can utilize the “and” and “or” Boolean operators.
name = "Karen"
age = 45
if name == "Karen" and age == 45:
print("Your name is Karen, and you are also 45 years old and still requesting for managers at every establishment you go to.")
if name == "Karen" or name == "Sharon":
print("Your name is either Karen or Sharon. You are probably going to ask for the manager, regardless of what I say...")
The “in” Operator
The “in” operator is a neat operator that can be utilized to check if a specific object exists within an object container, like a list.
x = "Bob"
if name in ["Chuck", "Arnold", "Bob"]:
print("Your name could be either Chuck, Arnold, or Bob.)
If you have watched the hit TV series, Silicon Valley, you may be aware of the whole debate between tabs and spaces. If you have not, check out this bit from the show.
Python utilizes indentation to define a code block rather than using brackets. By it standards, Python indentation is 4 spaces. However, tabs (and any other space size) will be sufficient. The catch is that you must be consistent. Personally, I am like the Pied Piper founder: I prefer tabs.
Example:
x = False
y = True
if x is True:
# do something
pass
elif y is True: # else if
# do something else
pass
else:
# do another thing
pass
Here is a better example:
x = 1
if x == 1:
print("x equals 1!") # should return true
else:
print("x does not equal 1!"
For a statement to be deemed as true, it must consist of one of the following as valid:
- The “True” boolean variable is given, or calculated using an expression, such as an arithmetic comparison.
- An object which is not considered “empty” is passed.
For it to be deemed empty:
- The string is empty: “”
- The list is empty: []
- The literal number zero: 0
- The false boolean variable: False
The “is” Operator
The “is” operator actually quite interesting. You can compare it to the “==” operator, but it is not the same. Unlike the “==” operator, the “is” operator does not match the values of the given variables but rather instance themselves.
Example:
x = [1,2,3,4,5]
y = [1,2,3,4,5]
print(x == y) # True
print(x is y) # False
The “not” Operator
The last operator we will go over is the “not” operator. Essentially, it reverses the result, and then returns false if the outcome is true.
print(not False) # true
print((not False) == (False)) # false
For the next few tables, take a look at some of the operators:
Python Logical Operators:
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true | x < 5 and x < 10 |
or | Returns True if one of the statements is true | x < 5 or x < 4 |
not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
Python Identity Operators:
Operator | Description | Example |
---|---|---|
is | Returns True if both variables are the same object | x is y |
is not | Returns True if both variables are not the same object | x is not y |
Python Membership Operators:
Operator | Description | Example |
---|---|---|
in | Returns True if a sequence with the specified value is present in the object | x in y |
not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
Python Bitwise Operators:
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
Exercise
For this exercise, it is going to be rather straightforward. Change the variables within the first section in order to make each “if-statement” come out as true. Please take a look at the solution if you get stuck!
Exercise:
# change this code
first_num = 10
second_num = 10
array_one = []
array_two = [1,2,3]
if first_num > 15:
print("1")
if array_one:
print("2")
if len(array_two) == 2:
print("3")
if len(array_one) + len(array_two) == 5:
print("4")
if array_one and array_two[0] == 1:
print("5")
if not second_num:
print("6")
Solution:
# alter this fragment
first_num = 16
second_num = 0
array_one = [1,2,3]
array_two = [1,2]
if first_num > 15:
print("1")
if array_one:
print("2")
if len(array_two) == 2:
print("3")
if len(array_one) + len(array_two) == 5:
print("4")
if array_one and array_two[0] == 1:
print("5")
if not second_num:
print("6")
Output:
<script.py> output:
1
2
3
4
5
6
For more information
As always, if you ever do get stuck and require more resources, please check out Python’s official documentation.
Michael is an Information Technology consultant, with a focus on cybersecurity. Every day, Michael strives to learn something new, with an aim to share it with you all!