- Learn Programming in Python with Cody Jackson
- Cody Jackson
- 251字
- 2021-06-10 19:06:08
Multiple line spanning
Statements can span more than one line if they are collected within braces (parentheses (), square brackets [], or curly braces {}). Normally parentheses are used. When spanning lines within braces, indentation doesn't matter; the indentation of the initial bracket is used to determine which code section the whole statement belongs to. The following example shows a single variable having to span multiple lines due to the length of the parameters:
tank1 = tank.Tank(
"Tank 1",
level=36.0,
fluid_density=DENSITY,
spec_gravity=SPEC_GRAVITY,
outlet_diam=16,
outlet_slope=0.25
)
tank1 is the variable, and everything to the right of the equal sign is assigned to tank1. While Python allows the developer to write everything within the parentheses on one line, the preceding example has separated each parameter into different lines for clarity.
The Python interpreter recognizes that everything within the parentheses is part of the same object (tank1), so spreading the parameters across multiple lines doesn't cause a problem.
String statements (text) can also be multiline if you use triple quotes. For example, the following screenshot demonstrates a long block of text that is spread over multiple lines:
When the variable containing the text is directly called (line 10 in the preceding screenshot), Python returns the raw text, including the \n symbol which represents a newline character; it tells the system where a new line starts and the old one ends. However, when the print() function is called in line 11, the text is printed as it was originally entered.
- 零基礎學C++程序設計
- Kubernetes實戰
- What's New in TensorFlow 2.0
- SpringMVC+MyBatis快速開發與項目實戰
- Java EE框架整合開發入門到實戰:Spring+Spring MVC+MyBatis(微課版)
- Visual Basic編程:從基礎到實踐(第2版)
- 程序員面試筆試寶典
- 算法基礎:打開程序設計之門
- 看透JavaScript:原理、方法與實踐
- GeoServer Beginner's Guide(Second Edition)
- Hands-On Swift 5 Microservices Development
- 前端HTML+CSS修煉之道(視頻同步+直播)
- Learning SciPy for Numerical and Scientific Computing(Second Edition)
- 機器學習微積分一本通(Python版)
- Kotlin Programming By Example