For this Project: https://www.codecademy.com/courses/learn-python-3/informationals/python3-reggies-linear-regression
On second prompt, it asks you to create a function to calculate the difference between a point and the linear function. Can someone explain the syntax/logic for the point input from the function and how within the function point is then designated as x_point, y_point?
def calculate_error(m, b, point):
x_point, y_point = point
y = m*x_point + b
distance = abs(y - y_point)
Are you asking what type it is?
Given you’ve got an
x_point and a
y_point, I am presuming you’re working with 2-dimensional Cartesian co-ordinates.
point will be an
(x,y) co-ordinate one assumes.
By passing in a tuple, when we do
x_point, y_point = point the tuple is expanded out so that the first item of the tuple goes to the first declared variable, and the second item to the second variable. Like so:
>>> point = (2,4)
>>> x_point, y_point = point
Edit: I’m not a Pro user, and have no access to that course material, so this is a bit of a guess.
Not being familiar with what data type ‘point’ might be, I guessed that it was a list or tuple. So I accessed the values like you would access a list:
x_point = point
y_point = point
It’s not as elegant as the one line of code solution, but I thought it might help you visualize what is going on better anyway.