1. Synatx of Assignment Statement:
The symbol '=' is used in an assignment statement is called as an assignment operator.
Note: The Assignment Operator assign value of right hand operand(value) to the left hand side operand and this operator should never be used for Equality purpose which is doubhle equal sign ‘==’.
The Basic Syntax of Assignment Statement in the Python programming language is :
variable = expression
where,
variable = variable name or value
expression = it could be either a direct value or a math expression/formula or a function call
Example –
a = 50
b = 34.25
a=25
In the above-given examples, Variable ‘a’ is assigned a value i.e 50 and variable 'b' is assigned float value 34.25, Variable ‘a’ is reassigned the value 25.
Assignment statements in Python
In this article, Lets start learning assignment statements in Python.
Assignment statement is vey common in programming and used frequently.
An Assignment statement is a statement that is used to set a value to the variable name in a program.
During life time of program, assignment statement allows variable to hold different types of values.
It stores the value at memory location which can be identified or refered by variable name.
we will learn the follwing topics in this article.
1. Syntax
2. Assignment Statement Forms
2.1 Basic Form
2.2 Tuple Assignment
2.3 Sequence Assignment
2.4 Multiple-target Assignment or Chain Assignment
2.5 Augmented Assignment
2.Assignment Statement Forms
2.1 Basic Form: This is one of the most common forms of Assignment Statements. Here the Variable name is defined, initialized, and assigned a value in the same statement. This form is generally used when we want to use the Variable quite a few times and we do not want to change its value very frequently.
e.g.
language="python"
print("Language =",language)
Output.
Language=python.
2.2 Tuple Assignment: In python program or script if we need to define and assign values for more than 1 variable a the same time Tuple assignment is useful.
This is one of the easy method and saves time in python. Here we need to keep in mind that every individual variable has a different value assigned to it.
e.g.
language1,language2="python","java"
a,b=30,40
print("Language1 =",language1)
print("Language2 =",language2)
print("a=",a)
print("b=",b)
Output.
Language1=python.
Language2=java.
a=30
b=40
Explanation:
When we declare a tuple,the values on the left side of the =, Python pairs objects on the right side with targets on the left by position and assigns them from left to right.
Therefore, the values of language1 and language2 are "python" and "java" respectively.
similarly a=30 and b=40
2.3 List Assignment
This works in the same way as the tuple assignment.
[v1, v2] = [12, 14]
print('v1 = ', v1)
print('v2 = ', v2)
OUTPUT
v1 = 12
v2 = 14
2.4 Sequence assignment:
In recent version of Python, tuple and list assignment have been generalized into instances of what we now call sequence assignment.
Python assigns items in the sequence on the right to variables in the sequence on the left by position.Sequence assignment assign values from left to right and assigns the values one at a time.
# python script for sequence assignment
a, b, c,d,e,f = 'Python'
print('a = ', a)
print('b = ', b)
print('c = ', c)
print('d = ', c)
print('e = ', c)
print('f = ', c)
OUTPUT:
a = P
b = y
c = t
e=h
e=o
f=n
2.5 Extended Sequence unpacking:
It allows us to be more flexible in how we select portions of a sequence to assign.If we use the star operator(*), it’s possible to have different numbers of elements on each side of the assignment operator. The starred variable will be assigned a list with all the leftovers that were not assigned to the other variables. There may be only one starred variable:
p, *q = 'Python Programming'
print('p = ', p)
print('q = ', q)
Here, p is matched with the first character in the string on the right and q with the rest. The starred name (*q) is assigned a list, which collects all items in the sequence not assigned to other names.
OUTPUT:
p = P
q = ['y', 't', 'h', 'o','n',' ','P','r','o','g','r','a','m','m','i','n',g']
This is especially handy for a common coding pattern such as splitting a sequence and accessing its front and rest part.
2.6 Multiple- target assignment:
x = y = 75
print(x, y)
In this form, Python assigns a reference to the same object (the object which is rightmost) to all the target on the left.
OUTPUT
75 75
2.7 Augmented assignment :
The augmented assignment is a shorthand assignment that combines an expression and an assignment.
x = 2
# equivalent to: x = x + 1
x += 1
print(x)
OUTPUT
3
There are several other augmented assignment forms:
-=, **=, &=, etc.
Previous Topic:-->> Constants in Python || Next topic:-->>Input/Output in Python