Assignment and more Operators
M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 10 min read
Assignment Operators in Python: Python also supports compound assignment operators, which combine an operation with assignment
Ø = (Assign)
Ø += (Add and Assign)
Ø -=( Subtract and Assign)
Ø *= (Multiply and Assign)
Ø /= (Modulus and Assign)
Ø %= (Divide(floor) and Assign)
Ø **= (Exponent and Assign)
|
Operator |
Name |
Description |
Syntax |
Example (X=10, Y=20) |
|
= |
assigns |
It is used to assign the value of the right side of the expression to the left side operand. |
z = x + y |
z = 10 + 20 |
|
+= |
Add and Assign |
It is used to add the right side operand with the left side operand and then assigning the result to the left operand. |
x += y #x = x + y |
x += y print(x) |
|
-= |
Subtract and Assign |
It is used to subtract the right operand from the left operand and then assigning the result to the left operand. |
x -= y #x = x-y |
x-= y print(x) |
|
*= |
Multiply and Assign |
It is used to multiply the right operand with the left operand and then assigning the result to the left operand. |
x *= y #x = x * y |
x *= y print(x) |
|
/= |
Divide and Assign |
It is used to divide the left operand with the right operand and then assigning the result to the left operand |
x /= y #x = x / y |
x /= y print(x) |
|
%= |
Modulus and Assign |
It is used to take the modulus using the left and the right operands and then assigning the result to the left operand. |
x %= y #x = x % y |
x %= y print(x) |
|
//= |
Divide (floor) and Assign |
It is used to divide the left operand with the right operand and then assigning the result(floor) to the left operand. |
x //= y #x = x // y |
x //= y print(x) |
|
**= |
Exponent and Assign |
It is used to calculate the exponent (raise power) value using operands and then assigning the result to the left operand. |
x **= y #x = x ** y |
x **= y print(x) |
Example: Python program for Assignment operators .
|
x = int(input('Enter first number: ') ) y = int(input('Enter second number: ') ) x+=y print(x) print('x+=y:', x+y) print('x-=y:', x-y) print('x*=y:', x*y) print('x%=y:', x%y) print('x**=y:', x**y) print('x//=y:', x//y) |
OUTPUT: Enter first number: 32 Enter second number: 6 x=y: False x+=y: 38 x-=y: 26 x*=y: 192 x%=y: 2 x**=y: 1073741824 x//=y: 5 |
Logical Operators: Logical operators are used on conditional statements (either True or False). They perform Logical AND, Logical OR and Logical NOT operations.
Ø Logical AND
Ø Logical OR
Ø Logical NOT
|
Operator |
Description |
|
and |
The condition will also be true if the expression is true. If the two expressions a and b are the same, then a and b must both be true. |
|
or |
The condition will be true if one of the phrases is true. If a and b are the two expressions, then an or b must be true if and is true and b is false. |
|
not |
If an expression a is true, then not (a) will be false and vice versa. |
Example: Python program for Logical operators
a = 5 # initialize the value of a
print(“Is this statement true?:”, a > 3 and a < 5)
print(“Any one statement is true?:”, a > 3 or a < 5)
print(“Each statement is true then return False and vice-versa:”, (not (a > 3 and a < 5)))
Membership Operators: The membership of a value inside a Python data structure can be verified using Python membership operators. The result is true if the value is in the data structure; otherwise, it returns false.
|
Operator |
Description |
|
in |
If the first operand cannot be found in the second operand, it is evaluated to be true (list, tuple, or dictionary). |
|
not in |
If the first operand is not present in the second operand, the evaluation is true (list, tuple, or dictionary). |
Example: Python program for Membership operators
x = ["Rose", "Lotus"]
print(' Is value Present?', "Rose" in x)
print(' Is value not Present?', "Riya" not in x)
Identity Operators
|
Operator |
Description |
|
is |
If the references on both sides point to the same object, it is determined to be true. |
|
is not |
If the references on both sides do not point at the same object, it is determined to be true. |
Example: Python program for Identity operators
|
a = ["Rose", "Lotus"] b = ["Rose", "Lotus"] c = a print(a is c) print(a is not c) print(a is b) print(a is not b) print(a == b) print(a} |
Output: True False False True True False |
Precedence and Associatively of Operators in Python
The operator precedence in Python is listed in the following table. It is in descending order (upper group has higher precedence than the lower ones). Operators in the same cell under the Operators column have the same precedence.
|
Sr |
Operators |
Meaning |
Execution |
|
1 |
(),[], {} |
Parentheses and braces |
Left to right |
|
2 |
[index], [index:index] |
Subscription, slicing, |
Left to right |
|
3 |
await x |
Await expression |
Left to right |
|
4 |
** |
Exponent |
Right to left |
|
5 |
+x ,-x ,~x |
Unary plus, Unary minus, Bitwise NOT |
Right to left |
|
6 |
*, @ /, // , % |
Multiplication, matrix multiplication ,Division, Floor division, Modulus |
Left to right |
|
7 |
+ , - |
Addition, Subtraction |
Left to right |
|
8 |
<<,>> |
left shift , Right shift (Bitwise) operators |
Left to right |
|
9 |
&,^,! |
Bitwise AND , Bitwise XOR , Bitwise OR |
Left To Right |
|
10 |
= = ,!=,> ,>=,<.<=, in, not in, is, is not , |
Comparisons, Identity, Membership operators |
Left To Right |
|
11 |
Not |
Logical NOT |
Right to left |
|
and , or |
Logical AND , Logical OR |
Left to right |
|
|
12 |
If - else |
Conditional expression |
Right to left |
|
13 |
lambda |
Lambda expression |
N/A |
|
14 |
:= |
Assignment expression (walrus operator) |
Right to left |
Python Operators Precedence Rule – PEMDAS
We might have heard about the BODMAS rule in your school’s mathematics class. Python also uses a similar type of rule known as PEMDAS.
P – Parentheses
E – Exponentiation
M – Multiplication
D – Division
A – Addition
S – Subtraction
The precedence of operators is listed from High to low. To remember the abbreviations, we have a funny concept “Please Excuse My Dear Aunt Sally”. Now we apply the PEMDAS rule and evaluate the following expression – ((((6+4)*2)-10)//2)-4*2 Output: -3.0
: LET Us REVISE :
- Operators are the symbols that represent specific operations
- The ternary operator is the operator that is used to show the conditional statements
- Operators are used to perform operations on variable and values
- Logical operators are used to combine conditional statements
- Identity operators are used to compare the objects , not if they are equal , but if they are actually the same object, with the same3 memory location
- Is operator returns true if both a variables are the same objects
- The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable
- Python allows you to assign a single value to several variables simultaneously
- Bitwise operator works on bits and performs bit by bit operation
- Associativity operators are usually evaluated from right to left
- An operator which requires three operands is called Ternary operator
- Relational operators are used to assigning values for the variables
- Assignment operators are used to assigning values for the variables
- Bitwise operators are used for performing operations over the Bits
- Arithmetic operators are used with numeric values to perform common mathematical operations
- ** performs exponential(power)calculations on operators
- Calculations in python are done in a specific order, which is called operator precedence
- Identify operators (is, is not) compare the memory two objects are referencing
- Something that has a value is called atom
- Expression is valid combination of operators and atoms
- A type whose value is not changeable in place is called immutable type
- Implicitly type conversion is a automatic internal conversion of data type (lower tohigher type)by python
- A type whose value is changeable in place is called mutable type
- Forced data type conversion by the user is called explicitly type conversion