Internship Assurance

DevOps & Cloud Engineering

Ever felt confused about assigning values to variables in Python? Wondering how to simplify operations like addition, subtraction, or even bitwise shifts?

Many of us face these questions when diving into Python programming.

Assignment operators in Python are here to make our lives easier. These operators help us assign values to variables in a clean and efficient way and make our code efficient and compact.

We’ll explore how these operators work and how we can use them to streamline our code.

Let’s start with the basics.

The equal sign (=) is the simple assignment operator in Python. It assigns the value on its right to the variable on its left.

**Example:**

In this example:

- We assign the values 10 to a and 5 to b.
- We add a and b and assign the result to c.
- We print the value of c, which is 15.

Now, let’s move on to augmented assignment operators.

These operators combine an operation with an assignment in one step. They make our code more concise and easier to read.

How It Works: This operator would thus add to the right operand and assign the result to the left operand.

**Example:**

Here, a is initially 10.

The ‘+=’ operator adds 5 to a and assigns the result back to a. So, a becomes 15.

How It Works: It subtracts the right operand from the left operand and assigns the result to the left operand.

**Example:**

Initially, a is 10.

The ‘-=’ operator subtracts 5 from a and assigns the result back to a. Thus, a becomes 5.

How It Works: This is the operator for multiplication of the left operand by the right operand, with the result being assigned to the left operand.

**Example:**

Initially, a is 10.

The ‘*=’ operator multiplies a by 5 and assigns the result back to a. Hence, a becomes 50.

How It Works: It divides the left operand by the right and assigns the outcome to the left operand.

**Example:**

Initially, a is 10.

The ‘/=’ operator divides a by 5 and assigns the result back to a. Thus, a becomes 2.0.

How It Works: This operator divides the left operand by the right operand and assigns the remainder to the left operand.

**Example:**

Initially, a is 10.

The ‘%=’ operator divides a by 3 and assigns the remainder back to a. Therefore, a becomes 1.

How It Works: It performs floor division of the left operand by the right operand and, therefore, modifies the value of the left operand.

**Example:**

Initially, a is 10.

The ‘//=’ operator divides a by 3 and assigns the floor value back to a. So, a becomes 3.

How It Works: The operator raises the left operand to the power of the right one and puts the result back into the left operand.

**Example:**

Initially, a is 2.

The ‘**=’ operator raises a to the power of 3 and assigns the result back to a. Therefore, a becomes 8.

How It Works: It performs a bitwise AND operation on the operands and assigns the result to the left operand.

**Example:**

Initially, a is 5 (0101 in binary).

The ‘&=’ operator performs a bitwise AND with 3 (0011 in binary) and assigns the result back to a. Thus, a becomes 1 (0001 in binary).

How It Works: This operator performs a bitwise OR operation on the operands and assigns the result to the left operand.

**Example:**

Initially, a is 5 (0101 in binary).

The ‘|=’ operator performs a bitwise OR with 3 (0011 in binary) and assigns the result back to a. So, a becomes 7 (0111 in binary).

How It Works: It performs a bitwise XOR operation on the operands and assigns the result to the left operand.

**Example:**

Initially, a is 5 (0101 in binary).

The ‘^=’ operator performs a bitwise XOR with 3 (0011 in binary) and assigns the result back to a. Thus, a becomes 6 (0110 in binary).

How It Works: This operator performs a bitwise right shift on the left operand and assigns the result to the left operand.

**Example:**

Initially, a is 8 (1000 in binary).

The ‘>>=’ operator right shifts a by 2 and assigns the result back to a. So, a becomes 2 (0010 in binary).

How It Works: It performs a bitwise left shift on the left operand and assigns the result to the left operand.

**Example:**

Initially, a is 3 (0011 in binary).

The ‘<<=’ operator left shifts a by 2 and assigns the result back to a. Therefore, a becomes 12 (1100 in binary).

Internship Assurance

DevOps & Cloud Engineering

Ever wondered if there’s a way to simplify assignments within expressions?

Meet the Walrus Operator.

The Walrus Operator (:=) allows us to assign values to variables as part of an expression. It makes our code more concise and readable.

Imagine you’re iterating over a list and want to process elements until the list becomes short. The Walrus Operator lets us do this efficiently.

**Example:**

**Output:**

- n is assigned the length of a within the while loop condition.
- The loop runs until a has two or fewer elements.
- We reduce our code’s length without sacrificing clarity.

Let’s dive into more practical examples of assignment operators in Python.

These examples will help us see how assignment operators make our code cleaner and more efficient.

**Output:**

- We use the += operator to accumulate the sum of numbers from 0 to 4.

**Output:**

- We use the -= operator to update the balance after each withdrawal.

**Output:**

**Output:**

**Output:**

**Output:**

**Output:**

**Output:**

**Output:**

**Output:**

**Output:**

**Output:**

Mastering assignment operators in Python can significantly improve our coding skills. These operators help us write cleaner, more efficient code. In this comprehensive guide, we explored assignment operators in Python, from the basic operator to the more advanced augmented operators and the Walrus Operator.

Whether we’re using simple assignments or the Walrus Operator, understanding these tools is essential. As we practice and apply these operators, our Python programming becomes more intuitive and effective.

FAQs

Can the Walrus Operator be used in all versions of Python?

No, the Walrus Operator (:=) was introduced in Python 3.8 and is not available in earlier versions.

What is the difference between ‘=’ and ‘==’ in Python?

- = operator is used for value assignment.
- == does a comparison to check if the two values are equal.

What will happen if we use augmented assignment operators with different data types?

Python will implicitly type cast if needed. For instance, if a float is added to an integer using ‘+=’, it will become a float.

How does the += operator differ from + in Python?

- += operator adds the right operand to the left operand and assigns the result to the left operand.
- + simply adds the operands without assignment.

Why is the Walrus Operator useful?

The Walrus Operator allows assignment within expressions, which can make code more concise and readable, especially within loops and comprehensions.

Book a free counselling session

Get a personalized career roadmap

Get tailored program recommendations

Explore industry trends and job opportunities

Programs tailored for your success

Popular

Data Science

Technology

Finance

Management

Future Tech

Upskill with expert articles

View all

Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved