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!
The following assumes that variable a is 10 and variable b is 20:
Operator | Description | Example |
= |
Simple assignment operator | c = a b will a The operation result assignment of b is c |
c = a is equivalent to c = c a | ||
c -= a is equivalent to c = c - a | ||
c *= a is equivalent to c = c * a | ||
c /= a is equivalent to c = c / a | ||
c %= a is equivalent to c = c % a | ||
c **= a is equivalent to c = c ** a | ||
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)
1 - c 的值为: 31 2 - c 的值为: 52 3 - c 的值为: 1092 4 - c 的值为: 52.0 5 - c 的值为: 2 6 - c 的值为: 2097152 7 - c 的值为: 99864
Bitwise operators
In the following table, variable a is 60 and b is 13.
Description | Instance | |
---|---|---|
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 ^ b) and the output result is 49. Binary interpretation: 0011 0001 | ~ | |
(~a) The output result is -61, binary interpretation: 1100 0011, in a The two's complement form of a signed binary number. | ##<< | |
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!
Previous article:PHP learning array_rand() array random selection function
Next article:The reason why echo is used when writing API output in PHP
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
Latest Issues
How to display the mobile version of Google Chrome
Hello teacher, how can I change Google Chrome into a mobile version?
From 2024-04-23 00:22:19
0
11
2434
There is no output in the parent window
document.onclick = function(){ window.opener.document.write('I am the output of the child ...
From 2024-04-18 23:52:34
0
1
1932
Related Topics
More>
|