03python

 
 

python operator
game (global variable / exception handling); odd and even; guess Age (while / break / continue /) ; while + continue + else vs while + break + else





1

Operators python

True True correct
False false false

num + = 1 + equivalent. 1 NUM = NUM
NUM - =. 1 is equivalent to NUM = NUM -. 1
NUM = 2 * equivalent to 2 * NUM = NUM
NUM / 2 = equivalent to NUM = NUM / 2
NUM = 2 // equivalent to 2 // NUM = NUM
NUM = 2% is equivalent to 2% NUM = NUM
NUM NUM equivalent to the num = = 2 2

>>> 5/2
2.5
>>> 5 // # 2 except the floor / divisible
2
>>> 9% modulo 2 #
1


// returns the quotient divisible take the integer part
9 // 2 output 4, output 4.0 2.0 9.0 @

9//2.0
4.0

Python bitwise operators:

The bitwise operators is considered in binary digital calculated. Python bitwise algorithm is as follows:

  1. & Bitwise AND operator: involved in computing two values, if the corresponding two bits are 1, then the 1-bit result is 0 otherwise
  2. l or bitwise operators: two long as the corresponding binary bit is a 1, it is a result bit.
  3. ^ Bitwise exclusive OR operator: When two different corresponding binary, the result is a
  4. ~ Bitwise operators: for each binary data bit inverse, i.e., the 1 to 0, the 0 to 1. ~ X -x-1 like
  5. Mobile operator << left: each binary operand to the left a number of bits of all, the number of bits of a mobile "<<" specifies the right, discarding the upper, lower 0s.
  6. Mobile operator >> Right: the respective binary operand is ">>" All right a number of bits left, ">>" the number of bits specified right
  • # The following is a binary operation
  •   a = 0011 1100
  •      b = 0000 1101
  •  a&b = 0000 1100
  •   a|b = 0011 1101
  •   a^b = 0011 0001
  •    ~a = 1100 0011

 

Python member operator

  • If you find value in return True, False otherwise specified sequence.

x In y sequence returns True if x in y sequence.

  • If none is found not in the specified sequence value return True, otherwise False.

x is not y sequence, if x is not the sequence returns True y.

 

Python identity operator

  • is is is determined from the two identifiers are not an object reference

x is y, similar to the id (x) == id (y), if the reference is to the same object it returns True, otherwise Falseis not 

  • is determined is not the identifier is not referenced from two different objects

x is not y, similar id (a)! = id (b). If the reference is not the result of the same object returns True, otherwise it returns False

 id () function used to obtain the memory address of the object

 

Python operator precedence

 

  • ** Index (highest priority)
    • ~ - Bitwise inversion, Unary plus and minus (the last two named methods and @ + - @)
      • * /% // multiply, divide, modulo, and take divisible
        • + - Addition Subtraction
          • << >> right shift, left shift operator
            • & 位 ‘AND’
              • ^ L Bitwise Operators
                • <= <>> = Comparison operators
                  • <> ==! = Equality operator
                    • =% = / = @ = - = = + = = * assignment operator
                      • It is is not the identity of the operator
                        • in not in member operator
                          • not or and logical operators

 


and, and, and 
only when the two conditions are all True (correct), the result will be as True (correct)

Condition 1 and Condition 2
. 5>. 3. 6 and <2 True

or or or
as long as a condition is True, results Ture,
5> 3 or 6 <2
true or false

not no, Ya minute fly butterflies

not 5> 3 == false
not 5 <3 == true

Short circuit principle
for the front and if the first condition is false, then the expression evaluates to two conditions before and after this and will certainly make up for the false, the second condition would not be counted

Or for the
first condition is true the foregoing, the calculation result of the expression before and after the two conditions or composition will certainly be true, a second condition will not be calculated

True or True and False => True
foregoing is true back (including back and) not calculated

not not True or True and False => True
同理

and or in no particular order, that is, from left to right Operator (following the short circuit principle)

2

Games (global variable / exception handling); odd-even number; guess the age (while / break / continue /); while + continue + else vs while + break + else

  • 1, return # allows a function ends
  • 2, break # can make a loop end
  • 3, exit () # can make a program ends
, Ltd. Free Join A_   # define global variables 
# has been implementing the program 
the while True: 
    A_ = the INPUT ( ' >> ' )   # peer indent, otherwise an error 
    # exception handling 
    the try : 
        A_ = int (A_)
     the except :
         Print ( ' you enter non-numeric type ' ) 
        A_ = 0 

    B_ = 100 
    C_ = 1000 IF B_ <= A_ <= C_:
         Print ( " True " )
     the else

    :
        print("False")

12 >>
False
>> 123
True
>> FGH
you enter a non-numeric type
False
>>


# Odd and even 
NUM =. 1 the while NUM <= 100:   # NUM <= 100 is equivalent to True # the while NUM <= 100: True equivalent to the while: IF NUM% 2 == 0:
         Print (NUM) 
    NUM +. 1 = 
NUM =. 1 the while NUM <= 100 :
     IF NUM. 1% 2 == :
         Print (NUM) 
    NUM +. 1 =


    
    


 


# 猜年龄
age = 50

flag = True

while flag:
    user_input_age = int(input("Age is :"))
    if user_input_age == age:
        print("Yes")
        flag = False
    elif user_input_age > age:
        print("Is bigger")
    else:
        print("Is smaller")

print("End")

 


# break  # 终止
age = 50

while True:
    user_input_age = int(input("Age is :"))
    if user_input_age == age:
        print("Yes")
        break
    elif user_input_age > age:
        print("Is bigger")
    else:
        print("Is smaller")

print("End")

 


continue continue 

# continue 
# total of 10 cycles, when the continue execution, when the cycle ends 
NUM =. 1 the while NUM <= 10 : 
    NUM + =. 1
     IF NUM ==. 3 :
         continue Print (NUM)


    

2
4
5
6
7
8
9
10
11


# Continue 
# total of 10 cycles, when executed continue, the end of the cycle when 
NUM =. 1 the while NUM <= 10 : 
    NUM + =. 1
     IF NUM ==. 3 :
         Continue Print (NUM)
 the else :
     Print ( ' This IS the else Statement ' )

    

2
4
5
6
7
8
9
10
11
This is else statement

 

num = 1
while num <= 10:
    num += 1
    if num == 6:
        break
    print(num)
else:
    print('This is else statement')

2
3
4
5

 

while + continue + else vs while + break + else

  • else performed with the normal circulation procedures, e.g. continue,
  • else not performed in a case where the program is interrupted, for example, in front of or break Error

Guess you like

Origin www.cnblogs.com/LL-HLK/p/11080008.html