[2.16] Python study notes operators, bitwise operators, if-else statements

Composite Operators

a *= b  # a = a * b
a += b  # a = a + b
a -= b  # a = a - b
...

Bitwise Operators

Digital binary operation

Bitwise AND

&, Are a bit, the bit corresponding to the return value is also a

print( 5 & 7 )

\ (5 \) binary form \ (101 \) , \ (7 \) binary format \ (111 \)
bit and then returns \ (101 \)

Can be used to remove a specified number of bits in the
example, I was wondering \ (114,514 \) the first two binary number, with the first four digits of the first five, I can make it bit and \ (11001 _ {(2)} \)

Bitwise or

\ (| \) , The corresponding bits as long as there is a \ (1 \) , shall be returned \ (1 \)

print( 5 | 7 )

Return \ (7 \)

Bitwise XOR

^, The corresponding bits are not the same, return \ (1 \) , returns the same \ (0 \)

print( 5 ^ 7 )

The return value is \ (2 \) , i.e. \ (_ {10 (2)} \)
several small Properties:

  1. It may be understood to not carry binary addition
  2. Meanwhile commutative and associative
  3. \(x\) ^ \(0 = x, x\) ^ \(x = 0\)
  4. By the \ (3 \) can be introduced, \ (X \) ^ \ (Y \) ^ \ (Y = X \) ^ \ (X = 0 \)

Bitwise

~, Each bit \ (1 \) becomes \ (0 \) , \ (0 \) becomes \ (1 \)
Note: If you want to achieve a several negated, only the desired bits are XOR \ (1 \) , and the remaining bits are \ (0 \) is the number of
example, to retrieve \ (123 \) in the binary section \ (2,4,6 \) bit takes the value of the back, only the exclusive-oR \ (101010 \)

\ (if \) expression

If it is true ( \ (True \) ), then execute \ (if \) next statement, if it is false ( \ (False \) ), is skipped

\ (Python \) is very strict indentation, indentation is not correct, it is being given the
pseudo-code format:

if 表达式 : 
    语句

If you write the following is wrong, because not indented

if 表达式 : 
语句

It is an expression of the contents of the judgment, such as

a == b, a > b ...

If true then the statement is executed under if

Further there are several situations for false:
1. \ (IF \) expression of the return value is \ (0 \) or \ (0.0 \)
2. The return value is an empty string '' or ''
3 The return value is \ (None \)
4. The return value is a Boolean value \ (False \)

The remaining values ​​are true

\ (if-else \) statement

Similarly, \ (Python \) also supports \ (IF-the else \) , but there are very strict indentation

if 表达式 :
    语句1
else : 
    语句2

If the expression is true, statement 1 is executed, if false, the statements 2

Guess you like

Origin www.cnblogs.com/with6676/p/12319765.html
Recommended