Assignment Operators in Python

 


In Python, assignment operators are used to assign values to variables. They allow you to assign a value to a variable, as well as perform operations and assign the result back to the same variable.

Assignment Operators:

  • = – The simple assignment operator assigns a value to a variable.

    x = 5

  • print(x)  # Output: 5


  • += – Adds the value on the right to the variable and assigns the result back to the variable.

    x = 10

  • x += 5  # Equivalent to x = x + 5

  • print(x)  # Output: 15


  • -= – Subtracts the value on the right from the variable and assigns the result back to the variable.

    x = 10

  • x -= 3  # Equivalent to x = x - 3

  • print(x)  # Output: 7


  • *= – Multiplies the variable by the value on the right and assigns the result back to the variable.

    x = 5

  • x *= 4  # Equivalent to x = x * 4

  • print(x)  # Output: 20


  • /= – Divides the variable by the value on the right and assigns the result back to the variable (returns a float).

    x = 10

  • x /= 2  # Equivalent to x = x / 2

  • print(x)  # Output: 5.0


  • //= – Performs floor division on the variable with the value on the right and assigns the result back to the variable.

    x = 10

  • x //= 3  # Equivalent to x = x // 3

  • print(x)  # Output: 3


  • %= – Takes the modulus (remainder) of the variable and the value on the right and assigns the result back to the variable.

    x = 10

  • x %= 3  # Equivalent to x = x % 3

  • print(x)  # Output: 1


  • **= – Performs exponentiation on the variable with the value on the right and assigns the result back to the variable.

    x = 2

  • x **= 3  # Equivalent to x = x ** 3

  • print(x)  # Output: 8


  • &= – Performs a bitwise AND operation on the variable with the value on the right and assigns the result back to the variable.

    x = 5  # 0101 in binary

  • x &= 3  # 0011 in binary

  • print(x)  # Output: 1 (0001 in binary)


  • |= – Performs a bitwise OR operation on the variable with the value on the right and assigns the result back to the variable.

    x = 5  # 0101 in binary

  • x |= 3  # 0011 in binary

  • print(x)  # Output: 7 (0111 in binary)


  • ^= – Performs a bitwise XOR operation on the variable with the value on the right and assigns the result back to the variable.

    x = 5  # 0101 in binary

  • x ^= 3  # 0011 in binary

  • print(x)  # Output: 6 (0110 in binary)


  • <<= – Performs a left shift operation on the variable with the value on the right and assigns the result back to the variable.

    x = 5  # 0101 in binary

  • x <<= 1  # Left shift by 1

  • print(x)  # Output: 10 (1010 in binary)


  • >>= – Performs a right shift operation on the variable with the value on the right and assigns the result back to the variable.

    x = 5  # 0101 in binary

  • x >>= 1  # Right shift by 1

  • print(x)  # Output: 2 (0010 in binary)


Examples:

Example 1: Using += and other assignment operators

  • x = 10

  • x += 5   # x = x + 5

  • x -= 2   # x = x - 2

  • x *= 3   # x = x * 3

  • x /= 2   # x = x / 2 (result will be a float)

  • x %= 4   # x = x % 4 (remainder)

  • x //= 2  # x = x // 2 (floor division)

  • x **= 2  # x = x ** 2 (exponentiation)


  • print(x)  # Output: 9.0


Example 2: Using bitwise assignment operators

  • x = 5  # 0101 in binary

  • y = 3  # 0011 in binary


  • x &= y  # Bitwise AND

  • print(x)  # Output: 1 (0001 in binary)


  • x |= y  # Bitwise OR

  • print(x)  # Output: 3 (0011 in binary)


  • x ^= y  # Bitwise XOR

  • print(x)  # Output: 0 (0000 in binary)


  • x <<= 2  # Left shift by 2

  • print(x)  # Output: 0 (0000 in binary)


  • x >>= 1  # Right shift by 1

  • print(x)  # Output: 0 (0000 in binary)


Key Points:

  • Assignment operators simplify common tasks by combining operations with assignment.

  • The operators like +=, -=, *=, /=, etc., update the variable with the result of the operation, reducing the need for separate steps.

  • Bitwise operators (&=, |=, ^=, etc.) are used for manipulating data at the bit level, often used in low-level programming.

Let me know if you need more details or examples! 😊


Post a Comment

0 Comments