0

basically I missed school during a year for personal reasons, so now we're doing python and I have little to no idea of what's going on. I did understand the basics of functions, but now I need to solve this code: The second part of the code is what I don't understand.

def determinant(x1,y1,x2,y2)
return x1*y2-y1*x2

def alignement(xA,yA,xB,yB,xC,yC):
    x1,y1=xB-xA,yB-yA
    x2,y2=xC-xA,yC-yA
    if determinant(x1,y1,x2,y2)==0:
        return True
    else:
        return False

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
whateer
  • 9
  • 1
  • By the way; I see that you're new, if one of our answers helps you out; mark it as correct so people know when they're looking around :) – damaredayo Mar 13 '20 at 13:56
  • Take a look at this: https://treyhunner.com/2018/03/tuple-unpacking-improves-python-code-readability/ – jjramsey Mar 13 '20 at 14:04

3 Answers3

1

This coma:

x1, y1=xB - xA, yB - yA

is the same as

x1 = xB - Xa
y1 = yB - yA

The comas at the function declaration:

def determinant(x1, y1, x2, y2):
    pass  # or

def alignement(xA, yA, xB, yB, xC, yC):
    pass

are just for splitting the variables, in some programming languages you do not need parentheses or comas, for instance if F# can be:

let determinant x1 y1 x2 y2 = // something
let sum a b = a + b
and you can call it:
sum 2 3 // will result in 5
Sachihiro
  • 1,597
  • 2
  • 20
  • 46
0

The comma in python simply indicates a new parameter or value being passed to a function, list or dictionary. An example with functions being:

def foo(param1, param2):
    print(param1, param2)

variable1 = "there"

foo("hello", variable1)

Then with lists:

list1 = ["hello", 1, "there", 2]

Then with dictionaries:

dict1 = {
    "hello" : "there",
    "foo" : 3
}

In your example, it would help to add some spacing (which I will do, and also explain)

I will go over just one line which should explain the rest.

x1, y1 = xB-xA, yB-yA

So here; x1 will be equal to xB-xA, and y1 will be equal to yB-yA because of how the commas are structured. The sides match, and this can scale up.

damaredayo
  • 1,048
  • 6
  • 19
0

I'm assuming your question is about these lines:

x1, y1 = xB-xA, yB-yA
x2, y2 = xC-xA, yC-yA

The comma operator in Python creates a tuple. In this case, it is used to perform multiple assignment. The lines above are equivalent to writing:

x1 = xB-xA
y1 = yB-yA
x2 = xC-xA
y2 = yC-yA

It uses a mechanism called tuple unpacking, you can read about it here.