Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In the previous lessons, we learned how to create variables and display their values.
Normally, we assign variables one at a time:
But Python gives us a convenient way to assign multiple variables at the same time.
For example:
Python can also help us:
*These techniques are called multiple assignment and unpacking.
Let's start with a simple example.
Instead of writing:
you can write:
Both approaches create the same three variables:
This is called multiple assignment.
Multiple assignment is useful when you have related values.
For example:
Output:
You can also use an f-string:
Output:
The values are assigned from left to right:
When using normal unpacking, the number of variables should match the number of values.
For example:
This works because there are:
But this does not work:
There are:
Python raises:
The opposite also causes an error:
There are:
Python raises a ValueError because there are not enough values.
For normal unpacking:
Sometimes you want several variables to start with the same value.
For example:
Now:
You can print them:
Output:
This is called chained assignment.
Chained assignment is generally straightforward with immutable values such as numbers, strings, and booleans.
For example:
This is perfectly fine.
Another example:
Both names refer to the same string value.
Lists are mutable, which means their contents can be changed.
Consider:
Now both names refer to the same list object.
If we change the list through list_a:
then:
Output:
Why?
Because both variables refer to the same list.
After adding "Apple":
If you want two independent lists:
Now they are separate objects.
You don't need to be afraid of chained assignment.
Just remember:
Suppose:
We want:
One common approach is to use a temporary variable:
This works.
But Python provides a much simpler way.
In Python, you can write:
That's it!
Before:
After:
Complete example:
Output:
a, b = b, a is a simple and common Python way to swap two values.a, b = b, a Work?You don't need to understand the internal details yet, but the basic idea is useful.
Python evaluates the right side first:
Suppose:
The right side gives:
Python then assigns those values to:
So:
This is why the swap works without needing a temporary variable.
Unpacking means taking values from a sequence and assigning them to multiple variables.
For example:
Output:
The list contains three values:
Python assigns them to:
This is called unpacking.
A tuple is another Python collection.
For example:
Output:
Although the lesson topic often calls this tuple unpacking, the same unpacking idea also works with lists and other iterable objects.
For example:
This also works.
Unpacking is not limited to tuples.
You can commonly unpack:
The values don't have to be the same type.
For example:
Here:
Python simply assigns each value to the corresponding variable.
*Sometimes you don't know exactly how many values should go into the middle.
Python provides the * operator for this situation.
For example:
Now:
Let's print them:
Output:
* do?The starred variable collects the remaining values.
*Consider:
Now:
You can also collect everything except the last value:
Now:
The starred variable receives a list.
For example:
gives:
Multiple assignment can also be useful when updating related values.
For example:
Output:
Why?
Python first evaluates the right side using the old values:
Then assigns:
This technique is commonly used in algorithms such as generating Fibonacci numbers.
The Fibonacci sequence begins:
We can generate it using multiple assignment:
Output:
The important line is:
Python calculates the complete right side first and then assigns the results.
❌
There are 2 variables but 3 values.
Python raises a ValueError.
❌
There are 3 variables but only 2 values.
Again, Python raises a ValueError.
* Doesmiddle is:
not:
The starred variable collects the values into a list.
Be careful with:
Both names refer to the same list.
If you need independent lists:
| Practice | ✅ Do | ❌ Don't |
|---|---|---|
| Multiple assignment | x, y = 10, 20 | Write many unrelated assignments on one line |
| Swapping | a, b = b, a | Use a temporary variable unnecessarily |
| Unpacking | name, age = student | Ignore the number of values |
| Starred unpacking | first, *middle, last = data | Forget that middle becomes a list |
| Lists | Create separate lists when needed | Accidentally share a mutable list |
| Readability | Group related values | Put many unrelated variables on one line |
Result:
For normal unpacking:
unless you use a starred variable to collect remaining values.
x and y?middle?Create a file named:
Then write:
Try these exercises yourself.
Create:
Print all three values.
Start with:
Swap them using only:
Given:
Store the values in:
Given:
Create:
where middle contains all values between the first and last.
What will this print?
Don't run it immediately.
First calculate the answer yourself, then run the program and check your prediction.
In the next lesson, we will continue your Python learning journey with Constants (Python Convention) (2: Variables).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Output Variables & Printing Techniques | Constants (Python Convention) |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.