'Zhuan' Python Beginner Tutorial 3 — Operators
Operators This time we are going to learn about operators. Operators are extremely important in programming, and they have the following functions: 1. Calculation 2. Comparison 3. Assignment, and other purposes. For a beginner, seeing this tutorial might be a little intimidating, but don't worry, because these things are very simple. As long as you have gone through high school, you will find that you have already learned a lot of this. If you haven't gone through high school yet, it's okay, because a lot of it is common sense and can be understood at a glance. Let's first look at the precedence of operators: = sign (as mentioned last time, this is an assignment sign, not an equality sign, and its precedence is from right to left, because the variable is on the left and the value is on the right) || sign, left to right, logical operation 'or'. This symbol in a program means "or", for example, a == 1 || b == 1. This code means when a equals 1 or b equals 1, meaning one of the two conditions is met. && sign, left to right, logical operation 'and'. This symbol consists of two & symbols and in a program means "and", for example, a == 1 && b == 1. This code means when a equals 1 and b equals 1, meaning both conditions must be met. | sign, left to right, bitwise operation 'or'. ^ sign, left to right, bitwise operation 'xor', bit exclusive or. & sign, left to right, bitwise operation 'and', bitwise and. ==, !=, <=, >=, several symbols, left to right, comparison operations. == means equal, != means not equal, <= means less than or equal to, >= means greater than or equal to, < means less than, > means greater than. <<, >> two symbols, left to right, shift operations, must be integer or character types. +, - two symbols, left to right, addition and subtraction (usage differs when applied to characters). *, /, % three symbols, left to right, multiplication, division, modulo (cannot be used with strings). Note: The calculation method of addition, subtraction, multiplication, and division is the same as in mathematics: multiply and divide first, then add and subtract. -, !, ++, --, ~, *, [], (), & left to right: - (negative number), ! (negation), ++ (increment), -- (decrement), ~ (bitwise NOT, bitwise complement), [] (array), () (function parameter passing), & (address-of operation) (only !(Not) can be used for strings) When the precedence is the same, the order of operations is determined by associativity. Note 1: a == 1 || b == 2 && c == 3, the meaning of this statement is: when (a equals 1 or b equals 2) and c equals 3, calculate the 'or' on the left first, then the 'and' on the right. Note 2: Mixed operations cannot use these symbols (such as +=, -=, *=, etc.). Note 3: ',' and '?:' are conditional operations, which the program does not support. I'm not sure if everyone still remembers the previous tutorial; if you don't, quickly go review it. Now let's do homework. Last time we learned to write an addition operation using variables; now we will do addition, subtraction, multiplication, and division. You can write them separately or together. I noticed I have assigned homework twice and no one has submitted it yet. If it's not submitted, I won't teach in the future! I hope friends will reply to the post because replying is a virtue!
Replies (0)
— All replies loaded —