Home > Article > Backend Development > Python language usage skills
The following editor will bring you a sharing of python language usage skills. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
一
It’s best to specify the path of python before writing:
# !/usr/bin/python
python needs to add encoding method in linux: to avoid Chinese garbled characters
# -*- coding: UTF-8 –*-
2 Among all kinds of languages, python should be the language that knows how to use indentation the most. Even if its for statement has multiple lines, you don’t need to think about java. It can be used in the same way as C++ and c. {}, like js and swift, can represent a sentence with a newline character instead of using the ; sign. Students with language background, please note:
In python’s view:
If the indentation method is changed, for example, indenting on the second for, it will cause an error:
The error that occurs is as follows:
So here comes the question: What impact do line breaks and indentation have on python? ? ?
It can be seen from the running results:
1 Newline can be regarded as the ; sign in java and c++, not It will affect the structure of the statement.
2 Indentations with the same distance can be regarded as a pair of {}, and {} should appear in pairs in any language.
The reason why this python statement appears in demo1
is equivalent to:
It’s because {} does not match. That’s why the following error occurs
IndentationError: unexpected indent
Or it can be understood this way: statements with the same indentation can be regarded as code blocks{}
The above article The sharing of Python language usage skills is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.
For more articles related to python language usage skills, please pay attention to the PHP Chinese website!