This is my 3 days to participate in the November Gwen Challenge, see the details of the event: the last Gwen Challenge 2021.

Member operator

In and not in are Python specific operators (all lowercase). It is used to determine whether an object belongs to a collection and runs very fast. Return True or False.

  1. judgeaWhether inlist1The list:
List1 = [1, 2, 3, 4, 5] a = 1 for I in list1: if I == a: print("a in list1 ") else: Print ('a not in list1 ') flag = False for I in list1: if I == a: flag = True break if flag: print('a not in list1 ') else: Print (" a not list1 element ") #, if use the in operator to do need not bother list1 = [1, 2, 3, 4, 5] a = 1 in list1: if a print (" a "is one of the elements in list1) else: Print ("a is not an element of list1 ")list1 = [1, 2, 3, 4, 5] a = 1 for I in list1: if I == a: print(" A is in list1 ") else: Print ('a not in list1 ') flag = False for I in list1: if I == a: flag = True break if flag: print('a not in list1 ') else: Print (" a not list1 element ") #, if use the in operator to do need not bother list1 = [1, 2, 3, 4, 5] a = 1 in list1: if a print (" a "is one of the elements in list1) else: Print ("a is not an element of list1 ")list1 = [1, 2, 3, 4, 5] a = 1 for I in list1: if I == a: print(" A is in list1 ") else: Print ('a not in list1 ') flag = False for I in list1: if I == a: flag = True break if flag: print('a not in list1 ') else: Print (" a not list1 element ") #, if use the in operator to do need not bother list1 = [1, 2, 3, 4, 5] a = 1 in list1: if a print (" a "is one of the elements in list1) else: Print (" A is not an element of list1 ")Copy the code

Identity operator

Python’s special syntax (all lowercase) :

  • isUsed to determine whether references to two variables are the same objectid()Get object).
  • is notThe effect is opposite.

⚠️ Note the difference between is and the comparison operator == :

  • = =Used to determine whether the value of the object referenced by a variable is equal.
> > > a = [1, 2, 3] > > > b = [1, 2, 3] > > > a is b False > > > a = b = True > > > a = 2 > > > b = 2.0 # by id () to check the memory address > > b > a is False >>> a == b TrueCopy the code

Ternary operator

The representation of the ternary operator in Python:

True_statements if expression else False_statements
Copy the code

Such as:

A = 1 b = 2 if a+b>3: print(a+b) else: print(b-a) a+b if a+b>3 else b-a #Copy the code

Operator priority

The following table lists all operators from highest to lowest priority. Operators with higher precedence are evaluated or processed first, and siblings are evaluated from left to right (except for assignment operators, which are evaluated from right to left) :

The operator describe
** Index (highest priority)
~ + – Bitwise flip, unary plus and minus (the last two methods are called +@ and -@)
% * / / / Multiply, divide, take modulus, take exact division
+ – Addition, subtraction
>> << Move right, move left
& A AND
^
< = < > > = Comparison operator
< > = =! = Equal operator
= %= /= //= -= += *= **= The assignment operator
is is not Identity operator
in not in Member operator
not or and Logical operator