Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Python Assignment Operators

by suresh

The Python Assignment Operators are handy to assign the values to the declared variables. Equals (=) operator is the most commonly used assignment operator in Python. For example:

i = 10

The list of  available assignment operators in Python language.

Python Assignment OperatorsExampleExplanation
=x = 25Value 25 is assigned to x
+=x += 25This is same as x = x + 25
-=x -= 25Same as x = x – 25
*=x *= 25This is same as x = x * 25
/=x /= 25Same as x = x / 25
%=x %= 25This is identical to x = x % 25
//=x //= 25Same as x = x // 25
**=x **= 25This is same as x = x ** 25
&=x &= 25This is same as x = x & 25
|=x |= 25This is same as x = x | 25
^=x ^= 25Same as x = x ^ 25
<<=x <<= 25This is same as x = x << 25
>>=x >>= 25Same as x = x >> 25

Python Assignment Operators Example

For this example, We are using four variables a, Total, x, y and their values are 7, 21, 9 and 65. Next, we use them to show the working functionality of all the Python Assignment Operators.

# Program to show all Python Assignment Operators */

>>> a = 7
>>> Total = 21
>>> Total += a # Using += Operator
>>> print("The Value of the Total after using += Operator is: ", Total)
>>> Total -= a # Using -= Operator
>>> print("The Value of the Total after using -= Operator is: ", Total)
>>> Total *= a # Using *= Operator
>>> print("The Value of the Total after using *= Operator is: ", Total)
>>> Total //= a # Using //= Operator
>>> print("The Value of the Total after using //= Operator is: ", Total)
>>> Total **= a # Using **= Operator
>>> print("The Value of the Total after using **= Operator is: ", Total)
>>> Total /= a # Using /= Operator
>>> print("The Value of the Total after using /= Operator is: ", Total)
>>> Total %= a # Using %= Operator
>>> print("The Value of the Total after using %= Operator is: ", Total)
>>> x = 9
>>> y = 65
>>> x &= y # Using &= Operator
>>> print("The Value of the x after using &= Operator is: ", x)
>>> x |= 9 # Using |= Operator
>>> print("The Value of the x after using |= Operator is: ", x)
>>> x ^= y # Using ^= Operator
>>> print("The Value of the x after using ^= Operator is: ", x)
Python Assignment Operators

In this Python Assignment Operators example program, We declared 2 integer values a, Total and we assigned values 7 and 21 respectively.

 a = 7
 Total = 21

The print statements will display the output of the Total after using Python Assignment Operators on a and Total. Let us see the functionalities of all the Python Assignment Operators

First assignment operator functionality,

 Total += a # Using += Operator
 print("The Value of the Total after using += Operator is: ", Total)

Total += a means

Total = Total + a ⇒ 21 + 7 = 28

So, the output of the above mentioned Python print statement will be 28

The functionality of a Second assignment operator in Python,

 Total -= a # Using -= Operator
 print("The Value of the Total after using -= Operator is: ", Total)

Total -= a means

Total = Total – a ⇒ 28 – 7 = 21

So, the output will be 21

Third assignment operator functionality,

 Total *= a # Using *= Operator
 print("The Value of the Total after using *= Operator is: ", Total)

Total *= a means

Total = Total * a ⇒ 21 * 7 = 147

So, the output will be 147

Fourth assignment operator in Python functionality,

 Total //= a # Using //= Operator
 print("The Value of the Total after using //= Operator is: ", Total)

Total //= a means

Total = Total // a ⇒ 147 // 7 = 21

So, the output of the above mentioned print statement will be 21

Fifth assignment operator functionality,

 Total **= a # Using **= Operator
 print("The Value of the Total after using **= Operator is: ", Total)

Total **= a means

Total = Total ** a ⇒ 21*21*21 *21*21*21*21 = 1,801,088,541

So, the output of the above mentioned print statement will be 1,801,088,541

Sixth assignment operator functionality,

Total /= a # Using /= Operator
>>> print("The Value of the Total after using /= Operator is: ", Total)

Total /= a means

Total = Total / a ⇒ 1,801,088,541 / 7 = 257,298,363

So, the output of Total /= a will be 257,298,363

The functionality of a Seventh assignment operator in python is

 Total %= a # Using %= Operator
 print("The Value of the Total after using %= Operator is: ", Total)

Total %= a means

Total = Total % a ⇒257,298,363 % 7 = 0 (because Remainder of 257,298,363 /7 is = 0)

In the next line, We declared 2 integer values x, y and we assigned values 9 and 65 respectively.

 x = 9
 y = 65

Eighth assignment operator functionality,

 x &= y # Using &= Operator
 print("The Value of the x after using &= Operator is: ", x)

x &= y means

x = x & y ⇒ 9 & 65

⇒ 00001001 & 01000001 = 00000001 ⇒ 1

So, the output of x &= y is 1. Please refer Python Bitwise Operators

Ninth assignment operator functionality,

 x |= 9 # Using |= Operator
 print("The Value of the x after using |= Operator is: ", x)

x |= y means

x = x | y ⇒ 1 | 9

⇒ 00000001 | 00001001 = 00001001 ⇒ 9

So, the output will be 9

The functionality of a Tenth assignment operator,

x ^= y # Using ^= Operator
>>> print("The Value of the x after using ^= Operator is: ", x)

x ^= y means

x = x ^ y ⇒ 9 ^65

⇒ 00001001 ^ 01000001 = 01001000 ⇒ 72

So, the output will be 72.

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy