Important Notice:

Assignment Operators

Assignment Operators

26 views 3 min read

Assignment Operators (असाइनमेंट ऑपरेटर) :-

Assignment Operators (असाइनमेंट ऑपरेटर) वे Operators हैं जिनका उपयोग किसी value या expression का result किसी variable में assign (निर्धारित/संग्रहित) करने के लिए किया जाता है।

Python में Assignment Operators का उपयोग variables को value देने तथा किसी variable की value को update (बदलने) करने के लिए किया जाता है।

English

Assignment Operators are used to assign a value or the result of an expression to a variable.

They are also used to update the value of a variable using arithmetic operations such as addition, subtraction, multiplication, division, etc.

1. Assignment Operator (=) :-

The Assignment Operator (=) is used to assign a value to a variable.

Assignment Operator (=) का उपयोग किसी variable में value store/assign करने के लिए किया जाता है।

Syntax -

variable = value
 
Example -
a = 20
print(a)
 
Output-
20
  2. Add and Assign Operator (+=) :-

The Add and Assign Operator (+=) adds a value to the existing value of a variable and assigns the result back to the same variable.

Add and Assign Operator (+=) variable की existing value में नई value को जोड़ता है और result को उसी variable में store करता है।

Syntax -
variable += value
or
variable = variable + value
 
Example -
a = 20
a += 5
print(a)
 
Output-
25
 
 
3. Subtract and Assign Operator (-=) :-

The Subtract and Assign Operator (-=) subtracts a value from the existing value of a variable and assigns the result back to the same variable.

Subtract and Assign Operator (-=) variable की existing value में से नई value को घटाता है और result को उसी variable में store करता है।

Syntax -
variable -= value
or
variable = variable - value
 
Example -
a = 20
a -= 5
print(a)
 
Output-
15
 
4. Multiply and Assign Operator (*=) :-

The Multiply and Assign Operator (*=) multiplies the existing value of a variable by a given value and assigns the result back to the same variable.

Multiply and Assign Operator (*=) variable की existing value को दी गई value से multiply करता है और result को उसी variable में store करता है।

Syntax -
variable *= value
or
variable = variable *value
 
Example -
a = 20
a *= 5
print(a)
 
Output-
100
 
5. Divide and Assign Operator (/=) :-

The Divide and Assign Operator (/=) divides the existing value of a variable by a given value and assigns the result back to the same variable.

Divide and Assign Operator (/=) variable की existing value को दी गई value से divide करता है और result को उसी variable में store करता है।

Syntax -
variable /= value
or
variable = variable /value
 
Example -
a = 20
a /= 5
print(a)
 
Output-
4.0
Note: Python में / का result सामान्यतः float होता है।
 
6. Modulus and Assign Operator (%=) :-

The Modulus and Assign Operator (%=) calculates the remainder and assigns it back to the variable.

Modulus and Assign Operator (%=) भाग करने के बाद प्राप्त remainder (शेषफल) को variable में store करता है।

Syntax -
variable %= value
or
variable = variable %value
 
Example -
a = 17
a %= 5
print(a)
 
Output-
2
 
7. Floor Division and Assign Operator (//=) :-

The Floor Division and Assign Operator (//=) performs floor division and assigns the result back to the variable.

Floor Division and Assign Operator (//=) floor division करता है और प्राप्त result को उसी variable में store करता है।

Syntax -
variable //= value
or
variable = variable //value
 
Example -
a = 17
a //= 5
print(a)
 
Output-
3
 
8. Exponentiation and Assign Operator (**=) :-

The Exponentiation and Assign Operator (**=) raises the variable to the given power and assigns the result back to the variable.

Exponentiation and Assign Operator (**=) variable की value पर power/घात लगाता है और result को उसी variable में store करता है।

 

Syntax -
variable **= value
or
variable = variable **value
 
Example -
a = 2
a **= 3
print(a)
 
Output-
8
 
Assignment Operators Using User Input in One Program :-
 
a = int(input("Enter a number: "))

print("\nOriginal Value :", a)

a += 5
print("After += 5     :", a)

a -= 3
print("After -= 3     :", a)

a *= 2
print("After *= 2     :", a)

a /= 4
print("After /= 4     :", a)

a %= 3
print("After %= 3     :", a)

a //= 2
print("After //= 2    :", a)

a **= 2
print("After **= 2    :", a)
 

 Output -

Enter a number: 20

Original Value : 20
After += 5     : 25
After -= 3     : 22
After *= 2     : 44
After /= 4     : 11.0
After %= 3     : 2.0
After //= 2    : 1.0
After **= 2    : 1.0

Related Notes