Home  >  Article  >  Backend Development  >  Is the length of a list variable in python?

Is the length of a list variable in python?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-19 17:55:2110849browse

Is the length of a list variable in python?

#Is the list length variable in python? Let me introduce to you the variable and immutable types in python:

Variable, immutable
Variable/immutable types refer to: the memory id does not change, and the type does not change. Under the premise of unchanged, whether the value is variable.
int() and str() are both immutable types
Lists and dictionaries are variable types

For variable objects, such as lists, when operating on the list, the contents inside the list will Changed, for example:

>>> a = ['c', 'b', 'a']>>> a.sort()>>> a
['a', 'b', 'c']

Related recommendations: "python video tutorial"

For immutable objects, For example, str, how to operate str:

>>> a = 'abc'>>> a.replace('a', 'A')'Abc'>> ;> a'abc'

Although the string has a replace() method and indeed 'Abc' is changed, the variable a is still 'abc' in the end. How should we understand it?

Let’s first change the code to the following:

>>> a = 'abc'>>> b = a.replace('a', 'A ')>>> b'Abc'>>> a'abc'

What you should always remember is that a is a variable, and 'abc' is a string object ! Sometimes, we often say that the content of object a is 'abc', but in fact, it means that a itself is a variable, and the content of the object it points to is 'abc':

Is the length of a list variable in python?

When we call a.replace('a', 'A'), the method replace is actually called on the string object 'abc', and although this method is named replace, it has not changed. The contents of the string 'abc'. On the contrary, the replace method creates a new string 'Abc' and returns it. If we use variable b to point to the new string, it is easy to understand. Variable a still points to the original string 'abc', but variable b points to The new string 'Abc' is:

Is the length of a list variable in python?

So, for immutable objects, calling any method of the object itself will not change the content of the object itself. Instead, these methods create a new object and return it, thus ensuring that the immutable object itself will always be immutable.

The above is the detailed content of Is the length of a list variable in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Does python support char?Next article:Does python support char?