Different Forms of Assignment Statements in Python


Assignment statement in python is the statement used to assign the value to the specified variable. The value assigned to the variable can be of any data type supported by python programming language such as integer, string, float Boolean, list, tuple, dictionary, set etc.

Types of assignment statements

The different types of assignment statements are as follows.

  • Basic assignment statement

  • Multiple assignment statement

  • Augmented assignment statement

  • Chained assignment statement

  • Unpacking assignment statement

  • Swapping assignment statement

Let’s see about each one in detail.

Basic Assignment Statement

The most frequently and commonly used is the basic assignment statement. In this type of assignment, we will assign the value to the variable directly. Following is the syntax.

variable_name = value

Where,

  • Variable_name is the name of the variable.

  • value is the any datatype of input value to be assigned to the variable.

Example

In this example, we are assigning a value to the variable using the basic assignment statement in the static manner.

variable = "Welcome to Tutorialspoint" 
print(variable)
print(type(variable))	

Output

Welcome to Tutorialspoint
<class 'str'>

Example

In this example, we will use the dynamic inputting way to assign the value using the basic assignment statement.

variable = "Welcome to Tutorialspoint"
print(variable)
print(type(variable))	

Output

Enter the value to assign to the variable: Welcome to Tutorialspoint
Welcome to Tutorialspoint
<class 'str'>

Multiple Assignment statement

We can assign multiple values to multiple variables within a single line of code in python. Following is the syntax.

v1,v2,……,vn = val1,val2,……,valn

Where,

  • v1,v2,……,vn are the variable names.

  • val1,val2,……,valn are the values.

Example

In this example, we will assign multiple values to the multiple variables using the multiple assignment statement.

a,b,c,d = 10,"python",23.5,True
print(a,b,c,d)
print(type(a))
print(type(b))
print(type(c))
print(type(d))

Output

10 python 23.5 True
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>

Augmented assignment statement

By using the augmented assignment statement, we can combine the arithmetic or bitwise operations with the assignment. Following is the syntax.

variable += value

Where,

  • variable is the variable name.

  • value is the input value.

  • += is the assignment operator with the arithmetic operator.

Example

In this example, we will use the augmented assignment statement to assign the values to the variable.

a = 10
print("Initial variable value:",a)
a *= 10
print("Augmented assignment variable value:",a) 

Output

Initial variable value: 10
Augmented assignment variable value: 100

Chained assignment statement

By using the chained assignment statement, we can assign a single value to the multiple variables within a single line. Following is the syntax -

v1 = v2 = v3 = value

Where,

  • v1,v2,v3 are the variable names.

  • value is the value to be assigned to the variables.

Example

Here is the example to assign the single value to the multiple variables using the chain assignment statement.

a = b = c = d = "Python Programming Language"
print(a)
print(b)
print(c)
print(d)

Output

Python Programming Language
Python Programming Language
Python Programming Language
Python Programming Language

Unpacking assignment statement

We can assign the values given in a list or tuple can be assigned to multiple variables using the unpacking assignment statement. Following is the syntax -

v1,v2,v3 = [val1,val2,val3]

Where,

  • v1,v2,v3 are the variable names.

  • val1,val2,val3 are the values.

Example

In this example, we will assign the values grouped in the list to the multiple variables using the unpacking assignment statement.

a,b,c = ["Tutorialspoint",10,"python"]
print(a,b,c)
print(type(a))
print(type(b))
print(type(c))

Output

Tutorialspoint 10 python
<class 'str'>
<class 'int'>
<class 'str'>

Swapping assignment statement

In python, we can swap two values of the variables without using any temporary third variable with the help of assignment statement. Following is the syntax.

var1,var2 = var2,var1

Where,

  • var1, var2 are the variables.

Example

In the following example, we will assign the values two variables and swap the values with each other.

a = 10
b = "Python"
print("values of variables a and b before swapping:",a,b)
a,b = b,a
print("values of variables a and b after swapping:",a,b)

Output

values of variables a and b before swapping: 10 Python
values of variables a and b after swapping: Python 10

Updated on: 20-Oct-2023

485 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements