Home > Backend Development > PHP Tutorial > python basic learning assignment operator, bitwise operator

python basic learning assignment operator, bitwise operator

little bottle
Release: 2023-04-06 12:18:01
Original
3239 people have browsed it

Connecting with the previous article, this article mainly describes Python’s assignment operators and bitwise operators, and attaches usage codes. It has certain learning value. Friends who are just getting started must understand it!

Assignment operator

The following assumes that variable a is 10 and variable b is 20:

## = Additional assignment operator c = a is equivalent to c = c a-=Subtractive assignment operatorc -= a is equivalent to c = c - a*= Multiplication assignment operatorc *= a is equivalent to c = c * a/= Division assignment operatorc /= a is equivalent to c = c / a%= Modulo assignment operatorc %= a is equivalent to c = c % a**= Power assignment Operator c **= a is equivalent to c = c ** a##//= The following example demonstrates the operation of all assignment operators in Python:
Operator
Description Example
=
Simple assignment operator c = a b will a The operation result assignment of b is c







Integer division assignment operator
c //= a is equivalent to c = c // a

#!/usr/bin/python3
a = 21
b = 10
c = 0
c = a + b
print ("1 - c 的值为:", c)
c += a
print ("2 - c 的值为:", c)
c *= a
print ("3 - c 的值为:", c)
c /= a 
print ("4 - c 的值为:", c)
c = 2
c %= a
print ("5 - c 的值为:", c)
c **= a
print ("6 - c 的值为:", c)
c //= a
print ("7 - c 的值为:", c)
Copy after login

The output result of the above example:

1 - c 的值为: 31
2 - c 的值为: 52
3 - c 的值为: 1092
4 - c 的值为: 52.0
5 - c 的值为: 2
6 - c 的值为: 2097152
7 - c 的值为: 99864
Copy after login

Bitwise operators

Bitwise operators treat numbers as binary to perform calculations. The bitwise operation rules in Python are as follows:

In the following table, variable a is 60 and b is 13.

Operator&|##^Bitwise XOR operator: when two corresponding When the binary bits are different, the result is 1(a ^ b) and the output result is 49. Binary interpretation: 0011 0001~ bitwise Negation operator: Negate each binary bit of the data, that is, change 1 to 0, and change 0 to 1(~a) The output result is -61, binary interpretation: 1100 0011, in a The two's complement form of a signed binary number. Left shift operator: Each binary digit of the operand is shifted to the left by a certain number of bits, specified by the number on the right of "<<" The number of digits to move, the high bits are discarded, and the low bits are filled with 0s. Right shift operator: " >>"All binary digits of the operand on the left are shifted to the right by a certain number of digits,">>"The number on the right specifies the number of digits to move
DescriptionInstance
Bitwise AND operator: Two values ​​participating in the operation, if the two corresponding bits are 1, the result of the bit is 1, otherwise it is 0(a & b) The output result is 12, binary interpretation : 0000 1100
Bitwise OR operator: As long as one of the two corresponding binary bits is 1, the result bit will be 1. (a | b) The output result is 61, binary interpretation: 0011 1101
##<<
a << 2 The output result is 240, binary interpretation: 1111 0000>>
a >> 2 The output result is 15, binary Explanation: 0000 1111

以下实例演示了Python所有位运算符的操作:

#!/usr/bin/python3
a = 60            # 60 = 0011 1100 
b = 13            # 13 = 0000 1101 
c = 0
c = a & b;        # 12 = 0000 1100
print ("1 - c 的值为:", c)
c = a | b;        # 61 = 0011 1101 
print ("2 - c 的值为:", c)
c = a ^ b;        # 49 = 0011 0001
print ("3 - c 的值为:", c)
c = ~a;           # -61 = 1100 0011
print ("4 - c 的值为:", c)
c = a << 2;       # 240 = 1111 0000
print ("5 - c 的值为:", c)
c = a >> 2;       # 15 = 0000 1111
print ("6 - c 的值为:", c)
Copy after login

以上实例输出结果:

1 - c 的值为: 12
2 - c 的值为: 61
3 - c 的值为: 49
4 - c 的值为: -61
5 - c 的值为: 240
6 - c 的值为: 15
Copy after login

相关教程:Python3视频教程

The above is the detailed content of python basic learning assignment operator, bitwise operator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template