What will be the output of the following program in python
A =4
B = A
A *=2
B +=3
Print(“A=”,A, “B = “, B)
Answers
Answer: A=8 B=7
Given python program:
A =4
B = A
A *=2
B +=3
print("A=",A, "B = ", B)
Explanation:
Line 1:
A=4
Equal to(=) is an assignment operator which assigns values from right side operands to left side operand.
Here, we are assigning value 4 to the variable 'A.'
Line 2:
B=A
Equal to(=) is an assignment operator which assigns values from right side operands to left side operand.
Here, we are assigning B=A which means we are assigning value 4 to the variable 'B.'
Line 3:
A*=2
*= is known as Multiply And. It multiplies right operand to the left operand and assign the result to left operand.
A*=2 is same as A=A*2.
Line 4:
+= is known as Add And. It adds right operand to the left operand and assign the result to left operand.
B+=3 is same as B=B+3
Line 5:
The print( ) function prints the given object to the standard output device (screen).
Parameters of print:
print(objects, 'separator')
Here, object is A and B and "A" and "B" are separators.
Python will interpret the program like:
A =4
B = 4
A =4*2 =8
B =4+3=7
print("A=",A, "B = ", B)
Output:
A=8 B=7