We will discuss about:
Multi-line statements
Statements in Python usually are single line and there is no end character for the statement.
When a statement needs to continue, or we desire to write it on many lines to make it more readable we can use “\” character
Example:
polygon_perimeter = side1 + \
side2 +\
side3 +\
side4 +\
side5
When statements are inside brackets i.e. (), [], {} need to spread multiple lines, there is no need for / character
Brackets are used in special for data structures, [] is used lists, () for tuple, and {} for dictionary
Hence, we can have:
list example on multi- line:
list1 = ['elem1', 'elem2', 'elem3', 'elem4',
'elem5', 'elem6', 'elem7', 'elem8']
tuple example on multi-line:
tuple1 = ('tup1', 'tup2', 'tup3', 'tup4',
'tup5', 'tup6', 'tup7', 'tup8')
dictionary example on multi-line:
dict1 = {
"category": "Computers",
"type": "boolean",
"difficulty": "medium",
"question": "The HTML5 standard was published in 2014.",
"correct_answer": "True",
"incorrect_answers": "False"
}
Code block or suite
In below example:
price=12
if price >= 10:
discount=0.2
print("discount is higher")
else:
discount=0.15
print("discount is smaller")
print(f"My discount is {discount}")
Two statements that follow after if price>=10: represent a code block or sometime named suite. Here suite contains 2 statements, in general it can contain more lines, those lines are indented in the same way.
For if referring to suite we have general syntax like below:
if condition1:
suite
elif condition2:
suite
else:
suite
In this syntax statements if condition1: , elif condition2: and else: are named header. A header line begins with a keyword, contain others like conditions, expressions and end with colon ":"
Structure with header lines and suite is encountered for other complex statements like while, def, class etc