Python Indentation, Multi-line Statements, and Quotations

Python Indentation, Multi-line Statements, and Quotations

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!")        


Isiaka Idris

????? ??????? ??????? ??????? ?? ????? ? ??????? ???? ??? ??????? ??????.

1 年

Copy! Thank you sir.

回复

要查看或添加评论,请登录

Abdulmutalib Idris的更多文章

  • Improving Nigerian Education Through School Management Systems

    Improving Nigerian Education Through School Management Systems

    The Nigerian education sector faces numerous challenges, particularly at the higher institution level. These include…

  • Python Lambda

    Python Lambda

    Is a function that take as many arguments as possible but can only have one expression. They are also consider…

  • Python Functions

    Python Functions

    A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a…

    2 条评论
  • Python Conditional and Loop Statements

    Python Conditional and Loop Statements

    If ..

    1 条评论
  • Python Datatype - Part 5 (Dictionary)

    Python Datatype - Part 5 (Dictionary)

    As we learnt earlier Dictionary is one of 4 built-in data types in Python used to store collections of data, the other…

    2 条评论
  • Python Datatype - Part 4 (Sets)

    Python Datatype - Part 4 (Sets)

    Python Sets As we learnt earlier Set is one of 4 built-in data types in Python used to store collections of data, the…

    1 条评论
  • Python Datatype - Part 3 (Tuples)

    Python Datatype - Part 3 (Tuples)

    Python Tuples As we learnt in the last class there are datatype use to store multiple items in single variable. Python…

  • Python Datatype - Part 2 (Lists)

    Python Datatype - Part 2 (Lists)

    Python Lists There are datatype use to store multiple items in single variable. Python have 4 built-in datatype that…

    1 条评论
  • Python Datatype - Part 1

    Python Datatype - Part 1

    In the last class we talked about variables and we said variable data type is whatever values are assigned to the…

  • Python Variables

    Python Variables

    In python variables are the reserved memory locations used for storing values. In python a built-in id() function…

社区洞察

其他会员也浏览了