This is the 7th day of my participation in Gwen Challenge
The operator
Operators are used to perform program code operations on more than one operand item.
For example: 2+3, the operands are 2 and 3, and the operator is +
Python operator.
Click the link below to jump to the corresponding knowledge points.
- Arithmetic operator
- Comparison operator
- The assignment operator
- Logical operator
- An operator
- Member operator
- Identity operator
- Python operator priority
IPython uses an introduction
IPython is a Python interactive Shell that is much easier to use than the default Python Shell. It supports automatic variable completion, automatic indentation, bash Shell commands, and many useful functions.
Here you’ll use the IPython interactive interpreter to test Python syntax.
The primary interface between a text operating system and the outside world is called the Shell. The Shell is the outermost layer of an operating system. The Shell manages your interaction with the operating system: waits for your input, interprets it to the operating system, and processes the output of various operating systems.
A Shell, commonly known as a Shell (to distinguish it from a core), is software that provides the user with a Command Interpreter, similar to CMD and PowerShell in Windows. It receives commands entered by the user and invokes the corresponding application.
The installationIPython
To install IPython, open CMD and enter the following command
pip install ipython
Copy the code
The Package Installer for Python (PIP) is a Python package management tool that enables you to search for, download, install, and uninstall Python packages. When the Python interpreter is installed, it is configured for the user to use directly.
useIPython
To enter the ipython interpreter, use the Win + R shortcut keys to call up the run window and type ipython or enter ipython in the CMD window
Then enter the corresponding Python syntax to get the corresponding interpretation. The following presentation
advantages
- Suitable for learning/validating Python syntax or local code
disadvantages
- Code cannot be saved
- Not suitable for running programs that are too large
Arithmetic operator
The operator | describe | The instance |
---|---|---|
+ |
add | 3 + 6 |
- |
Reduction of | Ten to five |
* |
take | 10 * 20 |
/ |
In addition to | 10/20 |
// |
Take the divisible | Returns the integer part of the division (quotient)9 / / 2Output Result 4 |
% |
modulo | Returns the remainder of the division9% 2 |
支那 |
power | Also known as power, power,2 * * 3 |
IPython test
Addition and subtraction arithmetic operator tests
In [15] :# Addition
In [16]: a = 3
In [17]: b = 6
In [18]: c = a + b
In [19]: c
Out[19] :9
In [20]: a + b
Out[20] :9
In [21] :# subtraction
In [22]: a = 10
In [23]: b = 5
In [24]: a - b
Out[24] :5
Copy the code
Multiplication and division arithmetic operator test
In [25] :# Multiplication
In [26]: a = 10
In [27]: b = 20
In [28]: a * b
Out[28] :200
In [29] :10 * 20
Out[29] :200
In [30] :# division
In [31] :10 / 20
Out[31] :0.5
In [32] :# take divisible
In [33] :10 / 3 # in addition to the ordinary
Out[33] :3.3333333333333335
In [34] :10 //3 # divisible
Out[34] :3
Copy the code
Tests for the mod and power arithmetic operators
In [35] :# modulo
In [36] :10 % 3
Out[36] :1
In [37] :# power operation
In [38] :2支那3
Out[38] :8
In [39] :2支那10
Out[39] :1024
Copy the code
Comparison operator
Let’s say a is equal to 20, and b is equal to 30
The operator | describe | The instance |
---|---|---|
= = |
Is equal to the | Return False |
! = |
Is not equal to | (a ! = b) returns True |
> |
Is greater than | A > b returns False |
< |
Less than | (a < b) returns True |
> = |
Greater than or equal to | (a >= b) returns False |
< = |
Less than or equal to | (a <= b) returns True |
IPython test
Equal and unequal comparison operator tests
In [41] :# equals the comparison operator
In [42]: a = 20
In [43]: b = 30
In [44]: a == b
Out[44] :False
In [45] :# is not equal to
In [46]: a ! = b Out[46] :True
Copy the code
Greater than and less than comparison operator tests
In [47] :# is greater than the
In [48]: a > b
Out[48] :False
In [49] :# is less than
In [50]: a < b
Out[50] :True
Copy the code
Greater than or equal to, less than or equal to comparison operator tests
In [51] :# greater than or equal to
In [52]: a >= b
Out[52] :False
In [53] :Less than or equal to
In [54]: a <= b
Out[54] :True
Copy the code
The assignment operator
The operator | describe | The instance |
---|---|---|
= |
A simple assignment operator | C = a + b Assigns the result of a + b to c |
+ = |
The addition assignment operator | C plus a is the same thing as c is equal to c plus a |
- = |
The subtraction assignment operator | C minus a is the same thing as c minus a |
* = |
Multiplication assignment operator | C times a is the same thing as c times a |
/ = |
The division assignment operator | C/a is the same thing as c = c/a |
/ / = |
Takes the divisible assignment operator | C //= a is equivalent to c = c // a |
% = |
takedie(remainder) assignment operator | C %= a is the same thing as c = c % a |
* * = |
Power assignment operator | c **= a Equivalent to c = c** a |
IPython test
Equal, plus, minus, and equal assignment operator tests
In [56] :# Assignment operator test
In [57] :# is equal to assignment
In [58]: a = 10
In [59]: b = 20
In [60]: c = a + b
In [61]: c
Out[61] :30
In [62] :# and equal
In [63]: a = 1
In [64]: a += 1
In [65]: a
Out[65] :2
In [66] :# is equal to the reduction
In [67]: a = 10
In [68]: a -= 1
In [69]: a
Out[69] :9
Copy the code
Multiply equal, divide equal, and divide exactly assignment operator tests
In [70] :# take is equal to the
In [71]: a = 10
In [72]: a *= 5
In [73]: a
Out[73] :50
In [74] :# in addition to equal
In [75]: a = 100
In [76]: a /= 10
In [77]: a
Out[77] :10.0
In [78] :Take the divisible assignment operation
In [79]: a = 10
In [80]: a /= 3 # Ordinary division is equal to
In [81]: a
Out[81] :3.3333333333333335
In [84]: a = 10
In [85]: a //= 3 # take divisible
In [86]: a
Out[86] :3
Copy the code
Take the remainder, power assignment operator test
In [87] :# take remainder assignment
In [88]: a = 10
In [89]: a %= 3
In [90]: a
Out[90] :1
In [91] :# Power assignment
In [92]: a = 2
In [93]: a **= 3
In [94]: a
Out[94] :8
Copy the code
It’s an assignment based on an arithmetic operator
Logical operator
The operator | Logical expression | describe |
---|---|---|
and |
x and y |
Boolean “and”– beBoth sides of this expression are satisfiedDid not return toTrue , otherwise,False |
or |
x or y |
Boolean “or”– Returns if one of the expressions is satisfiedTrue Neither side is satisfied with talentFalse |
not |
not x |
Boolean “not”– If x is True, return False. If x is False, it returns True. |
and
Logical operator IPython test
In [100] :# and logic operator test
In [101]: age = 21
In [102]: age > 18 and age < 30
Out[102] :True
In [103]: age > 30 and age < 18
Out[103] :False
In [108]: age < 18 and age == 21
Out[108] :False
Copy the code
When using the and operator, the right-hand expression is not evaluated if the left-hand expression does not meet the criteria
In [115]: a = 21
In [116]: a > 18 and a == 1 / 0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-116-9ea6c0ccc2d0> in <module>
----> 1 a > 18 and a == 1 / 0
ZeroDivisionError: division by zero
In [117]: a < 18 and a == 1 / 0
Out[117] :False
Copy the code
So to prove this with a division by zero error message,
a = 21
a > 18 and a==1/0
Copy the code
The Python interpreter reported an error in division by zero because a == 1/0.
a = 21
a < 18 and a==1/0
Copy the code
First, the expression a < 18 does not satisfy the condition, because it is also the and operator, it does not perform the right a == 1/0 operation, so it does not give an error and False.
or
Logical operator IPython test
In [118] :# or logical operator test
In [119]: score = 75
In [120]: score > 60 or socre < 80
Out[120] :True
In [121]: score > 60 or socre > 90
Out[121] :True
In [123]: score > 100 or score > 60
Out[123] :True
In [124]: score < 60 or score > 100
Out[124] :False
Copy the code
When using the OR logical operator, the expression on the right is not evaluated if the expression on the left satisfies the condition
Again, divide by zero:
In [126]: score = 95
In [127]: score > 90 or score == score / 0
Out[127] :True
In [128]: score < 90 or score == score / 0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-128-fd77ee8475bb> in <module>
----> 1 score < 90 or score == score / 0
ZeroDivisionError: division by zero
In [129] :Copy the code
not
Logical operator IPython test
In [137]: a = 100
In [138]: a == 100
Out[138] :True
In [139]: a == 10
Out[139] :False
In [140] :not a == 100
Out[140] :False
In [141] :not a == 10
Out[141] :True
Copy the code
The not operator is the opposite of an expression, False if it is True and True if it is not
An operator
Bitwise operators evaluate numbers as binary, and Python has the following table bit operators
The operator | describe |
---|---|
& |
Bitwise andOperator: Two values involved in the operation, ifBoth corresponding bits are1 , then the result of this bit is1 , or for0 |
| |
Bitwise orOperator: as long as one of the two corresponding binaries is1 , the result bit is1 . |
^ |
The bitwise exclusive orOperator: whenWhen two corresponding binaries differ, the result is 1 |
~ |
According to the notOperator: invert each binary bit of data, that is, change 1 to 0 and 0 to 1.~xSimilar to the-x-1 |
<< |
Move the leftOperator: each binary of the operand moves several bits to the left. The number to the right of “<<” specifies the number of bits to move.Discard the high ones and fill the low ones with zeros. |
>> |
The rightOperator: Move all the binary bits of the operand to the left of “>>” several right, and the number to the right of “>>” specifies the number to move |
IPython test
The default number we enter is base 10, so to make the test clearer, we use bin() to convert base 10 to base 2.
bin()
Simple use of functions
In [6] :bin(10)
Out[6] :'0b1010'
In [7] :bin(8)
Out[7] :'0b1000'
Copy the code
0b is binary
Bitwise and, bitwise or bitwise operator tests
In [0] :# bitwise and &
In [1] :bin(10)
Out[1] :'0b1010'
In [2] :bin(8)
Out[2] :'0b1000'
In [3]: a = 10
In [4]: b = 8
In [5]: a & b
Out[5] :8
In [6] :bin(a & b)
Out[6] :'0b1000'
In [7] :# or by location
In [8]: a | b
Out[8] :10
In [9] :bin(a | b)
Out[9] :'0b1010'
Copy the code
Bitwise xor, bitwise inverse, bitwise operator test
In [10] :# bitwise xOR
In [11]: a = 10
In [12]: b = 8
In [13] :bin(a)
Out[13] :'0b1010'
In [14] :bin(b)
Out[14] :'0b1000'
In [15]: a ^ b
Out[15] :2
In [16] :bin(a ^ b)
Out[16] :'0b10'
In [17] :# reverse by bit
In [18]: a = 13
In [19] :bin(a) # 0000, 1101,
Out[19] :'0b1101'
In [20] :bin(~a) # 1111 1110 2-base complement with sign bits is rendered
Out[20] :'-0b1110'
In [21]: ~a
Out[21] : -14
Copy the code
After bitwise inversion, as the form of binary complement is saved, it involves the knowledge points of binary origin, inversion and complement
Source code representation is the simplest representation of machine numbers. The highest bit is a sign bit. A sign bit of 0 means that the number is positive, and a sign bit of 1 means that the number is negative. For example, if [x] = 0000 0001, the decimal value is 1. If [x] = 1000 0001, the decimal value is -1.
Conversion skills of original, inverse and complement
- Positive numbers have the same original and inverse complement
- The complement of a negative number is equal to its inverse plus 1, the sign bit remains unchanged when the inverse is turned, and the rest are reversed by bits
- The complement of a complement is the source code
So when a is equal to 13, there are eight bits representing a binary
- 13 Binary:
0000, 1101,
- 13 If the digits are reversed:
1111, 0010,
, this isBinary complement with signed bits
Therefore, 1111 0010 complement code should be converted into the original code. Assuming that [x] complement code is 1111 0010, it can be concluded that the complement of the complement code is the original code
[x] =1111The original = 0010 [x]1000 1101 + 1 # 1 is decimal, replaced by 8-bit 2-base 0000 0001,2 base operation is 1 on every 2The original = [x]1000 1110
Copy the code
[x] complement = 1111 0010 shows that [x] = 1000 1110, the sign bit is 1 is negative, so it is -14 in decimal. The result of derivation is in agreement with that of test.
Left move, right move, bit operator test
In [27] :# left < <
In [27]: a = 10
In [28] :bin(a)
Out[28] :'0b1010' # 0000, 1010,
In [29] :bin(a << 2)
Out[29] :'0b101000' # 0010, 1000,
In [30]: a << 2
Out[30] :40
In [31] :# moves to the right > >
In [32]: b = 7
In [33] :bin(7)
Out[33] :'0b111' # 0000, 0111,
In [34] :bin(b >> 2)
Out[34] :'0b1' # 0000, 0001,
In [35]: b >> 2
Out[35] :1
Copy the code
Member operator
The member operator tests whether a sequence contains a specified member
The operator | describe |
---|---|
in |
Returns if a value is found in the specified sequenceTrue Otherwise returnFalse |
not in |
Returns if no value is found in the specified sequenceTrue Otherwise returnFalse |
IPython test
In [37] :3 in (1.2.3)
Out[37] :True
In [38] :3 not in (1.2.3.4.5)
Out[38] :False
In [39] :0 not in (1.2.3)
Out[39] :True
Copy the code
Identity operator
The operator | describe |
---|---|
is |
Is determines whether two identifiers refer to an object |
is not |
Is not determines whether two identifiers refer to different objects |
IPython test
In [40]: a = 20
In [41]: b = 20
In [42]: a is b
Out[42] :True
In [43]: a is not b
Out[43] :False
In [44]: a is not None
Out[44] :True
Copy the code
Difference between is and == : IS is used to determine whether two variable reference objects are the same, and == is used to determine whether the values of reference variables are the same.
In [46]: a = [1.2.3]
In [47]: b = a
In [48]: a is b
Out[48] :True
In [49]: a == b
Out[49] :True
In [50]: c = [1.2.3]
In [51]: a is c
Out[51] :False
In [52]: a == c
Out[52] :True
Copy the code
Python operator priority
The table below is listed in order of priority
The operator | describe |
---|---|
支那 |
Index (highest priority) |
~ + - |
Bitwise flip, unary plus and minus (the last two methods are called +@ and -@) |
% * / / / |
Multiply, divide, find the remainder and take the exact division |
+ - |
Addition subtraction |
>> << |
Shift right, shift left operator |
& |
Bitwise and operators |
^ | |
Bitwise xor, bitwise or operators |
< = < > > = |
Comparison operator |
= =! = |
Equal operator |
= %= /= //= -= += *= **= |
The assignment operator |
is is not |
Identity operator |
in not in |
Member operator |
not and or |
Logical operator |
Operator priority, do not remember all, if you do not know the priority of the operation process can use () to increase the priority of the operator.
In [53]: a = 5
In [54]: b = 6
In [55]: c = 10
In [56]: a + b * c
Out[56] :65
In [57]: (a + b) * c
Out[57] :110
Copy the code
The tail language
✍ Code writes the world and makes life more interesting. ❤ ️
✍ thousands of rivers and mountains always love, ✍ go again. ❤ ️
✍ code word is not easy, but also hope you heroes support. ❤ ️