Greater Than and Less Than Operators in Python
To compare two values, the following operators are used: greater than >, greater than or equal to >=, less than <, less than or equal to <=.
Let us have a variable tst:
tst = 3
Let's find out if the value of this variable is greater than zero:
if tst > 0:
print('+++') # this will work
else:
print('---')
Now let's change the value of the variable to negative:
tst = -3
if tst > 0:
print('+++')
else:
print('---') # will this work
Now let the value of the variable be 0. In this case, the block else will be executed:
tst = 0
if tst > 0:
print('+++')
else:
print('---') # will this work
Let's change the condition to greater than or equal to:
if tst >= 0:
print('+++') # this will work
else:
print('---')
If you change the condition to less than or equal, the first block will also work:
if tst <= 0:
print('+++') # this will work
else:
print('---')
Given a variable:
tst = 5
Check that it is greater than 10.
Check that the variable tst is less than 10.
Check that the variable tst is greater than or equal to 5.
Check that the variable tst is less than or equal to -5.
Given a variable:
tst = 5
Check that it is greater than 10.
The following code is given:
if age >= 18:
print('You have access to the site')
else:
print('But you do not have access to the site')
Set the variable age to a number with your age. Tell me what is printed to the console.