Important Notice:

Multiple Assignment

Multiple Assignment

28 views 2 min read

Multiple Assignment — 

Multiple Assignment का मतलब है — एक ही line में कई variables को अलग-अलग values एक साथ assign करना। इसमें बाईं तरफ (left side) के हर variable को दाईं तरफ (right side) की corresponding (मेल खाती हुई) value मिलती है, उसकी position (क्रम) के हिसाब से।

English: Multiple Assignment means assigning different values to multiple variables in a single line at once. Each variable on the left side gets the matching value from the right side, based on its position (order). Matching or related to something in the same order/position.

Syntax (तरीका):

var1, var2, var3 = value1, value2, value3
 
Example 1:
 सामान्य Multiple Assignment (Basic Multiple Assignment)
 
a, b, c = 10, 20, 30
print(a, b, c)

Output:

10 20 30

यहाँ:

a = 10
b = 20
c = 30

सभी values एक ही line में, अपनी-अपनी position के हिसाब से, अलग-अलग variables को मिल गईं।

Example 2:  
अलग-अलग Data Types के साथ (With Different Data Types)

Multiple Assignment में हर variable को अलग-अलग type की value भी दी जा सकती है।

English: In Multiple Assignment, each variable can also get a different data type of value.

name, age, marks = "Riya", 22, 89.5
print(name)
print(age)
print(marks)

Output:

Riya
22
89.5

यहाँ name को string, age को integer, और marks को float value मिली — तीनों अलग-अलग types हैं, फिर भी एक साथ assign हो गईं।

Example 3:
 List/Tuple से Assignment (Unpacking)

जब किसी list या tuple की values सीधे अलग-अलग variables में split (बांट) कर दी जाती हैं, तो इसे Unpacking कहते हैं।

English: When the values of a list or tuple are automatically split into separate variables, it is called Unpacking.

Unpacking (English): The process of extracting individual elements from a collection (like a list) and assigning them to separate variables.

values = [1, 2, 3]
a, b, c = values
print(a, b, c)

Output:

1 2 3

Tuple के साथ भी वैसे ही काम करता है:

point = (5, 10)
x, y = point
print(x, y)

Output:

5 10
 
Example 4: * के साथ Variable-Length Unpacking

जब variables की संख्या values से कम हो, तो * (star) चिन्ह का उपयोग करके बाकी बची हुई values को एक list के रूप में इकट्ठा (collect) किया जा सकता है।

English: When there are fewer variables than values, the * (star) symbol can be used to collect the remaining values as a list.

a, *b, c = [1, 2, 3, 4, 5]
print(a)
print(b)
print(c)

Output:

1
[2, 3, 4]
5

यहाँ:

a को पहली value (1) मिली।
c को आखिरी value (5) मिली।
बीच की सारी values b में एक list के रूप में इकट्ठी हो गईं ([2, 3, 4])।
जरूरी नियम (Important Rule)

बाईं तरफ के variables की गिनती (count) और दाईं तरफ की values की गिनती बराबर (equal) होनी चाहिए, वरना Error आएगा।

English: The number of variables on the left side and the number of values on the right side must be equal, otherwise an Error will occur.

कम values होने पर (Fewer values):

a, b, c = 1, 2

Output (Error):

ValueError: not enough values to unpack (expected 3, got 2)

ज्यादा values होने पर (More values):

a, b = 1, 2, 3

Output (Error):

ValueError: too many values to unpack (expected 2)

ValueError (English): An error that occurs in Python when the number of values doesn't match what is expected.

Related Notes