What is Single Assignment (S.A)?
Single assignment is the process of assigning a value to a variable in a single statement or line
using the assignment operator (=).
syntax of variable:
variable_name = value
where,
variable_name is the name of the variable.
value is the value that is stored in the variable.
'=' is the assignment operator.
Example:
age = 25 #Assigning integer value 25 to the variable 'age’
name = "John" # Assigning string "John" to the variable 'name’
height = 5.9 # Assigning float value 5.9 to the variable 'height’
is_student = True # Assigning Boolean value True to the variable 'is_student’
What is Multiple Assignment (M.A.) ?
In Python, multiple assignment is the process of assigning a single value to multiple variables or
assigning multiple values to multiple variables at the same time using a single statement and
the assignment operator (=).
syntax of variable:
variable_name_1 , variable_name_2 , . . . , variable_name_n = value_1 , value_2 , . . . , value_n
where,
variable_name is the name of the variable
value is the value that is stored in the variable
‘=’ is the assignment operator
Example:
# Assigns 10 to a, 20 to b, and 30 to c
a, b, c = 10, 20, 30
#Assigns 25 to age, "John" to name, 5.9 to height, and True to is_student
age, name, height, is_student = 25, "John", 5.9, True
#Assigns "Alice" to first_name, "Smith" to last_name, and 30 to age
first_name, last_name, age = "Alice", "Smith", 30
Why we need Multiple Assignment ?
1. Less Code: You can assign values to many variables in one line, which means less
code overall.
2. Faster: It can be quicker because you write less code and the computer processes
it in one go.
3. No Redundancy: It reduces repeated lines of code, making your program cleaner.
4. Grouping Related Data:If you need to assign related data, multiple assignment
groups these variables together logically, making it easier to follow the code’s
purpose.
Example: first_name, last_name, age = "John", "Doe", 30
5. Return Multiple Values from Functions:If a function gives you more than one
result, you can assign all the results to different variables at once, without
extra steps.
Example: def function1():
return 1, 2
a, b = function1() #function1() return 1 and 2 then assign to variable a and b
Example: All are same type of data
Example 1:
a, b, c = 1, 2, 3
print(a, b, c)
Output: 1 2 3
Example 2:
a, b, c = 1.7, 2.6, 3.2
print(a, b, c)
Output: 1.7 2.6 3.2
Example 3:
a, b, c = "akash", "mca", "vbu"
print(a, b, c)
Output: akash mca vbu
Example 4:
a, b, c = True, False, True
print(a, b, c)
Output: True False True
Example: All are mixed type of data
Example 1:
a, b, c = 1, "akash", 2.3
print(a, b, c)
Output: 1 akash 2.3
Example 2:
a, b, c = True, 2, "ram"
print(a, b, c)
Output: True 2 ram
Example 3:
a, b, c, d = 1, 2.3, "Akash", False
print(a, b, c, d)
Output: 1 2.3 Akash False
Example 4:
x, y, z = 1, "akash", 4.5
a, b, c = x, y, z
print(a, b, c)
Output: 1 akash 4.5
Example 5:
a, b, c = y, z, x
print(a, b, c)
Output: akash 4.5 1
Example :Assign same value
Example 1:
a, b, c = 1, 1, 1
print(a, b, c)
Output: 1 1 1
Example 2:
a=b=c=1
print(a, b, c)
Output: 1 1 1
Example 3:
a, b, c = 1 + 2, 2 + 3, 3 + 4
print(a, b, c)
Output: 3 5 7
Example 4:
a, b, c = 2.3, 4.5, 6.9
print(a, b, c)
Output: 2.3 4.5 6.9
Example 5:
a, b, c = "a" + "b", "na" + "me", "xy" + "z"
print(a, b, c)
Output: ab name xyz
Example : Assign expression of mixed type of data
Example 1:
a, b, c = 1 + 2, 2.3 - 4.5, "aka" + "sh"
print(a, b, c)
Output: 3 -2.2 akash
Example : Assign constant/variable/expression
Example 1:
x, y = 1, 2
a, b, c = 5, y, 5 + x
print(a, b, c)
Output: 5 2 6