All makes sense except the use of \, I have not seen this before. Does this just mean that the line break is ignored? So this codeblock will be processed as if it is all one line? (so more for readability?)
Hey. \ just indicates that your code will be continued on the next line. For example, take this piece of code.
distance = 2 + \
3
print(distance) #This will print out 5
On the other hand, if I were to do the same but leave out the \, I would get an error saying âinvalid syntaxâ. Hope this makes some sort of sense. Hereâs another page that explains it in more detail in case you want some more examples. Iâm not very advanced in python so Iâm not able to directly help you with that specific piece of code, but I thought Iâd share what I know. https://stackoverflow.com/questions/12612065/what-does-placing-at-the-end-of-a-line-do-in-python/12612168
Youâll notice that the above code is a conditional expression in a lambda expression. Iâm not sure the conditional expression is even taught in the basic Python course or anywhere expressly that I can recall. Itâs not uncommon for authors to throw in code patterns that havenât been taught. Better course auditing would bring this to bear, but that is not our purview over here.
Consider,
def cmp(a, b):
return 1 if a > b else -1 if b > a else 0
This is the Python near equivalent to a JS ternary expressionâŚ
return a > b ? 1 : b > a ? -1 : 0;
print (cmp(6, 5), cmp(5, 6), cmp(6, 6)) # 1 -1 0
A lambda is an anonymous function expression nearly equivalent to a defined function except it can be returned (or assigned) in its written form. If this is new then a quick segue to the documentation and/or some articles on the subject would be well worthwhile.
The advanced courses can sometimes take it for granted that a learner has more background that they might actually have. Whenever coming across code patterns you donât recognize, bring them up here and give attribution to the lesson where you found it. That way you donât get in too deep before grinding to a halt.