Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Assignment Operators

Python

python logo

In this tutorial we will learn about Assignment operators in Python.

We use the assignment operator to assign any value or result of an expression to a variable.

In the following Python code we are assigning integer value 10 to the variable x.

x = 10

In the following Python code we are assigning the result of an expression to a variable

y = 10 + 20

Shorthand Assignment Operators

Lets say, we have an integer variable x which is initially set to 10. Then we increase the value by 5 and assign the new value to it.

# declare and set value
x = 10

# increase the value by 5 and re-assign
x = x + 5

Another way of writing the code x = x + 5 is by using the shorthand assignment += as shown below.

# declare and set value
x = 10

# increase the value by 5 and re-assign
x += 5

Following is the list of shorthand assignment operators.

Simple assignmentShorthand assignment
x = x + yx += y
x = x - yx -= y
x = x * yx *= y
x = x / yx /= y
x = x % yx %= y
x = x ** yx **= y
x = x // yx //= y

Example

Write Python program to add the value of two variables x and y having value 10 and 20 respectively and print the sum.

# variable
x = 10
y = 20

print("Result:", (x + y))

We can achieve the same result by adding the value of y to x and saving the result in x.

# variable
x = 10
y = 20

# result
x += y

print("Result:", x)

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT