
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to assign multiple values to a same variable in Python?
In Python, if you try to do something like
a = b = c = [0,3,5] a[0] = 10
You'll end up with the same values in
a, b, and c: [10, 3, 5]
This is because all three variables here point to the same value. If you modify this value, you'll get the change reflected in all names, ie, a,b and c. To create a new object and assign it, you can use the copy module.
example
a = [0,3,5] import copy b = copy.deepcopy(a) a[0] = 5 print(a) print(b)
Output
This will give the output −
[5,3,5] [0,3,5]
- Related Articles
- How to assign multiple values to same variable in C#?\n
- Assign multiple variables with a Python list values
- How to assign values to variables in Python
- Assign multiple variables to the same value in JavaScript?
- How to assign same value to multiple variables in single statement in C#?
- How do I assign a dictionary value to a variable in Python?
- Can we assign a reference to a variable in Python?
- How to assign a PHP variable to JavaScript?
- How to assign a reference to a variable in C#
- Assign other value to a variable from two possible values in C++
- How do we assign values to variables in Python?
- How can we assign a function to a variable in JavaScript?
- How to assign int value to char variable in Java
- How to assign values to variables in C#?
- How do we assign values to variables in a list using a loop in Python?

Advertisements