# Python operators

PythonOperators are symbols used to perform operations on values and variables.

We can divide it according to the type of operation performed by the operator:

• Assignment operator
• Arithmetic Operator
• Comparison operator
• Logical Operators
• Bitwise operator

Plus some interesting`is`with`in`.

### Assignment operator

Assignment operators are used to assign values to variables:

``age = 8``

Or assign the variable value to another variable:

``````age = 8
anotherVariable = age``````

Since Python 3.8,`:=` Walrus operatorUsed to assign values to variables as part of another operation. For example in`if`Or in the condition part of the loop. talk about it later.

### Arithmetic Operator

Python has many arithmetic operators:`+`,`-`,`*`,`/`(distribution),`%`(More than),`**`(Exponentiation) and`//`(Floor division):

``````1 + 1 #2
2 - 1 #1
2 * 2 #4
4 / 2 #2
4 % 3 #1
4 ** 2 #16
4 // 2 #2``````

Note that there is no need for spaces between the operands, but this is helpful for readability.

`-`It can also be used as a unary negative operator:

``print(-4) #-4``

`+`Also used to concatenate string values:

``````"Roger" + " is a good dog"
#Roger is a good dog``````

We can combine assignment operators with arithmetic operators:

• `+=`
• `-=`
• `*=`
• `/=`
• `%=`
• ..and many more

example:

``````age = 8
age += 1``````

### Comparison operator

Python defines some comparison operators:

• `==`
• `!=`
• `>`
• `<`
• `>=`
• `<=`

You can use these operators to get boolean values (`True`or`False`), depending on the result:

``````a = 1
b = 2
a == b #False
a != b #True
a > b # False
a <= b #True``````

### Boolean operator

Python provides us with the following Boolean operators:

• `not`
• `and`
• `or`

when using it`True`or`False`Attributes, they work like logical AND, OR, and NOT, and are usually`if`Conditional expression evaluation:

``````condition1 = True
condition2 = False
not condition1 #False
condition1 and condition2 #False
condition1 or condition2 #True``````

Otherwise, please pay attention to the possible causes of confusion.

`or`The value used in the expression returns the value of the first operand, which is not a fake value (`False`,`0`,`''`,`[]`..). Otherwise, it returns the last operand.

``````print(0 or 1) ## 1
print(False or 'hey') ## 'hey'
print('hi' or 'hey') ## 'hi'
print([] or False) ## 'False'
print(False or []) ## '[]'``````

The Python documentation describes it as`if x is false, then y, else x`.

`and`The second parameter is evaluated only when the first parameter is true. Therefore, if the first parameter is false (`False`,`0`,`''`,`[]`..), the parameter is returned. Otherwise, it will calculate the second parameter:

``````print(0 and 1) ## 0
print(1 and 0) ## 0
print(False and 'hey') ## False
print('hi' and 'hey') ## 'hey'
print([] and False ) ## []
print(False and [] ) ## False``````

The Python documentation describes it as`if x is false, then x, else y`.

### Bitwise operator

Some operators are used to deal with bit and binary numbers:

• `&`Perform binary AND
• `|`Perform binary or
• `^`Perform a binary XOR operation
• `~`Perform a binary NOT operation
• `<<`Move left operation
• `>>`Move right operation

Those are rarely used and only used in very special circumstances, but they are worth mentioning.

### `is`with`in`

`is`is calledIdentity operator. It is used to compare two objects and returns true if both are the same object. More information about the object will be given later.

`in`is calledMember operator. Used to determine whether a value is included in a list or another sequence. More information will be provided in the list and other sequences later.