Welcome to the lesson on Python operators
Operators
Operators are use to perform operations and calculations between values and variables.
There are 6 types of operators.
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
Arithmetic operators
Arithmetic operators are used with numeric data types to perform calculations
| Operators | Name | Examples |
|---|---|---|
| + | addition | a + b |
| - | subtract | a - b |
| multiply | * | a * b |
| / | divide | a / b |
| % | modulus | a % b |
| ** | power of | a ** b |
| // | floor divide | a // b |
Assignment Operators
Assignment Operators are used to assign values to variables.
| Operators | Examples | is also |
|---|---|---|
| = | a = 2 | a = 2 |
| += | a += 2 | a = a + 2 |
| -= | a -= 2 | a = a - 2 |
| *= | a *= 2 | a = a * 2 |
| **= | a **= 2 | a = a ** 2 |
| /= | a /= 2 | a = a / 2 |
| %= | a %= 2 | a = a % 2 |
| //= | a //= 2 | a = a // 2 |
| >>= | a >>= 2 | a = a >> 2 |
| <<= | a <<= 2 | a = a << 2 |
Comparison Operators
Comparison Operators are used to compare between 2 values.
| Operators | Name | Examples |
|---|---|---|
| == | equal to | a == b |
| != | not equal to | a != b |
| > | is greater than | a > b |
| < | is lesser than | a < b |
| >= | is greater than or equal to | a >= b |
| <= | is less than or equal to | a <= b |
Logical Operators
Logical Operators are used to combine statements or variables.
| Operators | description | Examples |
|---|---|---|
| and | returns true if all statement are true | a = 5 and b = 4 |
| or | returns true if either statement is correct | a = 5 or b = 4 |
| not | reverse the result of the statement | not(a = 5 or b = 0) |
Identity and Membership Operators
Identity Operators are used to compare variables and values between each other.
| Operators | description | Examples |
|---|---|---|
| is | returns true if both variables or values are the same. | a is b |
| is not | returns true both variables or values are different | a is not b |
Membership Operators are used to check if a variable or value is in another variable or value
| Operators | description | Examples |
|---|---|---|
| in | returns true if variable 1 is present in variable 2 | a in b |
| not in | returns true variable 1 is not present in variable 2 | a not in b |
End of Python Operators
You have learned operators in simple terms. Let's proceed on to Quiz.