Python basis -day02-2

Operators

aims

  • Arithmetic operators
  • Comparison (relationship) operator
  • Logical Operators
  • Assignment Operators
  • Operator precedence

Mathematical symbol table link: https://zh.wikipedia.org/wiki/ mathematical symbol table

01. arithmetic operators

  • Is a complete basic arithmetic symbols used for arithmetic processing four
Operators description Examples
+ plus 10 + 20 = 30
- Less 10 - 20 = -10
* Multiply 10 * 20 = 200
/ except 10 / 20 = 0.5
// Take divisible (Supplier) 2 9 // output portion 4 to return the integer division
% Take the remainder Returns the division remainder = 1 2 9%
** power Also known as power, power, 2 3 = 8 **
  • In Python *operator results may also be used for string, the string is the calculation result of the specified number of times
In [1]: "-" * 50
Out[1]: '----------------------------------------' 

02. Comparison of (relationship) operator

Operators description
== Check the value of the two operands if equal , if so, the condition is true, returns True
!= Check the value of the two operands if not equal , if so, the condition is true, returns True
> Check whether the value of the left operand is greater than the value of the right operand, if so, the condition is true, returns True
< Check the value of the left operand if less than the value of the right operand, if so, the condition is true, returns True
>= Check whether the value of the left operand is greater than or equal to the value of the right operand, if so, the condition is true, returns True
<= Check the value of the left operand if less than or equal to the value of the right operand, if so, the condition is true, returns True

Python 2.x is judged not equal can also use the <>operator

!=In Python 2.x likewise be used to determine not equal

03. Logical Operators

Operators Logical expression description
and x and y Only the values of x and y are True, will return True
or as long as x or y has a value of False, it returns False
or x or y As long as x or y has a value of True, it returns True
only values of x and y are both False, will return False
not not x If x is True, it returns False
if x is False, returns True

04. Assignment Operators

  • In Python, use =may be assigned to variables
  • When arithmetic operations, in order to simplify preparation of the code, Pythonis also provided with a series of arithmetic operators corresponding to the assignment operator
  • Note: the middle of the assignment operator no spaces
Operators description Examples
= Simple assignment operator c = a + b a + b is the assignment of the operation result 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 = C = A is equivalent to C A
/= Division assignment operator c / = a is equivalent to c = c / a
//= Assignment operator take divisible c // = a is equivalent to c = c // a
%= Take modulus (remainder) assignment operator c% = a is equivalent to c = c% a
**= Power assignment operator C = C = A is equivalent to C A

05. Operator Priority

  • Arithmetic priority to the following table arranged from high to low order
Operators description
** Power (highest priority)
* / % // Multiply, divide, take the remainder, taking divisible
+ - Addition, subtraction
<= < > >= Comparison Operators
== != Equality operator
= %= /= //= -= += *= **= Assignment Operators
not or and Logical Operators

Guess you like

Origin www.cnblogs.com/jiexiaobai/p/12078909.html