Python Learning note expressions and operators (i)

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators

Expressions and operators

What is an expression?

1+2*3 is an expression, where the plus and multiplication sign are called operators, 1, 2, and 3 are called operands. The result of the 1+2*3 calculation is 7, 1+2*3 = 7. We can save the result of the calculation in a variable, ret = 1-2*3. So an expression is a code or statement consisting of operands and operators that can be evaluated and placed on the right side of "=" To assign a value to a variable.


Arithmetic operators: +-*///(divisible)% (take more) * *

>>> 2+3>>> 3-2>>> 2*3>>> 5/22.5>>> 5//2 >>> 5%2>>> 2**3 assignment operator: = , += -= *= /= %= //=  **=>>> num = 2    >>> num += 1    #  equivalent to  num = num + 1>>> num -= 1    #  equivalent to  num = num - 1>>> num *= 1   #   equivalent to  num = num * 1>>> num /= 1   #  Equivalent to  num = num / 1>>> num //= 1   #  equivalent to  num = num // 1>>> num %= 1   #  equivalent to   num = num % 1>>> num **= 2   #  equivalent to  num  = num ** 2 comparison operators:>,  <,  >=,  <=,  ==,!=   true false simply say what the name implies, Comparison operators are used to do comparisons, the results of the comparison will be two, respectively, is set up and not established, when the result is  true, the result is false when not established.  true and false  are used to represent the results of comparisons. >>> a = 5>>> b = 3>>> a > b   #  checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true.  True>>> a < b  #  checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true. false>>> a <= b  #  checks if the value of the left operand is less than or equal to the value of the right-hand operand, and if so, the condition is true. false>>> a >= b  #  checks if the value of the left operand is greater than or equal to the value of the right-hand operand, and if so, the condition is true. true>>> a == b  #  checks whether the values of the two operands are equal, and if so, the condition becomes true. false>>> a != b  #  checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. The true logical operator: not , and, and  or logical operators are used for logical calculations. Like the comparison operators we used above, each time the comparison is actually a conditional judgment, it will get a value of TRUE or false accordingly. The operand of a logical operator is an expression or variable that is used to make conditional judgments. >>> a > b and  a < b  #  if the two operands are true, the result is true, otherwise the result is false. false>>> a > b or  a < b  #  If there are at least one of the two operands to true,  then the condition becomes true, otherwise false. true>>> not a > b  #  reverses the state of the operation, the operand is true, the result is false, Conversely, when the truefalse result is true, we generally call the   result   true,  logical operator to have a truth table.

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9E/E0/wKioL1mYVTrS4ircAABfD5OXSFY519.png "title=" 22. PNG "alt=" Wkiol1myvtrs4ircaabfd5oxsfy519.png "/>

Member operator: not in, in (determines if there is a letter in a word)

The member operator is used to determine whether an element is a member of another element. For example, we can tell if there is "H" in "Hello" and the result is true or False.

>>> "h" in "Hello" # here means "H" in "Hello", after judging the result is truetrue >>> "H" not in "hello" # here means "H" not "Hello" ", after judging the result is Falsefalse


Identity operator: IS and is not (explained by data type, generally used to determine the data type of the variable)

Used to determine identity.


>>> a = 123456>>> B = a>>> B is a #判断 A and B are not the same 123456true>>> C = 123456>> > C is a #判断 C and a are not the same 123456false>>> C is not a #判断 C and a are not the same 123456True


Here we first assign 123456 to a, and then A to B, which is actually a and B values are 123456, but the value of C is also 123456, why the first time A is B result is true, C and a result is false?

The reason is this: we know that the program is running in memory, the first time we assign 123456 to a, in fact, in memory opened a space, put 123456 in this space, in order to find the 123456 here, there will be a point to this space address, this address is called memory address , is 123456 of the addresses stored in memory. A actually points to the address that stores 123456 of the memory space. The b=a is executed so that B points to the same address as a. Then we execute c = 123456, where we will open up a memory space and assign a value to C for the memory address pointing to that space, so that A and B point to the same 123456, and C to the other 123456.

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/00/30/wKiom1mYVYDDOa9HAAAPRzeBF5Q744.png-wh_500x0-wm_ 3-wmp_4-s_4201343603.png "title=" 22.png "alt=" Wkiom1myvyddoa9haaaprzebf5q744.png-wh_50 "/>

Bitwise operators:

First understand a concept:

The numbers we usually use are represented in binary notation in the computer, and this binary number is called the number of machines. The number of machines is signed, in the computer with a number of the highest bit of the symbol, positive 0, negative number is 1.

For example: decimal number +7, the computer word length is 8 bits, converted to binary is 00000111. If it is-7, it is 10000111. So, here's 00000111 and 10000111 is the number of machines.

  The original code is the absolute value of the symbolic bit plus the truth, that is, the first digit represents the symbol, and the remaining bits represent the values. For example, if it is a 8-bit binary:

[+1] original = 0000 0001

[-1] original = 1000 0001

The first bit is the sign bit. Because the first bit is the sign bit, the range of values for the 8-bit binary number is:
11111111 to 01111111 i.e. 127 to 127

  The Inverse code is represented by:
The inverse of a positive number is its own
Negative number of the inverse code is on the basis of its original code, the sign bit is unchanged, the remaining bits are reversed.

[+1] = [00000001] original = [00000001] Reverse

[-1] = [10000001] original = [11111110] Reverse

  The complement is expressed in the following ways:
A positive complement is its own
The complement of a negative number is on the basis of its original code, the sign bit is unchanged, the rest of you take the counter, the last +1. (That is, on the basis of the anti-code +1)

[+1] = [00000001] original = [00000001] counter = [00000001] Complement

[-1] = [10000001] original = [11111110] counter = [11111111] Complement


We set a=234 (binary to 11101010), b=44 (binary 101100)

& Bitwise AND Operator: Two values that participate in the operation, if two corresponding bits are 1, the result of that bit is 1, otherwise 0

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/947529/201608/947529-20160818133300843-2103118596. PNG "width=" 231 "height=" "/>

| Bitwise OR operator: As long as the corresponding two binary has one for 1 o'clock, the result bit is 1.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/947529/201608/947529-20160818133337781-586452579. PNG "width=" 239 "height=" "/>

^ Bitwise XOR Operator: When two corresponding binary differences, the result is 1

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/947529/201608/947529-20160818133220843-2063872357. PNG "width=" 237 "height="/>

~ Bitwise inverse Operator: Reverse each bits of the data, which turns 1 to 0 and 0 to 1

A = 10000000 = 128

~a

result:01111111 = 127

<< left move operator: Each binary of the operand all shifts left several bits, the number of "<<" to the right to specify the number of bits moved, high drop, low 0.

A = 10110011 = 179

A << 2

result:1011001100


>> Right Move operator: Move all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move

A = 10110011 = 179

A >> 2

result:00101100 = 44


Bit operators are generally used for binary operations, generally used in the lower level, we seldom use, know on it.

Priority level

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/9E/E0/wKioL1mYVbHhK3jSAAA7qGa0LVM697.png-wh_500x0-wm_ 3-wmp_4-s_1486788878.png "title=" 22.png "alt=" Wkiol1myvbhhk3jsaaa7qga0lvm697.png-wh_50 "/>

operator so many, priority can not remember what to do? Use parentheses. By using parentheses, we can easily prioritize the distinction.

This article is from the "on_the_road" blog, make sure to keep this source http://cqtesting.blog.51cto.com/8685091/1957869

Python Learning note expressions and operators (i)

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.