What meaning of this ""hello %s you are %s years old" % x"
drsalists at gmail.com
Sun Jul 27 23:37:19 CEST 2014
On Sun, Jul 27, 2014 at 11:49 AM, fl <rxjwg98 at gmail.com> wrote:
> In Python, when should you use lists and when tuples?
> Sometimes you don't have a choice, for example if you have
> "hello %s you are %s years old" % x
> then x must be a tuple.
> But if I am the one who designs the API and gets to choose the data types, then
> what are the guidelines?
You should use a tuple when you need something immutable (readonly),
like a dictionary key. Immutable objects are good for hashing.
You should use a list when you need something mutable (read/write),
like appending over and over. Mutable objects are not good for
hashing, because their hash value could change.
More information about the Python-list