DEV Community

Cover image for Assignment Operators in python
Aswin Barath
Aswin Barath

Posted on • Edited on

2 1 1 1 1

Assignment Operators in python

Assignment operators are used to assigning values to variables.

That is to store values in variables we use = assignment operator.
Alt Text
Output:

3.14
Enter fullscreen mode Exit fullscreen mode

OK, now comes the real fun.
Have ever been tired to use x = x + 5, where we type the variable x twice?
There's actually a shortcut for this called Augmented assignment operators.

Augmented assignment operators can be used as a replacement as follows:

x += 3     --->    x = x + 3    
x -= 3     --->    x = x - 3    
x *= 3     --->    x = x * 3    
x /= 3     --->    x = x / 3    
x %= 3     --->    x = x % 3    
x //= 3    --->    x = x // 3   
x **= 3    --->    x = x ** 3   
x &= 3     --->    x = x & 3    
x |= 3     --->    x = x | 3    
x ^= 3     --->    x = x ^ 3    
x >>= 3    --->    x = x >> 3   
x <<= 3    --->    x = x << 3
Enter fullscreen mode Exit fullscreen mode

Here's the Code and Output

Alt Text

9
6
18
6.0
Enter fullscreen mode Exit fullscreen mode

Alt Text

64
1
0
Enter fullscreen mode Exit fullscreen mode

Alt Text

2
3
Enter fullscreen mode Exit fullscreen mode

Alt Text

0
3
24
Enter fullscreen mode Exit fullscreen mode

Quick Note: The code snippets reuses the same variable to assign with different arithmetic operations / bitwise operations / shift operations.

So, while coding makes sure you practice to use print statements after each operation.

Best Resources

Who Am I?

I’m Aswin Barath, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging during the busy time of my freelancing work life. Here’s the link to all of my socials categorized by platforms under one place: https://linktr.ee/AswinBarath

Thank you so much for reading my blog🙂.

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay