Hướng dẫn compound assignment operators python


    This operator performs two operations in a sequence, such as -
  1. First, an add operation.
  2. Next, the assignment of the result of an add operation.

Understanding the += operator with a code -

i=2;        	#initializing i to 2
i+=2;		#equals to, i = i+2;
  • Statement i+=2 is equal to i=i+2, hence 2 will be added to the value of i, which gives us 4.
  • Finally, the result of addition, 4 is assigned back to i, updating its original value from 2 to 4.
  • A special case scenario for all the compound assigned operators


i=2;
i+=2*2;		#equals to, i = i+[2*2];

In all the compound assignment operators, the expression on the right side of = is always calculated first and then the compound assignment operator will start its functioning. Hence in the last code, statement i+=2*2; is equal to i=i+[2*2], which results in i=i+4, and finally it returns 6 to i.

Example with += operator


# Example with += compound assignment operator in Python


str ="Hello";
print["Original string value : ", str]
str+="World!"
print["Updated string value : ", str]


i=10
print["Original integer value : ", i]
i+=10 		# i = i+10 
print["Updated integer value : ", i]


f=10.555
print["Original float value : ", f]
f+=20		# f = f+20
print["Updated float value : ", f]
Output is
Original string value :  Hello
Updated string value :  HelloWorld!
Original integer value :  10
Updated integer value :  20
Original float value :  10.555
Updated float value :  30.555

Advertisement

  • -= operator

    1. This operator performs two operations in sequence -
    2. Subtraction operation.
    3. Assignment of the result of a subtract operation.

    Understanding the -= operator with a code -

    i=2;
    i-=2;
    
    • Statement i-=2 is equal to i=i-2, hence 2 will be subtracted from the value of i, which gives us 0.
    • Finally, the result of subtraction i.e. 0 is assigned back to i, updating its value to 0.
    • Example with -= operator


    # Example with -= compound assignment operator in Python
    
    
    i=10
    print["Original integer value : ", i]
    i-=10 		# i = i-10 
    print["Updated integer value : ", i]
    
    
    
    f=10.555
    print["Original float value : ", f]
    f-=20		# f = f-20
    print["Updated float value : ", f]
    
    
    g=99.99
    print["Original float value : ", g]
    g-=19.99		# g = g-19.99
    print["Updated float value : ", g]
    
    Output is
    Original integer value :  10
    Updated integer value :  0
    Original float value :  10.555
    Updated float value :  -9.445
    Original float value :  99.99
    Updated float value :  80.0

  • *= operator

    1. This operator performs two operations in sequence -
    2. Multiplication operation.
    3. Assignment of the result of a multiplication operation.

    Understanding the *= operator with a code -

    i=2;       #initializing i to 2
    i*=2;
    
    • Statement i*=2 is equal to i=i*2, hence 2 will be multiplied with the value of i, which gives us 4.
    • Finally, the result of multiplication, 4 is assigned back to i, updating its value to 4.
    • Example with *= operator


    # Example with *= operator in C++
    
    
    s=10
    print["Original value : ", s]
    s*=5+10		# s = s*[5+10]
    print["Value after *=5+10 operation : ", s]
    
    
    i=10
    print["Original int : ", i]
    i*=10.5		# i = i*10.5; 
    print["Value after *=10 operation : ", i]
    
    
    l=100
    print["Original int : ", l]
    l*=1000 	# l = l*1000; 
    print["Value after *=1000 operation : ", l]
    
    f=10.5
    print["Original float : ", f]
    f*=20 		# f = f*20; 
    print["Value after *=20 operation : ", f]
    

    Output -


    Original value :  10
    Value after *=5+10 operation :  150
    Original int :  10
    Value after *=10 operation :  105.0
    Original int :  100
    Value after *=1000 operation :  100000
    Original float :  10.5
    Value after *=20 operation :  210.0

  • /= operator

    1. This operator performs two operations in sequence -
    2. floating-point division operation.
    3. Assignment of the result of floating-point division operation.

    Understanding the /= operator with a code -

    i=4;     #initializing i to 4
    i/=2;
    
    • Statement i/=2 is equal to i=i/2, hence 4 will be divided by the value of i, which gives us 2.0
    • Finally, the result of division i.e. 2.0 is assigned back to i, updating its value from 4 to 2.0.
    • Example with /= operator


    # Example with //= operator in Python
    
    
    s=3;
    print[ "Original value : ", s]
    s//=5-2; 	# s = s//[5-2]
    print[ "Value after //=5-2 operation : ", s]
    
    
    i=10;
    print[ "Original value : ", i]
    i//=10; 		# i = i//10; 
    print[ "Value after //=10 operation : ", i]
    
    
    l=999;
    print[ "Original value : ", l]
    l//=2; 		# l = l//2; 
    print[ "Value after //=2 operation : ", l]
    
    
    f=80.5;
    print["Original value : ", f]
    f//=20; 		# f = f//20; 
    print["Value after //=20 operation : ", f]
    
    
    d=44.5;
    print["Original value : ", d]
    d//=22; 		# d = d//22;
    print["Value after //=22 operation : ", d]
    

    Output-


    Original value :  10
    Value after /=5-2 operation :  3.3333333333333335
    Original value :  10
    Value after /=10 operation :  1.0
    Original value :  999
    Value after /=2 operation :  499.5
    Original value :  80.5
    Value after /=20 operation :  4.025
    Original value :  44.5
    Value after /=22 operation :  2.022727272727273

  • //= operator

    1. This operator performs two operations in sequence -
    2. Integer division operation, which gives us an integer quotient value after dividing two integers and it gives a floating-point quotient after dividing a floating-point number with an integer value or vice versa.
    3. Assignment of the result of an integer division operation.

    Understanding the //= operator with a code -

    i=4;     #initializing i to 4
    i//=2;
    
    • Statement i/=2 is equal to i=i//2, hence 4 will be divided by the value of i, which gives us 2.
    • Finally, the result of division i.e. 2 is assigned back to i, updating its value from 4 to 2.
    • Example with //= operator


    # Example with //= operator in C++
    
    
    s=3;
    print[ "Original value : ", s]
    s//=5-2; 	# s = s//[5-2]
    print[ "Value after //=5-2 operation : ", s]
    
    
    i=10;
    print[ "Original value : ", i]
    i//=10; 		# i = i//10; 
    print[ "Value after //=10 operation : ", i]
    
    
    l=999;
    print[ "Original value : ", l]
    l//=2; 		# l = l//2; 
    print[ "Value after //=2 operation : ", l]
    
    
    f=80.5;
    print["Original value : ", f]
    f//=20; 		# f = f//20; 
    print["Value after //=20 operation : ", f]
    
    
    d=44.5;
    print["Original value : ", d]
    d//=22; 		# d = d//22;
    print["Value after //=22 operation : ", d]
    

    Output-


    Original value :  3
    Value after //=5-2 operation :  1
    Original value :  10
    Value after //=10 operation :  1
    Original value :  999
    Value after //=2 operation :  499
    Original value :  80.5
    Value after //=20 operation :  4.0
    Original value :  44.5
    Value after //=22 operation :  2.0

    Please share this article -

    Advertisement

    Chủ Đề