Computer Science
How is a mutable object different from an immutable object in Python ?
Identify one mutable object and one immutable object from the following:
(1, 2), [1, 2], {1:1, 2:2}, '123'
Python Funda
2 Likes
Answer
In Python, a mutable object can be changed after it is created (e.g., lists, dictionaries), while an immutable object cannot be modified after its creation (e.g., tuples, strings).
An example of a mutable object from the given options is [1, 2] (list) and {1:1, 2:2} (dictionary). [Write any one]
An example of an immutable object from the options is '123' (string) and (1, 2) (tuple). [Write any one]
Answered By
1 Like
Related Questions
Assertion (A): Positional arguments in Python functions must be passed in the exact order in which they are defined in the function signature.
Reasoning (R): This is because Python functions automatically assign default values to positional arguments.
- Both A and R are true, and R is the correct explanation of A.
- Both A and R are true, and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Assertion (A): A SELECT command in SQL can have both WHERE and HAVING clauses.
Reasoning (R): WHERE and HAVING clauses are used to check conditions, therefore, these can be used interchangeably.
- Both A and R are true, and R is the correct explanation of A.
- Both A and R are true, and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Give two examples of each of the following:
- Arithmetic operators
- Relational operators
If L1 = [1, 2, 3, 2, 1, 2, 4, 2, …], and L2 = [10, 20, 30, …], then
(Answer using builtin functions only)
(a) Write a statement to count the occurrences of 4 in L1.
OR
(b) Write a statement to sort the elements of list L1 in ascending order.(a) Write a statement to insert all the elements of L2 at the end of L1.
OR
(b) Write a statement to reverse the elements of list L2.