I'm looking at the following piece of code:
totalDistance += \
GetDistance(xCoords[i], yCoords[i],
xCoords[i+1], yCoords[i+1])
and can't understand what += \
means?
To break a line in Python, use the parentheses or explicit backslash(/). Using parentheses, you can write over multiple lines. The preferred way of wrapping long lines is using Python's implied line continuation inside parentheses, brackets, and braces.
The end parameter in the print function is used to add any string. At the end of the output of the print statement in python.
You cannot split a statement into multiple lines in Python by pressing Enter . Instead, use the backslash ( \ ) to indicate that a statement is continued on the next line.
Use the Ctrl - J key sequence instead of the Enter key to get a plain newline plus indentation without having IDLE start interpreting your code.
\
at the end of a line just indicates it will be continued on the next line as otherwise that (totalDist +=
) would raise an error... (also important to note that there can be nothing after the slash ... not even whitespace)
+=
just adds and assigns back
x = 1
x += 1 # x is now 2 (same as x = x + 1)
The \
escapes the line return immediately following it (there should not be any character between the \
and the implicit \n
).
There are also a few other exceptions; new lines are ignored when enclosed in the matching pairs of the following:
[]
()
{}
In other words, the following are equivalent:
a= [1,2,3]
a = [1,
2,
3]
The combination \
followed by newline means line continuation. You can think of the \
as escaping the newline, so that it doesn't have it's usual meaning of "line ending".
In Python, you can often arrange the code so that \
is unnecessary, eg.
totalDistance += GetDistance(
xCoords[i], yCoords[i],
xCoords[i+1], yCoords[i+1])
here, the newlines don't end the line because they are inside the ()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With