Section 5 Operators and Expressions
Hello everyone, today we are learning: operators and expressions.
The so-called calculations can be simply understood as addition, subtraction, multiplication, and division. Of course, python operations are much more than that. Because python's objects are not only numbers, but also strings, and many more... But even so, it does not prevent us from using mathematically-like "simple operations" concepts to understand its operating rules. The expression of all operating rules is presented by operators.
** One, operator**
Operators are symbols that connect python objects and make them operate according to certain operation rules. Next we will briefly understand the various operators and their usage.
** 1、 computation**
PS: A more common operation is to perform a mathematical operation on a variable and return the result of the operation to the variable.
a = 2
a = a * 3
** 2、 Comparison operation**
PS: Please note that the results of comparison operations are all boolean: true/false.
** 3、 Operation priority**
Just like mathematical operations, python operations also have priority. The complete "Python Operation Priority List" from the highest level to the lowest level in Python will be given below for reference.
** 4、 Change the order of operations**
Operators with the same precedence will be evaluated sequentially from left to right.
For example: 2 + 3 + 4 will be calculated in the form of (2 + 3) + 4.
To make the expression easier to read, we can use parentheses.
For example: 2 + (3 * 4) is easier to understand than 2 + 3 * 4,
Because the latter also requires you to understand the precedence of operators.
There is an additional advantage of using parentheses: ** It can help us change the order of operations. **
For example: 2*(3+4)
PS: Please use brackets in moderation. Redundant brackets like this (2 + (3 * 4)) seem stupid!
** 2. Regarding expressions**
In a narrow sense, it is an operation expression, that is, a calculation formula. Today, we expand its extension, we say that the expression form of python code statement is expression, in short, it is the way of program expression.
"Google Open Source Project Style Guide" Python style specification
https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/
Here are some important and noteworthy issues in Python expressions:
** 1、 Notes**
Any text on the right side of the # sign is mainly used to describe the remarks of the program code. In the code editor, it is usually displayed in green. Comments are text that will not be executed.
E.g:
print('hello world') #Note that print is a function
or:
print('hello world')
PS: The code will tell you how to do it, and the comments will tell you why.
You should use as many useful comments as possible in your program:
PS: This is really important. You will soon find that the person you need most is yourself.
** 2、 semicolon**
Students with experience in other languages may be accustomed to adding a semicolon at the end of the code, but there is no such requirement in the grammar rules of python. However, because Python allows multiple statements to be written on the same line, and you need to add a semicolon as a separator between each statement, so if you are used to adding a semicolon at the end of the code, python will not report an error.
However, I strongly recommend you not to do this, because that will make you look like a novice python or "former C/C++ programmer..." rather than a professional python programmer. In fact, we have never used or even seen a semicolon in a Python program.
** 3、 indentation**
The expression of code indentation should be familiar to programmers with language experience. It is to leave a few spaces or tabs at the beginning of the code, which is called indentation by python.
** Indentation is extremely important in Python's writing specifications, and it is not too much to be taken seriously, because it is actually part of the Python syntax. **
Leave a space at the beginning of the code line to determine the logical relationship between different code lines, which is also a way of grouping statements. This means that code statements at the same logical level must have the same indentation. Each group of such statements is called a block. In practical applications, you will understand how important the concept of blocks is.
One thing you need to remember: **Wrong indentation can cause errors. **
How to indent
Use four spaces for indentation. This is an official suggestion from the Python language. A good editor will automatically do this for you. Please make sure you use a consistent number of spaces in the indentation, otherwise your program will not run properly or cause unexpected errors.
PS: Python will always use indentation for blocks, and will never use braces.
** summary**
In this section, we introduce operators and expressions, which are the basic blocks for building any program. Next, we will see how to use them in the program.
Recommended Posts