Monday, 24 June 2019

Assignment Operators


Assignment Operators:

a=10 # here = is assignment operator, value 10 is stored in left-hand side variable ‘a’. Now the value of variable ‘a’ is 10.

Definition: Assignment operators are used to assigning the value to the left-hand side variable.

Example:
Ex(1)10=a #Error because variable always should be left-hand side.
Ex(2)
a=10
b=20
a=b # It is valid here b means value i.e 20 of the variable b

Difference between = and ==?
a=10
b=20

=
==
a=b
a==b
i.e we are assigning the value of variable ‘b’ to left hand side variable ‘a’
We comparing two variables, which returns Boolean True/False
Print(a)
print(a==b) or print(b==a) # compares, we get result True or false
Output: 20
Output: True

a+=10 or a=a+10

Shortcut Assignment Operators:

Ex:
a=20
>>> a+=10 #a=a+10àa=20+10àa=30
>>> print(a)
30
>>> 

We can combine assignment operators with some other operator to form a compound assignment operators or shortcut assignment operators

+=
-=
*=
/=
%=
//=
**=
&=
|=
The above is the list of all possible compound operators.

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...