How do I "word wrap" long lines of code?

#For Python3

This question is not related to any practice problems from this site, but a general question. For example, this problem listed below requires me to scroll left to right in order to read it (imagine if it was a longer 2D list!) :

last_semester_gradebook = [[“politics”, 80], [“latin”, 96], [“dance”, 97], [“architecture”, 65]]

Question: Is there a way to cut this example in half (and not run into errors) so I don’t have to keep scrolling left to right due to it being so long?

Thank you,

J

Please see How do I format code in my posts? as it’s much easier to read/use code on the forums that way :slightly_smiling_face:.

If you don’t want to split that into additional variables or otherwise there’s explicit and implicit line joining. Implicit line joining is preferred is basically every style guide you’ll find so use it wherever possible. Here’s an example of implicit line joining used for your code-

last_semester_gradebook = [
    ["politics", 80], ["latin", 96], ["dance", 97], ["architecture", 65],
]
# or the JSON like-
last_semester_gradebook = [
    ["politics", 80],
    ["latin", 96],
    ["dance", 97],
    ["architecture", 65],
]
# you could take that further but I'm certainly not writing it out

Implicit line joining- https://docs.python.org/3/reference/lexical_analysis.html#implicit-line-joining
Explicit line joining- https://docs.python.org/3/reference/lexical_analysis.html#explicit-line-joining

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.