Computer Science
In Python, strings are immutable while lists are mutable. What is the difference?
Answer
In Python, strings are immutable means that individual letter assignment for strings is not allowed. For example:
name='hello'
name[0] = 'p'
The above Python code will cause an error as we are trying to assign some value to an individual letter of a string.
Lists are mutable in Python means that we can assign values to individual elements of a list. For example:
a = [1, 2, 3, 4, 5]
a[0] = 10
The above Python code will work correctly without any errors as Lists are mutable in Python.
Related Questions
A program runs to completion but gives an incorrect result. What type of error would have caused it?
Add parentheses to the following expression to make the order of evaluation more clear.
y % 4 == 0 and y % 100 != 0 or y % 400 == 0
How does the // operator differ from the / operator? Give an example of where // would be needed.
MidAir Airlines will only allow carry-on bags that are no more than 22 inches long, 14 inches wide, and 9 inches deep. Assuming that variables named length, width, and depth have already been assigned values, write an expression combining the three that evaluates to True if bag fits within those limits, and False otherwise.