Expression, Boolean expressions and operators, Boolean operators, and

True, any non-zero value, non-empty strings, lists, tuples or dictionary will return True.

False, 0, None, an empty list, tuples and dictionaries will return False.

Boolean expressions are evaluated from left to right, and only when you need the right operand will be calculated. For example, the expression a and b, only when a calculation is True will b. This is sometimes referred to as short-circuit evaluation.

Clear and sharp expressions change the order of operations when parentheses.

First, arithmetic operators

(1)+

In [2]: [1,2,3] + [4,5,6] connecting two lists 
Out [2]: [1, 2, 3, 4, 5, 6]
In [3]: (1,2,3) + (4,) connecting two tuples 
Out [3]: (1, 2, 3, 4)
In [4]: '1234' + 'asdf' connecting the two strings 
Out [4]: '1234asdf'
In [5]: True+3               #True=1
Out[5]: 4

(2)*

Copy the code
In [6]: False*3 #False=0
Out[6]: 0

In [7]: [1,2,3]*3
Out[7]: [1, 2, 3, 1, 2, 3, 1, 2, 3]

In [8]: (1,2,3)*3
Out[8]: (1, 2, 3, 1, 2, 3, 1, 2, 3)

In [9]: 'asdf'*3
Out[9]: 'asdfasdfasdf'
Copy the code

(3) / other integer in arithmetic // List

Copy the code
The In [10]:] 3/2 
Out [10]: for 1.5 

the In [. 11]: 12 is //. 5 
Out [. 11]: 2 

the In [12 is]: 12.0. 5 // 
Out [12 is]: 2.0 

the In [13 is]: - 12 @ 5 rounding down, e.g. * 5 (-. 3). 3 = -12 + 5 * (- 2) = -2 -12 
Out [13 is]: -3 

the In [14]: -12/5 
Out [ 14]: -2.4
Copy the code


(4) I and operands% unfamiliar format string formatting, sorting the separate

Copy the code
In [15]: 10%3
Out[15]: 1

In [16]: 10.0%3
Out[16]: 1.0

In [17]: '%c,%d'%(65,65)
Out[17]: 'A,65'

In [18]: '%f,%s'%(66,68)
Out[18]: '66.000000,68'
Copy the code

(5) ** exponentiation equal pow ()

Copy the code
In [20]: 3**3
Out[20]: 27

In [21]: pow(3,3)
Out[21]: 27

In [22]: pow(3,3,10)   3**3%10
Out[22]: 7

In [23]: 9**0.5
Out[23]: 3.0
Copy the code

Second, relational operators

Copy the code
The In [24]:. 1 <2 <. 4 
Out [24]: True 

the In [25]: {l, 2,3} <{1,2,3,4} 
Out [25]: True 

the In [26 is]: [ 2,3,4] <[1,2,3,4] 
Out [26 is]: False 

the In [27]: [2,3,4] <[2,3,4,5] 
Out [27]: True 

the In [28]: [l, 2,3] <[1, 2,4] 
Out [28]: True 

the In [29]: {2,3,4} <{2,3,4,5} 
Out [ 29]: True 

the In [30]: (l, 2,3) <(1,2,3,4) 
Out [30]: True 

the In [31 is]: (2,3,4) <(1,2, 3,4-) 
Out [31 is]: False 

the in [33 is]: (l, 2,3) == (3,2,1) tuple list are ordered, when the order is not set 
Out [33]: false 

the In [34 is]: [l, 2,3] == [3,2,1] 
Out [34 is]: false 

the In [35]: {l, 2,3} {3,2,1} == 
Out [35]: True
Copy the code


Three, in membership test operators is identical to the test operator

Copy the code
The In [36]: in {l, 2,3}. 3 
Out [36]: True 

the In [37 []:. 3 in [l, 2,3] 
Out [37 []: True 

the In [38 is]: in. 3 (. 1, 2,3) 
Out [38 is]: True 

the in [39]: 'AS' in 'asdf' 
Out [39]: True 

the in [40]: X = [l, 2,3] 

the in [41 is]: Y = [ l, 2,3] 

the in [42 is]: X Y iS if two objects are the same, both having the same memory address 
Out [42 is]: False 

the in [43 is]: X == Y 
Out [43 is]: True 

the In [44 is]: X [. 1] IS Y [. 1] 
Out [44 is]: True 

the In [45]: X [. 1] == Y [. 1] 
Out [45]: True
Copy the code
Copy the code
The In [46 is]: X = [123,123,123] 

the In [47]: X [0] X IS [. 1] based memory management values, with a value in memory only one 
Out [47]: True 

the In [48]: X [. 1] == X [2] 
Out [48]: True
Copy the code
Copy the code
The In [49]: X = [l, 2,3] 

the In [50]: XX = y and y refer to the same object 

the In [51 is]: X IS y 
Out [51 is]: True 

the In [52 is]: x.append (100) 

the In [53 is]: x 
Out [53 is]: [. 1, 2,. 3, 100] 

the In [54 is]: y 
Out [54 is]: [. 1, 2,. 3, 100] will operate the x-y have the same impact
Copy the code

Fourth, the bit operators and set operators

Copy the code
In [55]: {1,2,3} | {3,4,5} to find and re-set 
Out [55]: {. 1, 2,. 3,. 4,. 5} 

the In [56 is]: {1,2 , 3} {3,4,5} & intersection of 
Out [56 is]: {3} 

the In [57 is]: {l, 2,3} ^ {3,4,5} symmetric difference 
Out [57]: { . 1, 2,. 4,. 5} 

the In [58]: {2,3,4} - {2,6,7} difference set 
Out [58]: {3, 4}
Copy the code

Fifth, and not logical operators or
operators and and or will not necessarily return True or False, but rather to give a final calculated value of the expression, but the operator will not return True or False.

Copy the code
In [59]: 3> 5 and a> 7 does not remember a short circuit defined rules 
Out [59]: False 

the In [60]:. 3>. 5 or a>. 7 
Traceback (MOST Recent Last Call): 

  File "<ipython- 60-6dac9f78ce10-INPUT> ", Line. 1, in <Module1> 
    . 3>. 5 or A>. 7 

NameError: name 'A' IS Not defined 

the in [61 is]:. 3 <. 5 or A>. 7 
Out [61 is]: True 

the in [62]: 3 and 5 short-circuit rules 
Out [62 is]:. 5 

the in [63 is]:. 3. 5 or 
Out [63 is]:. 3 

the in [64]:. 3 is not in the calculation result not [1,2,3] only It is True or False 
Out [64]: False 

the In [65]:. 3 Not 
Out [65]: False 

the In [66]: Not [] 
Out [66]: True
Copy the code

 Six comparison operators

When the two values ​​are compared with the operator, the result is a logical value, True or False.

True, any non-zero value, non-empty strings, lists, tuples or dictionary will return True.

False, 0, None, an empty list, tuples and dictionaries will return False.

Boolean expressions are evaluated from left to right, and only when you need the right operand will be calculated. For example, the expression a and b, only when a calculation is True will b. This is sometimes referred to as short-circuit evaluation.

Clear and sharp expressions change the order of operations when parentheses.

First, arithmetic operators

(1)+

In [2]: [1,2,3] + [4,5,6] connecting two lists 
Out [2]: [1, 2, 3, 4, 5, 6]
In [3]: (1,2,3) + (4,) connecting two tuples 
Out [3]: (1, 2, 3, 4)
In [4]: '1234' + 'asdf' connecting the two strings 
Out [4]: '1234asdf'
In [5]: True+3               #True=1
Out[5]: 4

(2)*

Copy the code
In [6]: False*3 #False=0
Out[6]: 0

In [7]: [1,2,3]*3
Out[7]: [1, 2, 3, 1, 2, 3, 1, 2, 3]

In [8]: (1,2,3)*3
Out[8]: (1, 2, 3, 1, 2, 3, 1, 2, 3)

In [9]: 'asdf'*3
Out[9]: 'asdfasdfasdf'
Copy the code

(3) / other integer in arithmetic // List

Copy the code
The In [10]:] 3/2 
Out [10]: for 1.5 

the In [. 11]: 12 is //. 5 
Out [. 11]: 2 

the In [12 is]: 12.0. 5 // 
Out [12 is]: 2.0 

the In [13 is]: - 12 @ 5 rounding down, e.g. * 5 (-. 3). 3 = -12 + 5 * (- 2) = -2 -12 
Out [13 is]: -3 

the In [14]: -12/5 
Out [ 14]: -2.4
Copy the code


(4) I and operands% unfamiliar format string formatting, sorting the separate

Copy the code
In [15]: 10%3
Out[15]: 1

In [16]: 10.0%3
Out[16]: 1.0

In [17]: '%c,%d'%(65,65)
Out[17]: 'A,65'

In [18]: '%f,%s'%(66,68)
Out[18]: '66.000000,68'
Copy the code

(5) ** exponentiation equal pow ()

Copy the code
In [20]: 3**3
Out[20]: 27

In [21]: pow(3,3)
Out[21]: 27

In [22]: pow(3,3,10)   3**3%10
Out[22]: 7

In [23]: 9**0.5
Out[23]: 3.0
Copy the code

Second, relational operators

Copy the code
The In [24]:. 1 <2 <. 4 
Out [24]: True 

the In [25]: {l, 2,3} <{1,2,3,4} 
Out [25]: True 

the In [26 is]: [ 2,3,4] <[1,2,3,4] 
Out [26 is]: False 

the In [27]: [2,3,4] <[2,3,4,5] 
Out [27]: True 

the In [28]: [l, 2,3] <[1, 2,4] 
Out [28]: True 

the In [29]: {2,3,4} <{2,3,4,5} 
Out [ 29]: True 

the In [30]: (l, 2,3) <(1,2,3,4) 
Out [30]: True 

the In [31 is]: (2,3,4) <(1,2, 3,4-) 
Out [31 is]: False 

the in [33 is]: (l, 2,3) == (3,2,1) tuple list are ordered, when the order is not set 
Out [33]: false 

the In [34 is]: [l, 2,3] == [3,2,1] 
Out [34 is]: false 

the In [35]: {l, 2,3} {3,2,1} == 
Out [35]: True
Copy the code


Three, in membership test operators is identical to the test operator

Copy the code
The In [36]: in {l, 2,3}. 3 
Out [36]: True 

the In [37 []:. 3 in [l, 2,3] 
Out [37 []: True 

the In [38 is]: in. 3 (. 1, 2,3) 
Out [38 is]: True 

the in [39]: 'AS' in 'asdf' 
Out [39]: True 

the in [40]: X = [l, 2,3] 

the in [41 is]: Y = [ l, 2,3] 

the in [42 is]: X Y iS if two objects are the same, both having the same memory address 
Out [42 is]: False 

the in [43 is]: X == Y 
Out [43 is]: True 

the In [44 is]: X [. 1] IS Y [. 1] 
Out [44 is]: True 

the In [45]: X [. 1] == Y [. 1] 
Out [45]: True
Copy the code
Copy the code
The In [46 is]: X = [123,123,123] 

the In [47]: X [0] X IS [. 1] based memory management values, with a value in memory only one 
Out [47]: True 

the In [48]: X [. 1] == X [2] 
Out [48]: True
Copy the code
Copy the code
The In [49]: X = [l, 2,3] 

the In [50]: XX = y and y refer to the same object 

the In [51 is]: X IS y 
Out [51 is]: True 

the In [52 is]: x.append (100) 

the In [53 is]: x 
Out [53 is]: [. 1, 2,. 3, 100] 

the In [54 is]: y 
Out [54 is]: [. 1, 2,. 3, 100] will operate the x-y have the same impact
Copy the code

Fourth, the bit operators and set operators

Copy the code
In [55]: {1,2,3} | {3,4,5} to find and re-set 
Out [55]: {. 1, 2,. 3,. 4,. 5} 

the In [56 is]: {1,2 , 3} {3,4,5} & intersection of 
Out [56 is]: {3} 

the In [57 is]: {l, 2,3} ^ {3,4,5} symmetric difference 
Out [57]: { . 1, 2,. 4,. 5} 

the In [58]: {2,3,4} - {2,6,7} difference set 
Out [58]: {3, 4}
Copy the code

Fifth, and not logical operators or
operators and and or will not necessarily return True or False, but rather to give a final calculated value of the expression, but the operator will not return True or False.

Copy the code
In [59]: 3> 5 and a> 7 does not remember a short circuit defined rules 
Out [59]: False 

the In [60]:. 3>. 5 or a>. 7 
Traceback (MOST Recent Last Call): 

  File "<ipython- 60-6dac9f78ce10-INPUT> ", Line. 1, in <Module1> 
    . 3>. 5 or A>. 7 

NameError: name 'A' IS Not defined 

the in [61 is]:. 3 <. 5 or A>. 7 
Out [61 is]: True 

the in [62]: 3 and 5 short-circuit rules 
Out [62 is]:. 5 

the in [63 is]:. 3. 5 or 
Out [63 is]:. 3 

the in [64]:. 3 is not in the calculation result not [1,2,3] only It is True or False 
Out [64]: False 

the In [65]:. 3 Not 
Out [65]: False 

the In [66]: Not [] 
Out [66]: True
Copy the code

 Six comparison operators

When the two values ​​are compared with the operator, the result is a logical value, True or False.

Guess you like

Origin www.cnblogs.com/bchy/p/11685033.html