Assignment operators in Python

Python Assignment operators

Assignment operators are used to assign values to variables. In Python Arithmetic as well as Bitwise operators can be combined with assignment operator, for updating variables.


Makes it easier for users to update and assign the values to variables.


'=' is the basic assignment operator.


OperatorOperation
=Is equal to(assign)
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign
%=Modulus and assign
**=Exponentiate and assign
//=Floor Division and assign
&=Bitwise AND and assign
|=Bitwise OR and assign
^=Bitwise XOR and assign
<<=Zero fill LEFT SHIFT and assign
>>=Zero fill RIGHT SHIFT and assign


Assignment operators makes the things easier and shorter.


Assignment operator

An assignment operator is 'is equal to' operator which we use in mathematics, for assigning values.


Assignment operators are basically used in initialization of variables.


Program


>>> a = 5
>>> print(a)
5
>>> b = 5.3
>>> print(b)
5.3
>>> st = "Hello world!"
>>> print(st)
Hello world!



First and foremost, the variables are initialized and then they are updated by using the following assignment operators.


Basic syntax:-
variable_name operator(+=) value


Python Add and assign operator

Once variables are initialized they can be updated by adding it with a value, and then it gets assign to the updated value. This can be done by using add and assign operator.


First the variable gets updated and then it is assign with the updated value.


Program


>>> a = 6
>>> a += 5
>>> a = 6
>>> print(a)
6
>>> a += 5
>>> print(a)
11
>>> st = "Hello "
>>> print(st)
Hello 
>>> st += "world!"
>>> print(st)
Hello world!



Python Subtract and assign operator

Once variables are initialized they can be updated by subtracting it with some value, and then the variable gets assigned with the updated value. This can be done by using Subtract and assign operator.

Program


>>> a = 9
>>> print(a)
9
>>> a -= 3
>>> print(a)
6




Python Multiply and assign operator

Once variables are initialized they can be updated by multiplying it with a value, and then it gets assign with the variable. This can be done by using multiply and assign operator.


Program


>>> mul = 5
>>> print(mul)
5
>>> mul *= 5
>>> print(mul)
25
>>> ss = "a"
>>> print(ss)
a
>>> ss *= 5
>>> print(ss)
aaaaa



Python Divide and assign operator

Once variables are initialized they can be updated by dividing it with a value, and then the updated value gets assign with the variable. This can be done by using divide and assign operator.


Program


>>> a = 6
>>> print(a)
6
>>> a /= 3
>>> print(a)
2.0


Python Modulus and assign operator

Getting the modulus and then assigning it with the variable. This can be done by using modulus and assign operator.

Program


>>> num = 5
>>> print(num)
5
>>> num %= 9
>>> print(num)
5
>>> num = 9
>>> print(num)
9
>>> num %= 3
>>> print(num)
0
>>> num2 = 9
>>> print(num2)
9
>>> num2 %= 2
>>> print(num2)
1


Python Exponentiate and assign operator

Exponentiation operation is done and then the result is assigned to the variable. This can be done by using exponentiate and assign operator.


Program


>>> a = 2
>>> print(a)
2
>>> a **= 3
>>> print(a)
8



Python Floor division and assign operator

Firstly the Floor division is performed and then the updated value is assigned to the variable. This can be done by using Floor division and assign operator.


Program


>>> a = 10
>>> a //= 3
>>> print(a)
3
>>> 10//3
3



Python Bitwise AND and assign operator

Performing Bitwise AND operation and then assigning the updated value to the variable. This can be done by using Bitwise AND and assign operator.


Program


>>> a = 3
>>> print(a)
3
>>> a &= 1
>>> print(a)
1
>>> 3 & 1
1
>>> b = 2
>>> b &= 1
>>> print(b)
0
>>> num = 3
>>> num &= 2
>>> print(num)
2
>>> num2 = 7
>>> num2 &= 3
>>> print(num2)
3


Python Bitwise OR and assign operator

OR operation is performed first and then the updated value is assigned to the variable. This can be done by using Bitwise OR and assign operator.


Program


>>> num1 = 3
>>> print(num1)
3
>>> num1 |= 1
>>> print(num1)
3
>>> 3 | 1
3
>>> num1 |= 3
>>> print(num1)
3
>>> num2 = 2
>>> num2 |= 1
>>> print(num2)
3
>>> num1 |= 2
>>> print(num1)
3
>>> num3 = 4
>>> num3 |= num1
>>> print(num3)
7


Python Bitwise XOR and assign operator

XOR operation is performed first and then the updated value is assigned to the variable. This can be done by using Bitwise XOR and assign operator.


Program


>>> num1 = 3
>>> print(num1)
3
>>> num1 ^= 1
>>> print(num1)
2
>>> num2 = 3
>>> num2 ^= num2
>>> print(num2)
0
>>> num1 ^= 1
>>> print(num1)
3
>>> 2 ^ 1
3
>>> num1 ^= 2
>>> print(num1)
1
>>> num3 = 7
>>> num3 ^= 3
>>> print(num3)
4


Python Zero fill LEFT SHIFT and assign operator

Bitwise 0 fill LEFT SHIFT operation is done first and then the updated value is assigned to the variable. This can be done by using Bitwise zero fill LEFT SHIFT and assign operator.


Program


>>> num = 1
>>> print(num)
1
>>> num <<= num
>>> print(num)
2
>>> 1 << 1
2
>>> num1 = 1
>>> num1 <<= 3
>>> print(num1)
8
>>> num2 = 3
>>> num2 <<= 1
>>> print(num2)
6



Python Zero fill RIGHT SHIFT and assign operator

Bitwise 0 fill RIGHT SHIFT operation is done first and then the updated value is assigned to the variable. This can be done by using Bitwise zero fill RIGHT SHIFT and assign operator.


Program

>>> num1 = 3
>>> print(num1)
3
>>> num1 >>= 1
>>> print(num1)
1
>>> 3>>1
1
>>> num2 = 3
>>> num2 >>= num2
>>> print(num2)
0
>>> num3 = 7
>>> num3 >>= num1
>>> print(num3)
3



Conclusion

Assignment operators are used to assign values to variables.

In Python assignment operators can be combined with arithmetic and bitwise operators, for updating and assigning values to a variable.





Previous                          Next

If you have any query, feel free to ask.

إرسال تعليق

If you have any query, feel free to ask.

Post a Comment (0)