Python indentation and colon description, python indent colon

Source: Internet
Author: User

Python indentation and colon description, python indent colon

For Python, code indentation is a syntax. Python does not use {} or begin like other languages... end separates code blocks, but uses code indentation and colons to differentiate the layers between codes.

The number of white spaces for indentation is variable, but all code block statements must contain the same number of white spaces for indentation, which must be strictly executed.

For example:

If True: print ("Hello girl! ") # Indent the placeholder else of a tab: # align with if print (" Hello boy! ") # Indent the placeholder of a tab

Python has strict requirements on code indentation. If you do not use proper code indentation, A SyntaxError exception is thrown.

if True:  print("Hello girl!")else:  print("Hello boy!") print("end")

Running the code section throws an exception.

The error indicates that the indentation method you use is inconsistent, some are tab key indentation, some are space indentation, and change to consistent.

Sometimes the Code uses reasonable indentation, but the indentation is different, and the code execution result is also different. Code with the same indentation indicates that the Code belongs to the same code block.

For example:

#-*-Coding: UTF-8-*-if True: print ("Hello girl! ") Else: print (" Hello boy! ") Print (" end ") print (" =========== gorgeous split line ============ ") if True: print ("Hello girl! ") Else: print (" Hello boy! ") Print (" end ")

Running result:

Print ("end") above the split line is not indented and alignment with if. Therefore, it and if belong to the same code block. After the if operation is completed, the output is executed.

Print ("end") and print ("Hello boy! ") Keep the same indentation, then it is consistent with print (" Hello boy! ") Is a code block within else.

A group of statements with the same indentation constitute a code block, which we call a code group.

For a composite statement such as if, while, def, and class, the first line starts with a keyword and ends with a colon (:). One or more lines of code after the row form a code group.

We call the code group at the beginning and later a clause)

Python multi-line statements

In a Python statement, a new line is generally used as the statement Terminator. However, you can use a slash (\) to split a line of statements into multiple lines for display.

num1 = 1num2 = 2num3 = 3total = num1 + \num2 + \num3print("total is : %d"%total)

The statement contains [], {}, or () Brackets. You do not need to use multiline connectors.

days = ['Monday', 'Tuesday', 'Wednesday',    'Thursday', 'Friday']print(days)

A line in Python code can have only one statement, with a new line as the statement Terminator. If a row contains multiple statements, an exception is thrown.

 

This method is like declaring variables in java.

In Python2, it seems that they can be separated by semicolons. I have not installed 2.x on my machine and have not tried it. I cannot test python3.

The solution is as follows:

num1,num2,num3 =1,2,3total = num1 + \num2 + \num3print("total is : %d"%total)

Empty Python lines

Functions or methods of classes are separated by blank lines, indicating the beginning of a new code. The class and function entry are also separated by a blank line to highlight the beginning of the function entry.

Unlike code indentation, empty lines are not part of Python syntax. No blank lines are inserted during writing, and the Python interpreter runs without errors. However, empty lines are used to separate two sections of code with different functions or meanings to facilitate code maintenance or reconstruction in the future.

Remember: Empty lines are part of the program code.

The above Python indentation and colon details are all the content shared by the editor. I hope you can give us a reference and support the help house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.