Sometimes statements get pretty intricate. The recommended Python style is to limit lines to 80 characters, including spaces, tabs, and other whitespace characters, and that’s a suggested limit throughout the programming world. Here’s what to do when lines get too long or when you want to split them up for clarity.
To split up a statement into more than one line, do one of two things:
Note that the line-continuation character is a backslash (\), not the division symbol (a forward slash, /). There must be no other characters on the line after the backslash, not even whitespace characters.
Here are examples of both:
| | >>> (2 + |
| | ... 3) |
| | 5 |
| | >>> 2 + \ |
| | ... 3 |
| | 5 |
Notice how you no longer get a SyntaxError. Each triple-dot prompt in the examples indicates that you are in the middle of entering an expression; we use them to align the code lines nicely. You do not type the dots any more than you type the greater-than signs in the usual >>> prompt, and if you are using IDLE, you won’t see them at all.
Here is a more realistic (and tastier) example: let’s say you’re baking cookies. The authors live in countries that use the Celsius scale, but you own cookbooks that use the Fahrenheit scale. You are wondering how long it will take to preheat your oven. Here are your facts:
The room temperature is 20 degrees Celsius.
Your oven controls use the Celsius scale, and the oven heats up at 20 degrees per minute.
Your cookbook uses the Fahrenheit scale, and it says to preheat the oven to 350 degrees.
You can convert tf degrees Fahrenheit to degrees Celsius as follows: (tf - 32) * 5 / 9. Let’s use this information to try to solve your problem.
| | >>> room_temperature_c = 20 |
| | >>> cooking_temperature_f = 350 |
| | >>> oven_heating_rate_c = 20 |
| | >>> oven_heating_time = ( |
| | ... ((cooking_temperature_f - 32) * 5 / 9) - room_temperature_c) / \ |
| | ... oven_heating_rate_c |
| | >>> oven_heating_time |
| | 7.833333333333333 |
Not bad—just under eight minutes to preheat.
The assignment statement to the variable oven_heating_time spans three lines. The first line ends with an open parenthesis, so you do not need a line-continuation character. The second ends outside the parentheses, so you need the line-continuation character. The third line completes the assignment statement.
That’s still hard to read. Once you’ve continued an expression on the next line, you can indent (by pressing the spacebar multiple times or the Tab key) to your heart’s content to make it more straightforward:
| | >>> oven_heating_time = ( |
| | ... ((cooking_temperature_f - 32) * 5 / 9) - room_temperature_c) / \ |
| | ... oven_heating_rate_c |
Or even this—notice how the two subexpressions involved in the subtraction line up:
| | >>> oven_heating_time = ( |
| | ... ((cooking_temperature_f - 32) * 5 / 9) - |
| | ... room_temperature_c) / \ |
| | ... oven_heating_rate_c |
In the previous example, we clarified the expression by working with indentation. However, we could have made this process even clearer by converting the cooking temperature to the Celsius scale before calculating the heating time:
| | >>> room_temperature_c = 20 |
| | >>> cooking_temperature_f = 350 |
| | >>> cooking_temperature_c = (cooking_temperature_f - 32) * 5 / 9 |
| | >>> oven_heating_rate_c = 20 |
| | >>> oven_heating_time = (cooking_temperature_c - room_temperature_c) / \ |
| | ... oven_heating_rate_c |
| | >>> oven_heating_time |
| | 7.833333333333333 |
The key takeaway here is that well-named temporary variables can significantly improve code clarity.