Having mastered the basic syntax, as well as the basic types, are you a little eager now to write a simple program? After learning the fundamental operations of Python in this class, we will be able to write small simple Python programs and to solve simple problems in Python. The fundamental operations in Python include arithmetic operation, comparison operation, logical operation, character operation and bitwise operation. Of them, arithmetic operations are the most fundamental. Let's look at the arithmetic operators provided in Python. Let's start with the simplest one. Positive and negative signs are the same as those in mathematics. They also include the plus sign, the minus sign, as well as the times sign and the division sign. The times sign is represented as an asterisk (*), while the traditional division is represented through a slash (/). Besides, there is power operation, also known as exponentiation operation, represented by two asterisks (**). Another is to find the remainder, or get the reminder using the percent sign (%). Besides, there is another special division, called "integer division". expressed through double slashes (//). Let's look at some simple ones. For example, there are two variables "pi" and "r". If we want to compute the circumference of a circle, in use, it would be something like 2 times pi times r, and then assign this expression to a variable, in the end, output this variable. Besides, signs like plus or minus are the same as their daily uses. Let's try them in several examples. Alright, several examples. 3 + 4 = 7, 3 * 4 = 12, 3 ** 2 = 9, 8 % 5 = 3, 1 / 2 = 0.5, 1 // 2 = 0, Then let's think, how to express -3 to the power of 2? Is it -3 ** 2 ? Definitely no. You guys may have guessed it, it's because the priority level of power operator is higher than that of negative. What shall we do? Obviously, we can add parentheses to change the priority level of operator. Now, we've got the correct result. In computing, we should consider the priority levels of various operators. For example, the priority of multiplication/division operator is higher than that of addition/subtraction operator. If the priority levels of the operators on both sides of operand are the same, what else shall we consider? We also need to consider the direction of combination from the left to the right or from the right to the left. Only in this way can we determine which formula is to be computed earlier and which one later. Arithmetic operations are very fundamental operations. In programs, if some objects need to be compared, we need comparison operation. In Python, the operators for comparison include the following ones, strictly less than, strictly greater than, less than or equal, greater than or equal, equal, and not equal. In comparison, if numerical values are compared, we compare them according to the values. For example, 4 is greater than 3, and the result is True. 2 equals 2. As for comparison of strings, the ASCII code values corresponding to the strings will be compared. Like comparison of these strings, the first character "a" is less than "x", and the result as we see is False. If you have learned any other programming language before, you must have noticed such an expression: "3 < 4 < 7", which is similar to that in math. In other programming languages, like C, the result would always be True. Why? Like in the C language, the result of 3 less than 4, is 1. Then, is 1 always less than 7? So, the result is always True. Well, if you want to express a situation when 3 is less than 4 and 4 is less than 7 in C, you will write it like this: use this symbol '&&' to indicate 'and'. In Python, it's different. Python allows you to write like a mathematical expression. Such a design in Python, at least for me, having used C for so many years, renders much happiness. With this instance, let's talk about the operator associativity. Like in the expression of "3 < 4 < 7", one operator of '<' is at either side of 4. They are the same, so their priority levels are surely the same. Now, which side of expression will be executed first? It depends on the associativity of this operator. For < , its associativity is from the left to the right. So, "3 less than 4" will be computed first. followed by the computation of "4 less than 7". In comparison operation, pay attention to other two signs as well. One is the equals sign, This equals sign means "comparing equality". We also call it "logical equality". It is different from the single equals sign in assignment. We must pay attention during use. The other sign is "not equal". When solving some judgment questions, we often need to connect several conditions. How to connect them? It requires the use of logical operators. In Python, there are three logical operators: "not", "and" and "or". They mean "reverse the state", "and" and "or", respectively. For instance, what is the effect of this expression? It means looking at the first expression or the second expression. If either of the two is True, the result will be True. As for the next operator, "and", it means that the first expression, and the second expression must both be True if you want the result to be True. So, as we see, "x" here has this value, and it is True that it is less than 5.0. But the "y" here has a value of -1024, and it is False that it is greater than 2.71. So, for True and False, the result is still False. "not" is quite simple, meaning "reverse the logical state". So, it is initially True that x is less than 5.0. With "not", however, the result becomes False. This instance, which we mentioned just now, is quite special. Of course, you are free to write it like this. In Python, there is another type of special operation, called "character operation". It is called the "raw string operator". Let's look at this instance. You guys may feel puzzled at this statement. Let me briefly explain it. The function of open() can be used to open a file. The file path is its first argument, "w" is to write a file. We'll talk about this in particular later. What is its effect? It is to create a file named "test.py" in folder C:\Python. But if we write like this, an error will appear. We must write like this, with an "r" before it. The result is correct now. Why? The reason is if "r" is followed by such a string, all the characters in it will be interpreted according to their literal meanings when interpreted. These characters are rightly the path and the filename we want. So the use of raw string may avoid errors in many cases. What if we do not add this "r"? If we need to use this form, two backslashes are helpful. They are the escape characters to be mentioned in future sections. It means that, in a string, if you want to express a simple text of backslash, you need to use two backslashes. We'll talk about this in details later. So under circumstances similar to this, we use the raw string operator "r", and it often becomes more convenient. Apart from those operations mentioned just now, fundamental operations also include the bitwise operation. Its operators, for example, include left shift and right shift. We do not introduce them here any more. If interested, you may try them by yourselves. Those operations we talked about just now along with bitwise operations allow mixed operation. During use, pay attention to priority, associativity and so on. Of those operations, the results of comparison operations and logical operations, are all Boolean values.