I have a university assignment to create a noughts&crosses/tictactoe game from a given template and this below was part of it:
board = [['1','2','3'],\
['4','5','6'],\
['7','8','9']]
Whether those backslashes are present or not it doesnt affect the function of the code as the list gives the same printed output and behaves in the same way so I'm just curious as to what their purpose is?
The backslash escapes the next character. Since the next character is a newline, the python parser would work as if the newline wasn't there. But a newline is completely harmless in this context, so its distracting and pointless to do it here.
A different case is string concatentation
Without the backslash this would be a two lines of code - in this case resulting in an indentation error. But since the newline has been escaped, its the same as
and python will concatenate those into a single string during compile.
NOTE
Although there are many escape sequences in string literals, this is the only escape allowed outside of them. Its called a "line continuation character" (or Explicit line joining) because it suppresses normal line termination rules in the python lexer.
Alternately, there is also Implicit line joining:
Since your list definition already follows implicit line joining rules, there is no need to add the explicit rule.