Checking for Inequality in Python
There is also an operator !=
which tests for inequality.
Let there be a variable:
tst = 5
Let's check that it is not equal to 0
:
if tst != 0:
print('+++') # this will work because the variable is NOT equal to 0
else:
print('---')
Now let's change the value of the variable:
tst = 0
if tst != 0:
print('+++')
else:
print('---') # this will work because the variable is equal to 0
Given a variable:
tst = -4
Check that it is not equal to 4
.
The following code is given:
tst = 123
if tst != 123:
print('+++')
else:
print('---')
Tell me what will be output to the console.
The following code is given:
tst = ['a', 'b', 'c']
if tst[1] != 'c':
print('+++')
else:
print('---')
Tell me what will be output to the console.
The following code is given:
tst1 = [3, 4, 5, 6]
tst2 = list('3456')
if tst1 != tst2:
print('+++')
else:
print('---')
Tell me what will be output to the console.