This article mainly explains the operators and control flows of Python. here we will only introduce the unique parts of Python, something similar to other languages, such as addition, subtraction, multiplication, division, and so on. As this article is an entry-level article, experts can skip it directly.
Operators in Python
What is an operator? For example, 4 + 5 = 9. In this example, 4 and 5 are called operands and "+" are called operators.
1. power operators
In Java, if we want to calculate the power of a number, we may need to use the pow () function in the Math library, however, in Python, we can use two consecutive * operators to represent power operations.
a = 5 ** 2print a
2. // operator
Many people may have said this. I know that double slashes are often used for comments. However, in Python, the single line comment symbol is #. you can use ''' to comment multiple lines ''', so // why is it used? Used to calculate the integer part of the division of two numbers.
a = 10.0 // 3print a
At this time, 3.0 is output, that is, the integer part of the division. The remainder is ignored.
3. &, | operator
We all know these two operators, & bitwise AND, | bitwise OR. let's talk about the operation principles of these two operators. what is the value of 7 & 18? Run it in the compiler.
a = 7 & 18print a
The printed result is 2. why? It is very simple. first we need to convert 7 and 18 to binary, 7-> 00000111 18-> 00010010, and then perform and calculate the two binary values. what is and operation? You only need to remember that when the two numbers are at the same position of 1, the result is 1, and the others are 0. The operation procedure is as follows:
Obviously, the result is 2. | the operator is the same as the & operation step. However, during the operation, if one of the two numbers is 1 at the same position, the result is 1, you can perform the operation on your own. I will not go into details here.
4. >>,< operator
At first glance, my first reaction was far greater than and far less than. this is the meaning of right shift and left shift, left shift and right shift? How to move it? Similarly, you need to convert the decimal number to the binary format, and then shift left and right. In fact, moving left is equal to one number multiplied by two, to move n times to the left is to multiply a number by n times of 2. to shift right is to the power of 'n' divided by 2.
# Coding = UTF-8 # shift left 2 places a = 5 <2 print a # shift right three places B = 32> 3 print B
5. not, and, or operator
These three operators are logical operators. in other programming languages, if you want to represent or non-operators, you may use this format | | ,&&,! But in Python, you must use or, and, not.
a = Trueb = Falsec = not ad = a and be = a or bprint cprint dprint e
The result is False, False, True, in turn.
6. let's not introduce operators. let's take a look at the operator priority. I made a diagram.
The so-called control flow is nothing more than if... else, while,. You can basically use it, but Python always has something special. let's take a look at it.
1. if... else
The if judgment statement is worth introducing as follows:
a = 10if 5 <= a <= 10: print a
In Java, you may want to write
int a = 10;if(a >= 5 && a <= 10){ System.out.println(a);}
2. while,
The only thing to remember is that in Python, both for and while can be followed by an else statement like if. The else statement will be executed after the loop statement is jumped out.
a = Falsewhile 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")
Tips: range() The function can set the number of steps. what does it mean? Try it by yourself ~
Summary
The above is all about operators and control flows in Python. This article describes the basics, but it is also an important part. I hope it will be helpful for you to use Python.
For more articles about operators and control flow in Python, refer to PHP Chinese network!