python basics 2 - operators

3. Operators

3.1 Arithmetic operators

  • Arithmetic operator is a type of operator
  • It is a symbol used to complete basic arithmetic operations and is used to process four arithmetic operations

operator

describe

example

+

add

10 + 20 = 30

-

reduce

10 - 20 = -10

*

take

10 * 20 = 200

/

remove

10 / 20 = 0.5

//

Divide

Returns the integer part (quotient) of the division 9 // 2 outputs the result 4

%

take the remainder

Returns the remainder of division 9 % 2 = 1

**

power

Also known as power, power, 2 ** 3 = 8

  • In Python, the * operator can also be used for strings, and the result of the calculation is the result of repeating the string a specified number of times

In [1]: "-" * 50
Out[1]: '----------------------------------------'

Arithmetic operator precedence

  • First multiply and divide and then add and subtract . The same level operators are calculated from left to right. You can use () to adjust the priority of the calculation

operator

describe

priority

**

power (highest priority)

high

* / % //

Multiply, Divide, Remainder, Divide

middle

+ -

addition, subtraction

Low

3.2 Comparison (relational) operators

operator

describe

==

Checks whether the values ​​of the two operands are equal , if so, the condition is true and returns True

!=

Checks if the values ​​of the two operands are not equal , if so, the condition is true and returns True

Check if the value of the left operand is greater than the right operand, if so, the condition is true, return True

Check if the value of the left operand is less than the right operand, if so, the condition is true and return True

>=

Check if the value of the left operand is greater than or equal to the value of the right operand, if so, the condition is true, return True

<=

Check if the value of the left operand is less than or equal to the value of the right operand, if so, the condition is true, return True

In Python 2.x , you can also use the <> operator to judge not equal to

!= can also be used to judge not

3.3 Logical Operators

operator

logical expression

describe

and

x and y

Returns True only if both x and y are True, otherwise returns False as long as either x or y has a value of False

or

x or y

Returns True as long as either x or y has a value of True, and returns False only if both x and y are False

not

not x

Returns False if x is True and returns True if x is False

3.4 Assignment operator

  • In Python, use = to assign values ​​to variables
  • In arithmetic operations, in order to simplify the writing of code, Python also provides a series of assignment operators corresponding to arithmetic operators
  • Note: Spaces cannot be used between assignment operators

operator

describe

example

=

simple assignment operator

c = a + b assigns the result of the operation of a + b to c

+=

addition assignment operator

c += a is equivalent to c = c + a

-=

Subtraction assignment operator

c -= a is equivalent to c = c - a

*=

multiplication assignment operator

c *= a is equivalent to c = c * a

/=

division assignment operator

c /= a is equivalent to c = c / a

//=

Integer division assignment operator

c //= a is equivalent to c = c // a

%=

modulo (remainder) assignment operator

c %= a is equivalent to c = c % a

**=

exponentiation assignment operator

c = a is equivalent to c = c  a

3.5 Precedence of operators

  • Arithmetic precedence in the following table is in order from highest to lowest

operator

describe

**

power (highest priority)

* / % //

Multiply, Divide, Remainder, Divide

+ -

addition, subtraction

<= < > >=

comparison operator

== !=

equals operator

= %= /= //= -= += *= **=

assignment operator

not or and

Logical Operators

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325084055&siteId=291194637
Recommended