Assignment operators are used to assigning values to variables.
That is to store values in variables we use =
assignment operator.
Output:
3.14
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
Here's the Code and Output
9
6
18
6.0
64
1
0
2
3
0
3
24
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
Python Blog Series : A Blog series where I will be learning and sharing my knowledge on each of the above topics.
Learn Python for Free, Get Hired, and (maybe) Change the World! : A detailed roadmap blog by Jayson Lennon (a Senior Software Engineer) with links to free resources.
Zero To Mastery Course - Complete Python Developer : A comprehensive course by Andrei Neagoie (a Senior Developer) that covers all of the above topics.
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🙂.
Top comments (0)