×

### Use Assignment Operators in Python - Complete guide ? Operators are special symbols in a programming language that are used in between operands to perform logical and mathematical operations. The value on which the operator conducts the computation is known as the operand. Arithmetic, logical, relational, assignment, and bitwise are just a few of the operators available.

Here at LinuxAPT, we shall look into the Python programming language's assignment operators.

#### More about Assignment Operators in Python

As the name suggest, you can assign any value to any variable using an assignment operator. This operator represents by the equality (=) sign whereas you need to write any variable on the left side which is also known as operand and on the right side you need to mention any number.

A basic syntax of representing the assignment operator is given below:

`a = Any number`

Here a is the variable or the operand and on the right side you can assign any constant number that will be stored in it for example,

`a = 5`

#### Different Assignment Operators in Python with examples

1. Add and Assignment Operator (+=)

Here we are not only assigning the value, but we are also going to add them as well and the result will be stored in the variable a on the left side.

It's Syntax is given below:

`a += b`

For example,

`a = 3b = 5 #a = a + b  a += bprint(a)`

Output:

`8`

2. Subtract and Assignment Operator (-=)

Here we are also going to subtract other than just assigning the values.

The result will be stored in variable an on the left side as shown below.

It's Syntax is given below:

`a -= b`

Example:

`a = 5 b = 3#a = a - b   a -= b  print(a)`

Output:

`2`

3. Multiply and Assignment Operator (*=)

Here, we are to use the multiplication operator as well as the assignment operator. The result will be stored in the variable an on the left side.

It's Syntax is given below:

`a *= b`

Example:

`a = 5 b = 3 #a = a * b   a *= bprint(a)`

Output:

`15`

4. Divide and Assignment Operator (/=)

Here we are also going to use the division operator along with the assignment operator. The result will be stored in the variable a on the left side.

It's Syntax is given below:

`a /= b`

Example:

`a = 3 b = 5#a = a / b   a /= bprint(a)`

Output:

`0.6`

5. Modulus and Assignment Operator (%=)

Modulus operator will show you the remainder when you divide two numbers, and their result will be stored in the variable.

It's Syntax is given below:

` a %= b`

Example:

`a = 3 b = 5#a = a % b   a %= bprint(a)`

Output:

`3`

6. Exponent and Assignment Operator (**=)

This operator is used to calculate the exponential value assigned to any number and its result will be stored on the left side in the variable a.

It's Syntax is given below:

`a **= b`

Example:

`a = 3 b = 5#a = a ** b   a **= bprint(a)`

Output:

`243`

7. Bitwise And (&) and Assignment Operator (&=)

This operator will first convert the numbers in their binary form and then performed the And (&) operator to display the result back in decimal form.

It's Syntax is given below:

`a &= b`

Example:

`a = 3 (0011) b = 5 (0101)#a = a(0011) & b(0101)   a &= b = 0001 = 1print(a)`

Output:

`1`

8. Bitwise OR and Assignment Operator (|=)

This operator will first convert the numbers in their binary form and then performed the OR (|) operator to display the result back in decimal form.

It's Syntax is given below:

`a |= b`

Example:

`a = 3 (0011) b = 5 (0101)#a = a (0011) | b (0101)  a |= b = 0111 = 7print(a)`

Output:

`7`

9. Bitwise XOR and Assignment Operator (^=)

This operator is used to perform Bitwise XOR on the operands and then assigning result to the left operand.

It's Syntax is given below:

`a ^= b`

Example:

`a = 3 (0011) b = 5 (0101)#a = a ^ b (0011) ^ (0101)   a ^= bprint(a) = (0110)`

Output:

`6`

10. Bitwise Right Shift and Assign Operator (>>=)

This operator is used to perform Bitwise right shift on the assigned values given to the variables.

It's Syntax is given below:

`a >>= b`

Example:

`a = 3 b = 5#a = a >> b   a >>= bprint(a)`

Output:

`0`

11. Bitwise Left Shift and Assign operator (<<=)

This operator is used to perform Bitwise left shift on the assigned values given to the variables.

It's Syntax is given below:

` a <<= b`

Example:

`a = 3 b = 5#a = a << b   a <<= bprint(a)`

Output:

`96`

#### Conclusion

This article covers some of the most useful assignment operators in Python. In fact, Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, bitwise computations. The value the operator operates on is known as Operand.

#### Search by typing a one letter Word Term Here

• #### Install VidCutter on Linux Mint 21 "Vanessa" - Step by step guide

• #### Install TeXworks on Ubuntu 22.10 (Kinetic Kudu) - Step by step process

• #### DU Command in Linux - More about it ?

• #### Create a Network Bridge on CentOS 8 - Step by step guide ?

• #### What is CHOWN Command in Linux ?

• #### Check Ports in Use on Ubuntu 22.04 (Jammy Jellyfish)

• #### Install ZSH on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### How to use Ansible Loops ?

• #### Ansible 'When' Conditionals - More about it ?

• #### AWK Features Explained with Examples

• #### Bash "If String Equals" Comparison - Complete guide ?

• #### 6 Best Text Editors for Linux

• #### Disable the Network Interface on Boot in CentOS 8 - Step by step guide ?

• #### DMIDECODE Command in Linux - How to use it ?

• #### Compare Numbers in a Bash Script - How to do it ?

• #### Linux LSOF Command - Explained with Examples

• #### LVEXTEND Command in Linux - Explained with Examples ?

• #### PASSWD Command in Linux - Explained with Examples

• #### Replace a String in a File Using Ansible

• #### Rsync Command Options - Explained with Examples

• #### Execute Remote Command with SSH

• #### Linux sed Command Options - Explained with examples ?

• #### How Tags in Ansible Playbook works running Ubuntu 22.04 (Jammy Jellyfish) ?

• #### TAR Command Options in Linux - Explained with Examples

• #### Ansible "Debug" Module - Explained with Examples with Ubuntu

• #### ulimit Linux Command - Explained with Examples

• #### Working with Variables in Ansible running Ubuntu 22.04 (Jammy Jellyfish)

• #### Fix Inactive UFW Status on Ubuntu 22.04 (Jammy Jellyfish)

• #### Run Background Process in Ubuntu 22.04 (Jammy Jellyfish) Linux system

• #### UFW Allow Port on Ubuntu 22.04 (Jammy Jellyfish)

• #### UFW Remove Rule in Ubuntu 22.04 (Jammy Jellyfish) Linux system

• #### Python Compare Strings - How it works ?

• #### Which Flavour of Ubuntu Should You Choose ?

• #### Install pageedit editor on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### Install Webfs Server on Jammy Jellyfish Ubuntu 22.04 system

• #### Install kakoune Editor on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### Install Luakit browser on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### Install Gnome Edit on Ubuntu Jammy Jellyfish ( 22.04 ) - Step by step guide ?

• #### Install Pluma on Debian 11.4 (Bullseye) System - Step by step guide ?

• 