In Place Operators

*Quick Post

An in place operator is basically a method of shortening some operators. For example rather than writing out the operation for “x = x + 3” you can just write the operator “x += 3” – both are the same and both will produce the same result.

The same kind of operation can be achieved for subtraction (-), multiplication (*), division (/), and remainder (%).

[Console]

>>> x = 2

>>> print(x)

>>> print += 3

=====

[Interpreter ]

2

5

In addition to numerical operations, in place operators can also be used on strings, for example:

[Console]

>>> x = “Hello ”

>> print(x)

>>> x += “World”

>>> print(x)

=====

[Interpreter]

Hello

Hello World

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s