Foundations of Python

You are currently auditing this course.
69 / 134

Boolean Expressions

A boolean expression is either true or false. The following examples use the operator ==, which compares two operands and produces True if they are equal and False otherwise:

1 == 1

It returns True.

2 == 1

It returns False.

True and False are special values that belong to the class bool,

print(type(True))

It returns <class 'bool'>

The == operator is one of the comparison operators; the others are:

x != y               # x is not equal to y
x > y                # x is greater than y
x < y                # x is less than y
x >= y               # x is greater than or equal to y
x <= y               # x is less than or equal to y
x is y               # x is the same as y
x is not y           # x is not the same as y
  • Avoid the mistake of using a single equal sign (=) instead of a double equal sign (==).

  • = is an assignment operator and == is a comparison operator.

  • There is no such thing as =< or =>.

INSTRUCTIONS

Please define a function with the name is_on_line which would take three arguments say x1, x2 and x. This function should return True if x is on the one dimensional line joining x1 and x2 like the following diagram:

x1-----x-----x2

If x is outside the line joining x1 and x2, it should return False for example:

x1-----x2-----x

Please note that if x is equal to x1 or x2, it is considered at line. Also, the x1, x2 and x can be negative numbers.

Test Cases:

Input: is_on_line(10, 20, 30)

Expected Output: False


Input: is_on_line(10, 20, 20)

Expected Output: True


Input: is_on_line(10, 20, -10)

Expected Output: False


Input: is_on_line(-4, -5, -4.5)

Expected Output: True


Please note that the previous question in which you had to write bool_func has been removed. So most of the comments below are obsolete now.


Loading comments...