Assign Multiple Variables in a Single Line
To assign multiple variables with a single value in a statement in Python, use the following syntax.
</>
Copy
variable_1 = variable_2 = variable_3 = value
We can assign any number of variables with a single value.
Examples
In the following program, we take variables for months and assign values of 31 for months with 31 days, 30 for months with 30 days, and 28 for feb. We assign all the months with 31 days in a single statement. Similarly for months with 30 days.
Example.py
</>
Copy
jan = mar = may = jul = aug = oct = dec = 31
apr = jun = sep = nov = 30
feb = 28
total = jan + feb + mar + apr + may + jun + jul + aug + sep + oct + nov + dec
print(total)
Output
365
In the following example, we initialize variables a
and b
with 5
and find their product.
Example.py
</>
Copy
a = b = 5
product = a * b
print(product)
Output
25
Conclusion
In this Python Tutorial, we learned how to assign two or more variables with a single value in a single statement in Python, with examples.