28.10.2021 Views

Python Tutorial ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Line 5 - a is greater than b

Line 6 - a is either less than or equal to b

Line 7 - b is either greater than or equal to b

Python Assignment Operators:

Assume variable a holds 10 and variable b holds 20, then:

Operator Description Example

=

+=

-=

Simple assignment operator, Assigns values from right side operands to left

side operand

Add AND assignment operator, It adds right operand to the left operand and

assigns the result to left operand

Subtract AND assignment operator, It subtracts right operand from the left

operand and assigns the result to left operand

c = a + b will assign

value of a + b into c

c += a is equivalent to

c = c + a

c -= a is equivalent to

c = c - a

*=

Multiply AND assignment operator, It multiplies right operand with the left

operand and assigns the result to left operand

c *= a is equivalent to

c = c * a

/=

Divide AND assignment operator, It divides left operand with the right

operand and assigns the result to left operand

c /= a is equivalent to

c = c / a

%=

Modulus AND assignment operator, It takes modulus using two operands

and assigns the result to left operand

c %= a is equivalent

to c = c % a

**=

Exponent AND assignment operator, Performs exponential (power)

calculation on operators and assigns value to the left operand

c **= a is equivalent

to c = c ** a

//=

Floor Dividion and assigns a value, Performs floor division on operators and

assigns value to the left operand

c //= a is equivalent to

c = c // a

Example:

Try following example to understand all the assignment operators available in Python programming language:

#!/usr/bin/python

a = 21

b = 10

c = 0

c = a + b

print "Line 1 - Value of c is ", c

c += a

print "Line 2 - Value of c is ", c

TUTORIALS POINT

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!