Abstract
This chapter introduces lists, a sequential container type that is mutable. Lists are the first mutable type introduced in the book; they can be regarded as modifiable counterparts of tuples. Two ways of changing a list object are introduced, and common tips and pitfalls on working with list are discussed. In addition, the concept of attributes and methods is introduced.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
The values of variables that are bound to immutable objects can change, because the assignment statement can change the binding between identifiers and objects. For example, when the statement \(x=x+1\) or \(x+\!\!=1\) is executed, the identifier x is unbound from its previous value \(o_1\) and bound to a new object \(o_2\), the value of which is \(o_1+1\). In this process, neither the object \(o_1\) nor the object \(o_2\) changes, but the value of the variable x changes. In memory, the assignment statement changes only the binding table, but not the number objects.
- 2.
Due to the caching of small numbers introduced in Chap. 2, the integers 1, 2, 3, the string ‘a’ and the object None are not copied. The figure is for illustration of a simple recursive copying mechanism.
Author information
Authors and Affiliations
Corresponding author
Rights and permissions
Copyright information
© 2015 Springer Science+Business Media Singapore
About this chapter
Cite this chapter
Zhang, Y. (2015). Lists and Mutability. In: An Introduction to Python and Computer Programming. Lecture Notes in Electrical Engineering, vol 353. Springer, Singapore. https://doi.org/10.1007/978-981-287-609-6_7
Download citation
DOI: https://doi.org/10.1007/978-981-287-609-6_7
Published:
Publisher Name: Springer, Singapore
Print ISBN: 978-981-287-608-9
Online ISBN: 978-981-287-609-6
eBook Packages: EngineeringEngineering (R0)