Points: Basic Programming Concepts

Here we'll write programs to work with two points, starting by finding the slope of the line between the two points.

Slope

(The Math): Calculating Slope Between Two Points

How to find the slope of a line between two points.

Show Notes ▼

(Program): Finding the Slope Between two Points Using Simple Variables

A simple program to find the slope.

Show Notes ▼

(Program): Tuples

Tuples are a simple way of grouping variables. Since each point has both x and y coordinates it makes sense to group them together. So, we can rewrite the program like so:

Show Notes ▼

(Program): Classes

Another, much more powerful, way of grouping things is a class. A point on the coordinate plane, with its x and y values would make a good class.

Show Notes ▼

(Program): Writing Functions

Functions are used to separate out pieces of code that do specific calculations. They are expecially useful for operations that you have to do repeatedly.

Show Notes ▼

Exercises

1) Write a program to find the distance between two points. The distance (d) formula is: $$ d = \sqrt{\Delta x^2 + \Delta y^2} $$
Show Notes ▼
Show Solution ▼
2) Write a program using tuples to find the distance between the two points.
3) Write a program using the points class to find the distance between two points.
4) Write a function that calculates the distance between two points.
5) Write a function that calculates the distance between two points using the points class to pass the coordinates of the points.