Python Indentation, Multi-line Statements, and Quotations
Abdulmutalib Idris
Head of IT and Media / Full Stack Developer @ Agro Preciso LTD
Python Indentation
The spaces left at the beginning of a code line is called indentation. In other languages indenting is a matter of choice as it is for readability. Python indentation indicates blocks of code (as python provides no braces to indicate blocks of code) and is very important to avoid syntax errors.
For example:
if 4 > 1:
print("Four is greater than one!")
If the indentation is ignored like below a syntax error will be thrown by the compiler
if 4 > 1:
print("Four is greater than one!")
The number of spaces in the indentation is variable (has to be at least one), but all statements within the block must be indented the same amount.
The below will run fine:
if 5 > 2:
print("Five is greater than two!")?
if 5 > 2:
print("Five is greater than two!")?
But the following will result in syntax error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Python Multi-Line Statements
New line indicate end of statement in python but if you want a statement to continue in a new line, then the line continuation character (\) can be use to denote the line continues in the new line
For example:
item_one = 1
item_two = 3
item_three = 4
total = item_one + \
item_two + \
item_three
print (total)
Statements contained within the [ ], { }, or ( ) brackets do not need to use the line continuation character.
For example:
daysofweek = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday', 'Sunday']
print (daysofweek)
领英推荐
Quotations in Python
', ", ''' or """ are all accepted quotes to denote string in python, as long as the same type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines. For example, all the following are allowed:
word = 'word'
print (word)
sentence = "This is a sentence."
print (sentence)
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
print (paragraph)
Python Comments
Comments can be used to explain Python code. Comments can be used to make the code more readable. Comments can be used to prevent execution when testing code.
Single Line Comments: starts with a #, and Python will ignore the line:
#This is a comment
print("Hello, World!")
Comments can be placed at the end of a line, and Python will ignore the rest of the line:
print("Hello, World!") #This is a comment
# It can also be used to prevent Python from executing a line of code:
#print("Hello, World!")
print("Cheers, Mate!")
Multiline Comments
Python have no syntax for multiline comment so one can use one of two (2) options; insert # for each line or since Python will ignore string that are not assigned to a variable, you can add a multiline string (""") to hold your comment
#This is a comment
#written in
#more than just one line
print("Hello, World!")
or
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
????? ??????? ??????? ??????? ?? ????? ? ??????? ???? ??? ??????? ??????.
1 年Copy! Thank you sir.