current position:Home>How IOS developers learn python programming 3-operator 2

How IOS developers learn python programming 3-operator 2

2022-01-31 04:41:32 Sissy's father

This is my participation 11 Yue Gengwen challenges 3 God , Check out the activity details :2021 The last time Gengwen challenge .

member operator

in And not in yes Python Unique operators ( It's all lowercase ). Used to determine whether an object belongs to a collection , Very fast . Back to True perhaps False.

  1. Judge a Whether in list1 In the list : 
list1 = [1, 2, 3, 4, 5]
a = 1

for i in list1:
    if i == a:
        print("a stay list1 in ")
    else:
        print('a be not in list1 in ')

flag = False
for i in list1:
    if i == a:
        flag = True
        break
if flag:
    print("a yes list1 One of the elements of ")
else:
    print("a No list1 The elements of ")
    
    
#  If you use in Operator , There's no need for such trouble 
list1 = [1, 2, 3, 4, 5]
a = 1
if a in list1:
    print("a yes list1 One of the elements of ")
else:
    print("a No list1 The elements of ")list1 = [1, 2, 3, 4, 5]
a = 1

for i in list1:
    if i == a:
        print("a stay list1 in ")
    else:
        print('a be not in list1 in ')

flag = False
for i in list1:
    if i == a:
        flag = True
        break
if flag:
    print("a yes list1 One of the elements of ")
else:
    print("a No list1 The elements of ")
    
    
#  If you use in Operator , There's no need for such trouble 
list1 = [1, 2, 3, 4, 5]
a = 1
if a in list1:
    print("a yes list1 One of the elements of ")
else:
    print("a No list1 The elements of ")list1 = [1, 2, 3, 4, 5]
a = 1

for i in list1:
    if i == a:
        print("a stay list1 in ")
    else:
        print('a be not in list1 in ')

flag = False
for i in list1:
    if i == a:
        flag = True
        break
if flag:
    print("a yes list1 One of the elements of ")
else:
    print("a No list1 The elements of ")
    
    
#  If you use in Operator , There's no need for such trouble 
list1 = [1, 2, 3, 4, 5]
a = 1
if a in list1:
    print("a yes list1 One of the elements of ")
else:
    print("a No list1 The elements of ")
 Copy code 

Identity operator

Python Characteristic grammar ( It's all lowercase ):

  • is Used to judge whether the reference of two variables is the same object ( You can use id() Get objects ).
  • is not opposite .

️ Be careful : is And comparison operators == The difference between :

  • == Used to determine whether the values of objects referenced by variables are equal .
>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a is b
False
>>> a == b
True

>>> a = 2
>>> b = 2.0 #  It can be done by id() Check the memory address  
>>> a is b  
False
>>> a == b
True
 Copy code 

Ternary operator

python The representation of the trinary operator in :

True_statements if expression else False_statements
 Copy code 

for example :

a = 1
b = 2
if a+b>3:
    print(a+b)
else:
    print(b-a)

a+b if a+b>3 else b-a   #  Ternary operator 
 Copy code 

Operator priority

The following table lists all operators from the highest to the lowest priority . Operators with higher priority have priority in calculation or processing , The same level is calculated from left to right ( Except for assignment operators , It's in right to left order ):

Operator describe
** Index ( The highest priority )
~ + - Flip by bit , One yuan plus sign and minus sign ( The last two methods are called [email protected] and [email protected]
* / % // ride 、 except 、 modulus 、 Divide and conquer
+ - Add 、 Subtraction
>> << Move right 、 Move left
& position AND
^
<= < > >= Comparison operator
<> == != Equals operator
= %= /= //= -= += *= **= Assignment operator
is is not Identity operator
in not in member operator
not or and Logical operators

copyright notice
author[Sissy's father],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310441294210.html

Random recommended