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 common limit throughout the programming world. Here’s what to do when lines get too long or when you want to split it up for clarity.
In order to split up a statement into more than one line, you need to do one of two things:
Note that the line-continuation character is a backslash (\), not the division symbol (/).
Here are examples of both:
| >>> (2 + |
| ... 3) |
| 5 |
| >>> 2 + \ |
| ... 3 |
| 5 |
Notice how we don’t get a SyntaxError. Each triple-dot prompt in our examples indicates that we are in the middle of entering an expression; we use them to make the code line up 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 we’re baking cookies. The authors live in Canada, which uses Celsius, but we own cookbooks that use Fahrenheit. We are wondering how long it will take to preheat our oven. Here are our facts:
The room temperature is 20 degrees Celsius.
Our oven controls use Celsius, and the oven heats up at 20 degrees per minute.
Our cookbook uses Fahrenheit, and it says to preheat the oven to 350 degrees.
We can convert t degrees Fahrenheit to t degrees Celsius like this: (t - 32) * 5 / 9. Let’s use this information to try to solve our 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 variable oven_heating_time spans three lines. The first line ends with an open parenthesis, so we do not need a line-continuation character. The second ends outside the parentheses, so we need the line-continuation character. The third line completes the assignment statement.
That’s still hard to read. Once we’ve continued an expression on the next line, we can indent (by pressing the Tab key or by pressing the spacebar a bunch) to our heart’s content to make it clearer:
| >>> 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 Celsius 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 message to take away here is that well-named temporary variables can make code much clearer.