Python six types of operators (Diary)

Learning Python for a long time, and I alone Python summed up in the more common six kinds of operators, feeling a little incomplete, we hope everyone can discuss with Python study together!

First, arithmetic operators

plus

Less -

Take *

In addition to /

Modulo%

ToSei //

Opposite sign -

The same number +

Absolute value abs (x)

To Integer int (x)

Converted to float float (x)

Complex complex (re, im)

A complex conjugate c.conjugate ()

It returns a value of (x // y, x% y) divmod (x, y)

y-th power of x (two way) x ** y, pow (x, y)

 

Second, comparison operators

Less than (<), less than or equal to (<=), equality (==), greater than (>), less than (> =), not equal (! =), IS (two identifiers is determined by reference to an object), IS not (not the same object)

1. eight comparison operators of the same priority

2.python chain allows comparison x <y <z, which corresponds to x <y and y <z

3. The size of the complex is not available for comparison, it is limited to equal

 

Third, the logical operators

x or y shorting operator (which only the first operand is false only a value of the second operand is calculated)

x and y shorting operator (which only the first operand is a value True only the second operand)

Not x not low priority (not a == b corresponds not (a == b)), a = not b is wrong

 

Fourth, bitwise operators

python bits operator is regarded as a binary digital be calculated

& Bitwise AND operator: the result parameter calculating two values ​​(binary), as if the two are the corresponding 1, instead of 1, otherwise 0

| Bitwise OR operator: involved in computing the value of two, only the two bits corresponding to the number, there is a 1, the result is a

^ Bitwise exclusive OR operator: the number of bits corresponding to different values ​​of 1, 0 is the same

~ Bitwise operators: for a single number, binary format, the corresponding bit values ​​negated, becomes a variable 0,0 1

Mobile operator << left: a number of bits of each bit to the left, the high discard, low fill 0

Mobile operator >> Right: shifting each bit and a number of bits

>>> a = bin(20)
>>> a
'0b10100'
>>> b = bin(30)
>>> b
'0b11110'

Binary number: the default 8, 0b prefix to identify, from high status to read, write from right to left, not enough to fill digits 0, 8 up until reading

Python binary function call bin, 0b representing a binary identifier, the default binary 8 bits, a, b corresponding to the binary number:

a = 25 = 0001 1001

b = 62 = 0011 1110

a&b =24= 0001 1000

a|b =63 = 0011 1111

a^b = 39 = 0010 0111

~a = -26 = 1110 0110

negative number:

Source: 00,011,010

Anti-code: 11100101

Complement: 11,100,110

a<<2 = 100 = 0110 0100

a>>2 = 6= 0000 0110

>>> bin(25)
'0b11001'
>>> bin(62)
'0b111110'
>>> 25&62
24
>>> bin(24)
'0b11000'
>>> 25|62
63
>>> bin(63)
'0b111111'
>>> 25^62
39
>>> bin(39)
'0b100111'
>>> ~25
-26
>>> bin(-26)
'-0b11010'
>>> 25<<2
100
>>> bin(100)
'0b1100100'
>>> 25>>2
6
>>> bin(6)
'0b110'

verification

>>> eval('0b00011000')
24
>>> eval('0b00111111')
63
>>> eval('0b00100111')
39
>>> eval('-0b00011010')
-26
>>> eval('0b01100100')
100
>>> eval('0b00000110')
6

Fifth, the assignment operator

= Simple assignment

Addition assignment operator + =

- = subtraction assignment operator

* = Subtraction assignment operator

/ = Division assignment operator

% Act = Remainder assignment operator

** = power assignment operation

= // Assignment operator take divisible

 

Six members of the operator

python provides the operator member, a test element is in the sequence (squence) in

in if the specified element in the sequence, return True, otherwise False

If the element is not specified not in sequence, return True, otherwise False

Thank you, everyone watching!

Guess you like

Origin www.cnblogs.com/hewanli/p/11521197.html
Recommended