Python Basic Knowledge

Python is a computer programming language. Computer programming language and our daily use of natural language is different, the biggest difference is that the natural language in different contexts have a different understanding, and the computer according to the programming language to perform the task.

We must ensure that the programming language to write the program Must not be ambiguous. So any programming language has its own set of syntax, the compiler or interpreter is responsible for the grammar of the program code into the CPU can execute the machine code, and then execute. Python is no exception.

Python syntax is relatively simple, using indentation, write the code looks like the following:

# print absolute value of an integer:
a = 100
if a >= 0:
    print(a)
else:
    print(-a)

The statements beginning with # are annotations, annotations are visible, can be arbitrary, and the interpreter ignores comments. Each other line is a statement. When the statement to the colon: the end of the indentation of the statement as a code block.

Indulgence has advantages and disadvantages. The advantage is that you are forced to write the formatted code, but does not specify whether the indentation is a few spaces or a tab. In accordance with the custom management, should always adhere to the use of four spaces indent.

Another advantage of indentation is to force you to write less indented code, and you will tend to split a long code into several functions to get the less indented code.

Indentation is the “copy – paste” function is invalid, this is the most pit father place. When you refactor the code, pasting past code must re-check whether the indentation is correct. In addition, the IDE is hard to format Python code like formatting Java code.

Finally, make sure that the Python program is case-sensitive, and if the case is wrong, the program will report an error.

summary

Python uses indentation to organize code blocks, so be sure to follow the conventions and stick to the indentation of four spaces.

In the text editor, you need to set the Tab automatically converted to four spaces, to ensure that no Tab and space.