Finding Quadrant from x and y values – Python

Hello, programmer! Today, in this tutorial, we will do some basic coding in Python to find which quadrant of the s a specific point belongs to in space.

In 2D space, there are a total of four coordinates.

The condition for each quadrant, in this case, is as follows:

Quadrant 1: negative x and a positive y.
Quadrant 2:  x and y are both positive.
Quadrant 3:  x and y are both negative.
Quadrant 4: positive x and a negative y.

Finding Quadrant from x and y values – Python

Method #1: Using If-Else Conditional Statements (Static Input)

Approach:

  • Give the x-coordinate as static input and store it in a variable.
  • Give the y-coordinate as static input and store it in another variable.
  • Check if the given x-coordinate is less than 0 and y-coordinate is less than 0 using the if conditional statement.
  • If it is true, then print “The point is in 3rd Quadrant”.
  • Check if the given x-coordinate is less than 0 and y-coordinate is greater than 0 using the if conditional statement.
  • If it is true, then print “The point is in 1st Quadrant”.
  • Check if the given x-coordinate is greater than 0 and y-coordinate is greater than 0 using the if conditional statement.
  • If it is true, then print “The point is in 2nd Quadrant”.
  • Else print “The point is in 4th Quadrant”.
  • The Exit of the Program.

Below is the implementation:

# Give the x-coordinate as static input and store it in a variable.
x_coordinate = 6
# Give the y-coordinate as static input and store it in another variable.
y_coordinate = -2
# Check if the given x-coordinate is less than 0 and y-coordinate is less than 0
# using the if conditional statement.
if(x_coordinate < 0 and y_coordinate < 0):
    # If it is true, then print "The point is in 3rd Quadrant".
    print("The point is in 3rd Quadrant")
# Check if the given x-coordinate is less than 0 and y-coordinate is greater
# than 0 using the if conditional statement.
elif(x_coordinate < 0 and y_coordinate > 0):
    # If it is true, then print "The point is in 1st Quadrant".
    print("The point is in 1st Quadrant")
# Check if the given x-coordinate is greater than 0 and y-coordinate is greater
# than 0 using the if conditional statement.
elif(x_coordinate > 0 and y_coordinate > 0):
    # If it is true, then print "The point is in 2nd Quadrant".
    print("The point is in 2nd Quadrant")
# Else print "The point is in 4th Quadrant".
else:
    print("The point is in 4th Quadrant")

Output:

The point is in 4th Quadrant

Method #2: Using If-Else Conditional Statements (User Input)

Approach:

  • Give the x-coordinate as user input using the int(input()) function and store it in a variable.
  • Give the y-coordinate as user input using the int(input()) function and store it in another variable.
  • Check if the given x-coordinate is less than 0 and y-coordinate is less than 0 using the if conditional statement.
  • If it is true, then print “The point is in 3rd Quadrant”.
  • Check if the given x-coordinate is less than 0 and y-coordinate is greater than 0 using the if conditional statement.
  • If it is true, then print “The point is in 1st Quadrant”.
  • Check if the given x-coordinate is greater than 0 and y-coordinate is greater than 0 using the if conditional statement.
  • If it is true, then print “The point is in 2nd Quadrant”.
  • Else print “The point is in 4th Quadrant”.
  • The Exit of the Program.

Below is the implementation:

# Give the x-coordinate as user input using the int(input()) function and store it in a variable.
x_coordinate = int(input("Enter x coordinate = "))
# Give the y-coordinate as user input using the int(input()) function and store it in another variable.
y_coordinate = int(input("Enter x coordinate = "))
# Check if the given x-coordinate is less than 0 and y-coordinate is less than 0
# using the if conditional statement.
if(x_coordinate < 0 and y_coordinate < 0):
    # If it is true, then print "The point is in 3rd Quadrant".
    print("The point is in 3rd Quadrant")
# Check if the given x-coordinate is less than 0 and y-coordinate is greater
# than 0 using the if conditional statement.
elif(x_coordinate < 0 and y_coordinate > 0):
    # If it is true, then print "The point is in 1st Quadrant".
    print("The point is in 1st Quadrant")
# Check if the given x-coordinate is greater than 0 and y-coordinate is greater
# than 0 using the if conditional statement.
elif(x_coordinate > 0 and y_coordinate > 0):
    # If it is true, then print "The point is in 2nd Quadrant".
    print("The point is in 2nd Quadrant")
# Else print "The point is in 4th Quadrant".
else:
    print("The point is in 4th Quadrant")

Output:

Enter x coordinate = -4
Enter x coordinate = -2
The point is in 3rd Quadrant