Liaoche Python Course

Source: Internet
Author: User

To define a tuple of only 1 elements, if you define this:

>>> t = (1)>>> t1

The definition is not a tuple, it is 1 this number! This is because the parentheses () can represent both tuple and parentheses in the mathematical equation, which creates ambiguity, so Python rules that, in this case, the parentheses are calculated, and the result is naturally 1 .

Therefore, only 1 elements of a tuple definition must be added with a comma , to disambiguate:

>>> t = (1,)>>> t(1,)

Python will also add a comma when displaying a tuple of only 1 elements, , lest you misunderstand the parentheses in the mathematical sense.

ifStatement execution has a characteristic, it is to judge from the top, if in a certain judgment True , the statement corresponding to the execution of the sentence, will ignore the remaining elif andelse。

continueThe effect is to end the cycle ahead of time and start the next cycle directly.

breakThe effect is to end the cycle prematurely.

breakThe statement can exit the loop directly during the loop, and the continue statement can end the cycle in advance and start the next cycle directly. These two statements usually have to be used in conjunction with the if statement.

pay special attention to not abusing break and continue statements. breakand continue will cause the code to perform logical fork too much, error prone. Most loops do not need to use break and continue statements, the above two examples, can be overridden by the loop condition or modify the loop logic, remove break and continue statement.

Sometimes, if the code is written with a problem, it will cause the program to fall into a "dead loop", that is, forever loop. You can then either Ctrl+C exit the program or force the end of the Python process.

Compared with list, Dict has the following features:

    1. The speed of finding and inserting is very fast and will not slow with the increase of key;
    2. It takes a lot of memory, and it wastes a lot of memory.

And the list is the opposite:

    1. The time to find and insert increases as the element increases;
    2. Small footprint and little wasted memory.

So, Dict is a way of exchanging space for time.

Dict can be used in many places where high-speed lookups are needed, almost everywhere in Python code, it is important to use dict correctly, and the first thing to keep in mind is that the Dict key must be an immutable object .

This is because Dict calculates the storage location of value based on key, and if each calculation of the same key results in a different result, the dict interior is completely chaotic. The algorithm for calculating the position by key is called the hash Algorithm (hash).

To ensure the correctness of the hash, the object as a key can not be changed. In Python, strings, integers, and so on are immutable, so you can safely use them as keys. The list is mutable and cannot be a key.

STR is an immutable object, and list is a mutable object.

For mutable objects, such as list, to manipulate the list, the contents of the list will change, such as:

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

For non-mutable objects, such as STR, we operate on STR:

>>> a = ‘abc‘>>> a.replace(‘a‘, ‘A‘)‘Abc‘>>> a‘abc‘

Although the string has a replace() method, it does change ‘Abc‘ , but the variable is a still at the end ‘abc‘ , how should it be understood?

Let's start by changing the code to the following:

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

Always keep in mind that a it is a variable, but a ‘abc‘ string Object! Sometimes, we often say that the object's a content is ‘abc‘ , but actually refers to, a itself is a variable, it points to the content of the object is ‘abc‘ :

When we call a.replace(‘a‘, ‘A‘) , the actual invocation method replace is on the string object ‘abc‘ , and although the method is called, it replace does not change ‘abc‘ the contents of the string. Instead, the replace method creates a new string ‘Abc‘ and returns, and if we point to the new string with a variable, b it's easy to understand that the variable a still points to the original string ‘abc‘ , but the variable points to the b new string ‘Abc‘ :

Therefore, for an immutable object, any method that invokes the object itself does not change the contents of the object itself. Instead, these methods create a new object and return it, ensuring that the immutable object itself is always immutable.

Liaoche Python Course

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.