Python operators and expressions
In programming, Operators are special symbols (+, -, /) that are used for computations. The values that an operator acts on are called operands, operands could be variables or direct values. An expression is a combination of operator and operands.
Objectives
-
Python Arithmetic operators and expressions
-
Python Assignment operators and expressions
-
Python Comparison operators and expressions
-
Python Logical operators and expressions
-
Python Identity operators and expressions
-
Python Membership operators and expressions
Python Arithmetic operators and expressions
Used for mathematical calculation
Name | Operator | Syntax | Example | Output |
---|---|---|---|---|
Addition | + | a + b | | 21 |
Subtraction | - | a - b | | 1 |
Multiplication | * | a * b | | 132 |
Division | / | a / b | | 1.0909090909090908 |
Modulus | % | a % b | | 1 |
Exponentiation | ** | a ** b | | 8 |
Floor division | // | a // b | | 4 |
Python Assignment operators and expressions
Assignment operator basically responsible for assign value into variable. For example
a = 100
print(a)
- Here
-
-
We assigned 100 into the variable
-
Then print will print 100 because, earlier we assigned 100 there
-
Operator | Short form | Actual form | Description |
---|---|---|---|
+= | a += 3 | a = a + 3 | Add plus 3 with value of a and again assigned to a |
-= | a -= 3 | a = a - 3 | Subtract 3 with value of a and again assigned to a |
*= | a *= 3 | a = a * 3 | Multiply 3 with value of a and again assigned to a |
/= | a /= 3 | a = a / 3 | Divide 3 with value of a and again assigned to a |
%= | a %= 3 | a = a % 3 | Modulus 3 with value of a and again assigned to a |
//= | a //= 3 | a = a // 3 | Divides 3 with value of a and again assigned floor value to a |
**= | a **= 3 | a = a ** 3 | Exponentiation 3 with value of a and again assigned to a |
Python Comparison operators and expressions
Responsible for make comparison of 2 operand, means variable or value
Name | Operator | Example | Output |
---|---|---|---|
Equal | == | | False |
Not equal | != | | True |
Greater than | > | | True |
Less than | < | | False |
Greater than or equal to | >= | | True |
Less than or equal to | < = | | False |
Python Logical operators and expressions
Logical operators are used to combine conditional statements
Name | Operator | Description | Example | Output |
---|---|---|---|---|
And | and | Returns True if both statements are true | | False |
Or | or | Returns True if one of the statements is true | | False |
Not | not | Reverse the result, returns False if the result is true | | True |
Python Identity operators and expressions
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location
Name | Operator | Description | Example | Output |
---|---|---|---|---|
Is | is | Returns True if both variables are the same object | | False |
Is Not | is not | Returns True if both variables are not the same object | | True |
Python Membership operators and expressions
Name | Operator | Description | Example | Output |
---|---|---|---|---|
In | in | Returns True if a sequence with the specified value is present in the object | | True |
Not in | not in | Returns True if a sequence with the specified value is not present in the object | | False |