Python’s Assignment Operators

Today we’re going to take a closer look into the world of assignments in Python. You might think this is boring stuff, but trust me, it’s not! Assignments are a fundamental part of any programming language, and they can be pretty ***** fun if you know how to use them correctly.

So let’s get started with the basics.In Python, an assignment statement looks like this:

# Assigning the value 42 to the variable x
x = 42

This line creates a new variable called `x`, and assigns it the value of 42. Pretty simple, right? But what if you want to do something more complex? Maybe you have two variables that need to be updated at once:

# This line creates a new variable called `a`, and assigns it the value of 10.
a = 10

# This line creates a new variable called `b`, and assigns it the value of 5.
b = 5

# This line creates a new variable called `c`, and assigns it the sum of `a` and `b`.
c = a + b

# This line creates a new variable called `d`, and assigns it the product of `c` and 3.
d = c * 3

# This line creates a new variable called `e`, and assigns it the value of `d` divided by 2.
e = d / 2

# This line creates a new variable called `f`, and assigns it the value of `e` divided by 4.
f = e / 4

# This line creates a new variable called `g`, and assigns it the value of `f` raised to the power of 2.
g = f ** 2

# This line creates a new variable called `h`, and assigns it the value of `g` floor divided by 7.
h = g // 7

# This line creates a new variable called `i`, and assigns it the remainder of `h` divided by 9.
i = h % 9

# This line creates a new variable called `j`, and assigns it the bitwise XOR operation between `i` and 6.
j = i ^ 6

# This line creates a new variable called `k`, and assigns it the value of `j` minus 1.
k = j - 1

# This line creates a new variable called `l`, and assigns it the value of `k` plus 5.
l = k + 5

# This line creates a new variable called `m`, and assigns it the bitwise AND operation between `l` and 0xFFFF.
m = l & 0xFFFF

# This line creates a new variable called `n`, and assigns it the bitwise OR operation between `m` and 0x8000.
n = m | 0x8000

# This line creates a new variable called `o`, and assigns it the value of `n` divided by 32768.
o = n / 32768

# This line creates a new variable called `p`, and assigns it the value of `o` plus 4096.
p = o + 4096

# This line creates a new variable called `q`, and assigns it the value of `p` multiplied by 2.5.
q = p * 2.5

# This line creates a new variable called `r`, and assigns it the value of `q` divided by 10.
r = q / 10

# This line creates a new variable called `s`, and assigns it the value of `r` raised to the power of 3.
s = r ** 3

# This line creates a new variable called `t`, and assigns it the value of `s` floor divided by 100.
t = s // 100

# This line creates a new variable called `u`, and assigns it the remainder of `t` divided by 10.
u = t % 10

# This line creates a new variable called `v`, and assigns it the value of `u` multiplied by 2.
v = u * 2

# This line creates a new variable called `w`, and assigns it the value of `v` plus 7.
w = v + 7

# This line creates a new variable called `x`, and assigns it the value of `w` plus 42.
x = w + 42

# This line creates a new variable called `y`, and assigns it the value of `x` multiplied by 9.
y = x * 9

# This line creates a new variable called `z`, and assigns it the value of `y` floor divided by 5.
z = y // 5

Wow, that’s a lot of assignments! But don’t worry, Python has some cool tricks up its sleeve to make this process easier. Let’s talk about augmented assignment operators. These are the same as regular assignment operators, but with an added twist: they also perform an operation on the left-hand side variable before assigning it a new value.

For example, let’s say you have two variables `a` and `b`, and you want to add 10 to both of them:

# This script demonstrates the use of augmented assignment operators in Python.

# First, we declare two variables, 'a' and 'b', and assign them initial values of 5 and 7 respectively.
a = 5
b = 7

# Next, we use the augmented assignment operator '+=' to add 10 to the value of 'a' and assign the result back to 'a'.
a += 10 # This is equivalent to 'a = a + 10'
# Now, the value of 'a' has been updated to 15.

# Similarly, we use the augmented assignment operator '+=' to add 10 to the value of 'b' and assign the result back to 'b'.
b += 10 # This is equivalent to 'b = b + 10'
# Now, the value of 'b' has been updated to 17.

# Finally, we print the values of 'a' and 'b' to confirm the updates.
print(a) # Output: 15
print(b) # Output: 17

This is much easier than writing `a = a + 10` and `b = b + 10`. And it’s also more concise! Python has many other augmented assignment operators, including -= (subtract), *= (multiply), /= (divide), //= (integer division), **= (exponentiation), |= (bitwise OR), &= (bitwise AND), = (right shift with zero fill), = (left shift with sign extension), and %= (modulo).

Assignments in Python can be simple or complex, but they’re always fun. And if you use augmented assignment operators, you can save yourself some time and keystrokes.

SICORPS