Python basic grammar (Updating ...)

Python basic grammar

2.1 Notes

C / C ++ / Java comments in

  1. Single-line comments.//
  2. Multi-line comments./*...*/

Comments in Python

  1. Single-line comments.#

    Multi-line comments. Use of three or three pairs of single quotes quotes

    Sample code:

    print('Hello')  # 这里是单行注释
    '''
    多行注释
    。。。
    。。。
    
    '''
    """
    多行注释
    ....
    ...
    ...
    """

Operators 2.2

2.2.1 arithmetic operators

Example:

a = 8
b = 3
print(a + b) # 11
print(a - b) # 5
print(a * b) # 24
print(a ** b) # 512 幂
print(a / b) # 2.6666666666666665 除 小数
print(a // b) # 2 除 求整
print(a % b) # 2 取余

2.2.2 Comparison operators

Including :( greater than),> = (greater than or equal), <(less than), <= (less than or equal to), == (equal to),! = (Not equal)

2.2.3 Assignment Operators

Comprising: a = (assignment) + = (addition assignment), - = (subtraction assignment), * = (multiplication assignment), ** = (power assignment), / = (non-divisible assignment), // = (divisible assignment),% = (modulo assignment), ............

2.2.4 Bitwise Operators

Comprising: & (and bit), | (or bits), ^ (exclusive or) ~ (negation), >> (shift right), << (shift left)

Bits and features: 0 & 0 = 0 0 & 1 = 0 1 & 0 = 0 1 & 1 = 1 x & 0 = 0 x & 1 = x

Bits or features: 0 | 0 = 0 0 | 1 = 1 1 | 0 = 1 1 | 1 = 1 x | 0 = xx | 1 = 1

XOR features: 0 ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0 x ^ 0 = xx ^ 1 = ~ x

Negated features: ~ ~ 1 0 = 0 1 =

Left features: the left one is equivalent to multiplying 2

Right features: right one equivalent of dividing by 2 (integer division)

a = 19       # 0001 0011
b = 25       # 0001 1001
print(a & b) # 0001 0001 17

a = 19       # 0001 0011
b = 25       # 0001 1001
print(a | b) # 0001 1011 27

a = 19       # 0001 0011
b = 25       # 0001 1001
print(a ^ b) # 0000 1010 10

a = 19       # 0001 0011
print(~a)    # 1110 1100 -20

a = 19       # 0001 0011
print(a << 1)# 0010 0110 38

a = 19       # 0001 0011
print(a >> 1)# 0000 1001 9

2.2.5 Logical Operators

and (logical AND), or (logical OR), not (non-logical)

Note: bit integer operator is operated, the logical operator is a Boolean operation.

True and True = True True and False = False False and True = False False and False = False

True or True = True True or False = True False or True = True False or False = False

not True = False not False = True

c = True
d = False
print(c and d) # False
print(c or d) # # True
print(not c) # False
print(not d) # True

2.2.6 member operator

in (in the sequence), not in (not in sequence)

Mainly refers to the sequence: list, Ganso, string

str1 = 'Hello'
print('e' in str1)  # True 如果'e'存在这个序列中则返回True,否则反之
print('a' in str1)  # False
print('e' not in str1)  # True 如果'e'不在这个序列中则返回True,否则反之
print('a' not in str1)  # False

2.2.7 identity operator

is (two if the same object identifier), is not (whether two different object identifiers)

a = 'hello'
b = 'hello'
print(a is b)  # True
print(a is not b)  # False
c = 'helloworld'
print(a is c)  # False
print(a is not c)  # True

ps: Why a and b are the same object? ?

As can be seen from the figure b and b points to the same content

1563884333266.png

2.2.8 ternary operator

Such as: c = a > b ? a : b

PS: Generally not recommended to write the ternary operator error if you use the ternary operator under the conditions of a very complex situation can easily occur, the others pit pit himself. Use if else can.

2.2.9 operator precedence

== == be updated ...

Guess you like

Origin www.cnblogs.com/xsh168/p/11234129.html