# How IOS developers learn python programming 2-operator 1

2022-01-31 04:48:58

This is my participation 11 The fourth of the yuegengwen challenge 2 God , Check out the activity details ：2021 The last time Gengwen challenge .

`Python` The language supports the following types of operators :

1. Arithmetic operator
2. Compare （ Relationship ） Operator
3. Assignment operator
4. Logical operators
5. An operator
6. member operator
7. Identity operator
8. Ternary operator

### Arithmetic element operator

`Python` Medium ` Arithmetic element operator ` It's different from what we usually use `OC` perhaps `Swift` Is basically the same as , What's more special is ：

• `**`： power , return `x` Of `y` The next power . for example ：`10**20`, Express `10` Of `20` Power .
• `//`： Divide and conquer , Returns the integer part of the quotient . for example `9/2` Output `4`.

What should be paid attention to during normal use ：

1.  `/` The result is a floating point number , Even if both numbers are integers , The result is also a floating-point number ：
``````>>> 9/3
3.0

>>> 10/3
3.3333333333333335
Copy code ``````
1.  `//` Divide and conquer , Only integer part , The remainder is discarded ：
``````>>> 10//3
3

>>> -10//3
-4  #  Rounding down
Copy code ``````
1.  `%` Remainder operation
``````>>> 10%3
1

>>> -10%3
2	#  Rounding down
Copy code ``````
1.  `divmod()` Get quotient and remainder at the same time , The return value is `tuple (x//y, x%y)`
``````>>> divmod(10,3)
(3,1)
Copy code ``````

️ Be careful ： Because of the precision of floating point numbers ,`Python` There are also some small computational problems ：

``````0.1+0.1+0.1-0.3
5.551115123125783e-17
Copy code ``````

have access to `decimal` modular ：

``````from decimal import Decimal
Decimal('0.1')+Decimal('0.1')+Decimal('0.1')-Decimal('0.3')
Decimal('0.0')
Copy code ``````

### Comparison operator

`Python` Medium ` Comparison operator ` It's different from what we usually use `OC` perhaps `Swift` Is basically the same as , What's more special is ：

• `<>`： It's not equal to , Compare whether two objects don't want to wait , similar `!=`.(`python3` in `<>` Have been abandoned )

What to pay attention to ：

1.  `==` Compare values （`int` And `str` You can't directly compare ）：
``````>>> a = 1
>>> b = 2
>>> a != b
True
>>> a <> b
File "<stdin>", line 1
a <> b
^
SyntaxError: invalid syntax
Copy code ``````
1. There is a comparison between strings `ASCII` value ：
``````>>> 'abc' < 'xyz'   # 97 98 99 < 120 121 122
True

>>> (3,2) < ('a','b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
Copy code ``````
1. Continuous comparison ：
``````>>> 3 > 2 > 1	# 3 > 2 and 2 > 1
True
>>> 3 > 2 > 2	# 3 > 2 and 2 > 2
False
>>> (3 > 2) > 1	# True and True > 1
False
Copy code ``````

### Assignment operator

Operator explain Example
= assignment x = y
-= Assignment reduction x -= y, x = x - y
*= Take the assignment x *= y, x = x * y
/= In addition to the assignment x /= y, x = x / y
%= Take the remainder and assign x %= y, x = x % y
**= Power assignment x **= y, x = x ** y
//= Take an integer and assign a value x //= y, x = x // y
&= Bitwise and assignment x &= y, x = x & y
= By bit or by assignment
^= Assign values by bitwise exclusive or x ^= y, x = x ^ y
<<= Shift left assignment x <<= y, x = x << y
>>= Shift assignment right x >>= y, x = x >> y

What to pay attention to ：

1.  `python` I won't support it `a++` grammar ：
``````>>> a++ 		# python I won't support it a++ grammar
File "<stdin>", line 1
a++
^
SyntaxError: invalid syntax
Copy code ``````
1.  `+=` Can't open write `+ =`：
``````>>> a + = 1		#  Can't open write  + =
File "<stdin>", line 1
a + = 1
^
SyntaxError: invalid syntax
Copy code ``````

### An operator

`Python` Medium ` Arithmetic element operator ` It's different from what we usually use `OC` perhaps `Swift` Is basically the same as .

### Logical operators

`Python` Support for logical operators , But not supported `&&` and `||`, Instead, more humanized English words `and` `or` `not`（ It's all lowercase ）：

• `a and b`  Both are `True` The result is `True`
• `a or b`  One of the two is `True` The result is `True`