Things to consider in Python

Source: Internet
Author: User

1. And the expression is evaluated from left to right in Python, and if all values are true, the last value is returned, and if False, the first false value is returned;

The or is also from left to has a calculated expression, returning the first true value;

Where the number 0 is false, others are true;

The characters "" are false, others are true;

2. The numbers in Python are represented in the following way:

2 binary is starting with 0b: For example: 0b11 means 3 decimal

8 binary is starting with 0o: For example: 0o11 means 9 decimal

The 0x is the beginning of the system: for example: 0x11 means the decimal

But there was a problem with the test that the output was automatically converted to decimal:

1 >>> a=0b1111002 >>> a3 60

Use Bin,oct,hex to output the binary, octal, hexadecimal form of the digits, for example:

1 >>> a=0b1111002 >>> a=603 >>> bin (a)4  '0b111100'5 >>> Oct (a)6'  0o74'7 >>> Hex (a)8'0x3c' 

3. Is and = = difference:

is used to determine whether the two variables refer to the same object, = = is used to determine whether the value of the reference variable is equal.

IS and! = differ from the above, one compares the reference object and the other compares the value of the two.

4. Just started to learn python, when want to self-increment operation is natural a++, the results found that the compiler is not know + +, so went to search the Internet, the results found a foreigner's question and answer is very exciting, involving the design of the language of Python the principle.

The problem is that Python does not have a self-increment operator and how the increment operation is implemented.

The answer is about the self-increment operation, Python does not use + + Philosophy logic: The compilation of concise and concise language itself, it is not specifically translated.

There's a foreigner back there. An example is very wonderful, pointing out some of the differences between Python and C language, the language description may not be accurate, directly on the example:

>>> B = 5  >>> a = 5  >>> ID (a)  162334512  >>> ID (b)   162334512 is    b  True  

As you can see, in Python, variables are based on content rather than variable names in C, so as long as your digital content is 5, regardless of your name, the ID of the variable is the same, and it also shows that a variable in Python can be accessed with multiple names.

This design logic determines that the value of the numeric type in Python is immutable, because if, as in the above example, both A and B are 5, when you change A, B will also change, which is certainly not what we want.

Therefore, the correct self-increment operation should be a = a + 1 or a + = 1, when this a self-increment, through the ID () observed that the ID value changed, that a is already the name of the new value.

What the students upstairs say is not a problem in a scripted programming environment. However, in an interactive environment, the compiler will have a small integer pool concept, the number of (-5,256) will be pre-created, and when A and B over this range, two variables will point to different objects, so the address will be not the same, such as the following example:

>>> a=1000>>> b=1000>>> ID (a); ID (b)22366123662242236617350384>>>

Things to consider in Python

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.