Operators in Python
What are operators? Let's take a simple example 4 + 5 = 9. In the example, 4 and 5 are called operands and "+" is called an operator.
1 . The power operator
In Java, if we wanted to power a number, we'd probably have to resort to the pow() function in the Math library, but in Python we can use two consecutive *'s to represent the power operation.
a = 5 ** 2 print a
2 . // Operators
A lot of people are going to say, "I know this one. I always use double slashes in my comments, but it's awkward. In Python, the single-line comment symbol is #, and for multi-line comments, you can use ''', so what's // used for? It's used to find the integer part of the division of two numbers.
a = 10.0 // 3 print a
This time it outputs 3.0, the integer part of the division, and the remainder is ignored.
3 . &, | operators
These two operators we all know, & bitwise with , | bitwise or, the following to talk about the operation principle of these two operators, look at a number 7 & 18 is equal to how much? Put it in the compiler and run it.
a = 7 & 18 print a
The result is 2. Why? It's simple, we first need to convert 7 and 18 to binary, 7-> 00000111 18->00010010 , and then sum these two binary numbers, what is summing? Just remember that when both numbers have a 1 in the same place, the result will be 1, otherwise it will be 0. The operation process is as follows
Obviously the result is 2. The | operator follows the same steps as &, but the result of | is 1 if one of the two numbers in the same position is 1. You can do the math yourself, so I won't go into it here.
4 . >>,<< operators
At first glance, when I saw this symbol, my first reaction was far greater than and far less than, which is is the meaning of right shift and left shift, left shift and right shift? How? The same need to convert the decimal number into binary form, and then left and right shift, in fact, left shift once is to let a number multiplied by 2, left shift n times is to let a number multiplied by 2 of the n times, right shift is the opposite, is divided by 2 of the 'n' times.
# coding=utf-8 # Shifted 2 places to the left a = 5 << 2 print a # Shift three places to the right b = 32 >> 3 print b
5 . not, and, or operators
These three are logical operators. In other programming languages if you want to represent or and not, you might use the format ||,&&,! in other programming languages, but in Python you use or,and,not.
a = True b = False c = not a d = a and b e = a or b print c print d print e
The results are, in order, False False True
6 . Instead of going over the operators, let's look at the prioritization of the operators; I made a diagram
The closer you get to the head of the fish, the higher the grade.
Python Control Flow
Control flow is nothing more than if...else, while, for, and so on. Everyone knows how to use the basics, but Python is always special, so here's a look.
1 . if...else
The if judgment statement has an introductory point, as follows
a = 10 if 5 <= a <= 10: print a
In Java, I'm afraid you'd have to write it like this.
int a = 10; if(a >= 5 && a <= 10){ (a); }
2 . while , for
The only thing to remember is that in Python, either for or while can be followed by an else statement, just like if, and the else statement is executed after the loop statement has exited.
a = False while a: print ("I am while") else: print ("I am else") for i in range(1, 2): print ("I am for") else: print ("I am else")
Tip: range()
What does it mean that a function can set the step interval? Try it yourself~
summarize
This is all about operators and control flow in Python, this article is about basic introductory knowledge, but it is also a very important part of the hope that you can help you use Python.